]> git.tdb.fi Git - libs/net.git/blob - source/communicator.h
Style update: spaces around assignments
[libs/net.git] / source / communicator.h
1 #ifndef MSP_NET_COMMUNICATOR_H_
2 #define MSP_NET_COMMUNICATOR_H_
3
4 #include "protocol.h"
5 #include "streamsocket.h"
6
7 namespace Msp {
8 namespace Net {
9
10 class Communicator
11 {
12 public:
13         sigc::signal<void> signal_handshake_done;
14
15 private:
16         StreamSocket &socket;
17         const Protocol &protocol;
18         ReceiverBase &receiver;
19         int handshake_status;
20         unsigned buf_size;
21         char *in_buf;
22         char *in_begin;
23         char *in_end;
24         char *out_buf;
25
26 public:
27         Communicator(StreamSocket &, const Protocol &, ReceiverBase &);
28         ~Communicator();
29
30         void initiate_handshake();
31         bool is_handshake_done() const { return handshake_status==2; }
32
33         template<typename P>
34         void send(const P &pkt)
35         {
36                 if(handshake_status!=2)
37                         throw InvalidState("Handshaking is not done");
38                 unsigned size = protocol.assemble(pkt, out_buf, buf_size);
39                 socket.write(out_buf, size);
40         }
41
42 private:
43         void data_available();
44         bool receive_packet(const Protocol &, ReceiverBase &);
45         void send_handshake();
46 };
47
48 } // namespace Net
49 } // namespace Msp
50
51 #endif