]> git.tdb.fi Git - libs/net.git/blob - source/communicator.h
Add function to check if handshake is done
[libs/net.git] / source / communicator.h
1 /* $Id$
2
3 This file is part of libmspnet
4 Copyright © 2009  Mikkosoft Productions, Mikko Rasa
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_NET_COMMUNICATOR_H_
9 #define MSP_NET_COMMUNICATOR_H_
10
11 #include "protocol.h"
12 #include "streamsocket.h"
13
14 namespace Msp {
15 namespace Net {
16
17 class Communicator
18 {
19 public:
20         sigc::signal<void> signal_handshake_done;
21
22 private:
23         StreamSocket &socket;
24         const Protocol &protocol;
25         ReceiverBase &receiver;
26         int handshake_status;
27         unsigned buf_size;
28         char *in_buf;
29         char *in_begin;
30         char *in_end;
31         char *out_buf;
32
33 public:
34         Communicator(StreamSocket &, const Protocol &, ReceiverBase &);
35         ~Communicator();
36
37         void initiate_handshake();
38         bool is_handshake_done() const { return handshake_status==2; }
39
40         template<typename P>
41         void send(const P &pkt)
42         {
43                 if(handshake_status!=2)
44                         throw InvalidState("Handshaking is not done");
45                 unsigned size=protocol.assemble(pkt, out_buf, buf_size);
46                 socket.write(out_buf, size);
47         }
48
49 private:
50         void data_available();
51         bool receive_packet(const Protocol &, ReceiverBase &);
52         void send_handshake();
53 };
54
55 } // namespace Net
56 } // namespace Msp
57
58 #endif