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