]> git.tdb.fi Git - libs/net.git/blob - source/net/communicator.h
Increase Communicator buffer size to 64k
[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 class incompatible_protocol: public std::runtime_error
20 {
21 public:
22         incompatible_protocol(const std::string &w): std::runtime_error(w) { }
23         virtual ~incompatible_protocol() throw() { }
24 };
25
26
27 class Communicator
28 {
29 public:
30         sigc::signal<void> signal_handshake_done;
31         sigc::signal<void, const std::exception &> signal_error;
32
33 private:
34         StreamSocket &socket;
35         const Protocol &protocol;
36         ReceiverBase &receiver;
37         int handshake_status;
38         unsigned buf_size;
39         char *in_buf;
40         char *in_begin;
41         char *in_end;
42         char *out_buf;
43         bool good;
44
45 public:
46         Communicator(StreamSocket &, const Protocol &, ReceiverBase &);
47         ~Communicator();
48
49         void initiate_handshake();
50         bool is_handshake_done() const { return handshake_status==2; }
51
52         template<typename P>
53         void send(const P &);
54
55 private:
56         void send_data(unsigned);
57
58         void data_available();
59         bool receive_packet(const Protocol &, ReceiverBase &);
60         void send_handshake();
61 };
62
63 template<typename P>
64 void Communicator::send(const P &pkt)
65 {
66         send_data(protocol.serialize(pkt, out_buf, buf_size));
67 }
68
69 } // namespace Net
70 } // namespace Msp
71
72 #endif