// InStream::ReceiveHandler implementation virtual void onReceive(const void* data, int size) { std::string key = remoteHost.keyStr(); if (os.isValid()) { // -> already initialized std::string s((char*) data, size); printf("Received %d bytes from %s: %s\n", size, key.c_str(), s.c_str()); server.broadcastMessage(data, size); } else { // -> connect outstream if (size == sizeof(ServiceId)) { ServiceId service = *((ServiceId*) data); printf("Received client callback service id %d from %s\n", service, key.c_str()); os = remoteHost.connect(service); ready = true; sendMessage("HELLO", 6); } else { // invalid init message printf("Received invalid callback service id from %s\n", key.c_str()); is.reset(); server.removeConnection(this); delete this; return; } } is.read(this); }
// OutStream::ShutdownHandler implementation virtual void onShutdown(bool success) { printf("Shut down %s.\n", (success) ? "successfully" : "failed"); if (os.isValid() && !success) os.reset(); server.removeConnection(this); delete this; }
virtual void onReset() { // (InStream OR OutStream reset) printf("Reset.\n"); is.reset(); if (os.isValid()) os.reset(); server.removeConnection(this); }
void sendMessage(const void* data, int size) { if (os.isValid() && ready) { // printf("sendMessage()\n"); ready = false; os.write(data, size, this); } else printf("sendMessage(): %s not ready!\n", remoteHost.keyStr().c_str()); }
virtual void onShutdown() { printf("Client shutdown.\n"); ready = false; if (os.isValid()) { printf(" Shutting down outstream.\n"); os.shutdown(this); } else { printf(" Done.\n"); server.removeConnection(this); delete this; } }