]> git.tdb.fi Git - libs/net.git/blob - source/communicator.h
Add an overload for resolve that takes host and service separately
[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 sequence_error: public std::logic_error
11 {
12 public:
13         sequence_error(const std::string &w): std::logic_error(w) { }
14         virtual ~sequence_error() throw() { }
15 };
16
17
18 class Communicator
19 {
20 public:
21         sigc::signal<void> signal_handshake_done;
22
23 private:
24         StreamSocket &socket;
25         const Protocol &protocol;
26         ReceiverBase &receiver;
27         int handshake_status;
28         unsigned buf_size;
29         char *in_buf;
30         char *in_begin;
31         char *in_end;
32         char *out_buf;
33         bool good;
34
35 public:
36         Communicator(StreamSocket &, const Protocol &, ReceiverBase &);
37         ~Communicator();
38
39         void initiate_handshake();
40         bool is_handshake_done() const { return handshake_status==2; }
41
42         template<typename P>
43         void send(const P &pkt)
44         {
45                 if(!good)
46                         throw sequence_error("connection aborted");
47                 if(handshake_status!=2)
48                         throw sequence_error("handshaking not done");
49                 unsigned size = protocol.assemble(pkt, out_buf, buf_size);
50                 socket.write(out_buf, size);
51         }
52
53 private:
54         void data_available();
55         bool receive_packet(const Protocol &, ReceiverBase &);
56         void send_handshake();
57 };
58
59 } // namespace Net
60 } // namespace Msp
61
62 #endif