]> git.tdb.fi Git - libs/net.git/blobdiff - source/communicator.h
Add protocol framework
[libs/net.git] / source / communicator.h
diff --git a/source/communicator.h b/source/communicator.h
new file mode 100644 (file)
index 0000000..144f872
--- /dev/null
@@ -0,0 +1,57 @@
+/* $Id$
+
+This file is part of libmspnet
+Copyright © 2009  Mikkosoft Productions, Mikko Rasa
+Distributed under the LGPL
+*/
+
+#ifndef MSP_NET_COMMUNICATOR_H_
+#define MSP_NET_COMMUNICATOR_H_
+
+#include "protocol.h"
+#include "streamsocket.h"
+
+namespace Msp {
+namespace Net {
+
+class Communicator
+{
+public:
+       sigc::signal<void> signal_handshake_done;
+
+private:
+       StreamSocket &socket;
+       const Protocol &protocol;
+       ReceiverBase &receiver;
+       int handshake_status;
+       unsigned buf_size;
+       char *in_buf;
+       char *in_begin;
+       char *in_end;
+       char *out_buf;
+
+public:
+       Communicator(StreamSocket &, const Protocol &, ReceiverBase &);
+       ~Communicator();
+
+       void initiate_handshake();
+
+       template<typename P>
+       void send(const P &pkt)
+       {
+               if(handshake_status!=2)
+                       throw InvalidState("Handshaking is not done");
+               unsigned size=protocol.assemble(pkt, out_buf, buf_size);
+               socket.write(out_buf, size);
+       }
+
+private:
+       void data_available();
+       bool receive_packet(const Protocol &, ReceiverBase &);
+       void send_handshake();
+};
+
+} // namespace Net
+} // namespace Msp
+
+#endif