23 #include "common_priv.h"
29 #include <Variant/Variant.h>
67 std::string message = libvariant::SerializeJSON(msg);
69 unsigned numwritten = 0;
70 while (numwritten < message.size() + 1) {
71 numwritten +=
sock.
Write(message.c_str() + numwritten, (message.size() + 1) - numwritten);
77 std::vector<char> buf(4*1024);
83 if (buf.size() - num <= 0) {
84 buf.resize(buf.size()*2);
86 unsigned numread =
sock.
Recv(&buf[num], buf.size() - num,
false);
95 char *end = (
char*)memchr(&buf[0], 0, num);
97 Variant v = libvariant::DeserializeJSON(&buf[0]);
100 num -= (end - &buf[0]);
101 memmove(&buf[0], end, num);
102 end = (
char*)memchr(&buf[0], 0, num);
107 fprintf(stderr,
"RemoteContext: Uncaught errno exception (%d): %s\n", e.
Error(), e.
what());
void ShutdownWrite()
Shutdown the write end of this socket. Any future attempt to write to this socket will fail...
An abstraction of a socket address with convenience methods.
unsigned Recv(void *ptr, unsigned len, bool block)
void DispatchMessage(const Variant &msg)
virtual void Terminate()
Signal to the Context that the network is terminating. After this call most methods will throw a Shut...
void SendMessage(const Variant &msg)
static int Poll(IteratorRef< FileHandle * > begin, IteratorRef< FileHandle * > end, double timeout)
poll a list of FileHandles for any activity and call the appropriate On method.
RemoteContext(const SocketAddress &addr)
std::auto_ptr< Pthread > thread
void Close()
Close the file and reset the internal state.
void SetNoDelay(bool nodelay)
PthreadFunctional * CreatePthreadFunctional(T *obj, void *(T::*meth)(void))
bool Readable(bool r)
Set that the file is currently readable or not.
std::vector< SocketAddress > SockAddrList
virtual const char * what() const
bool Good() const
Convenience method for testing if the file this FileHandle has is open and not at end of file...
void Connect(const SocketAddress &addr)
Create a new socket and try to connect to the given address.
unsigned Write(const void *ptr, unsigned len)
Write data to the file descriptor.