]> git.tdb.fi Git - libs/net.git/blob - source/net/communicator.h
af5ff58791ce1ad72a1d97c07591264927bfae64
[libs/net.git] / source / net / communicator.h
1 #ifndef MSP_NET_COMMUNICATOR_H_
2 #define MSP_NET_COMMUNICATOR_H_
3
4 #include <sigc++/signal.h>
5 #include "protocol.h"
6
7 namespace Msp {
8 namespace Net {
9
10 class StreamSocket;
11
12 class sequence_error: public std::logic_error
13 {
14 public:
15         sequence_error(const std::string &w): std::logic_error(w) { }
16         virtual ~sequence_error() throw() { }
17 };
18
19
20 class Communicator
21 {
22 public:
23         sigc::signal<void> signal_handshake_done;
24         sigc::signal<void, const std::exception &> signal_error;
25
26 private:
27         StreamSocket &socket;
28         const Protocol &protocol;
29         ReceiverBase &receiver;
30         int handshake_status;
31         unsigned buf_size;
32         char *in_buf;
33         char *in_begin;
34         char *in_end;
35         char *out_buf;
36         bool good;
37
38 public:
39         Communicator(StreamSocket &, const Protocol &, ReceiverBase &);
40         ~Communicator();
41
42         void initiate_handshake();
43         bool is_handshake_done() const { return handshake_status==2; }
44
45         template<typename P>
46         void send(const P &);
47
48 private:
49         void send_data(unsigned);
50
51         void data_available();
52         bool receive_packet(const Protocol &, ReceiverBase &);
53         void send_handshake();
54 };
55
56 template<typename P>
57 void Communicator::send(const P &pkt)
58 {
59         send_data(protocol.serialize(pkt, out_buf, buf_size));
60 }
61
62 } // namespace Net
63 } // namespace Msp
64
65 #endif