1 #ifndef MSP_NET_COMMUNICATOR_H_
2 #define MSP_NET_COMMUNICATOR_H_
4 #include <msp/core/except.h>
5 #include <msp/core/noncopyable.h>
6 #include <sigc++/signal.h>
14 class sequence_error: public invalid_state
17 sequence_error(const std::string &w): invalid_state(w) { }
20 class incompatible_protocol: public std::runtime_error
23 incompatible_protocol(const std::string &w): std::runtime_error(w) { }
27 class Communicator: public NonCopyable
30 sigc::signal<void> signal_handshake_done;
31 sigc::signal<void, const std::exception &> signal_error;
35 const Protocol &protocol;
36 ReceiverBase &receiver;
37 int handshake_status = 0;
38 std::size_t buf_size = 65536;
39 char *in_buf = nullptr;
40 char *in_begin = nullptr;
41 char *in_end = nullptr;
42 char *out_buf = nullptr;
46 Communicator(StreamSocket &, const Protocol &, ReceiverBase &);
49 void initiate_handshake();
50 bool is_handshake_done() const { return handshake_status==2; }
56 void send_data(std::size_t);
58 void data_available();
59 bool receive_packet(const Protocol &, ReceiverBase &);
60 void send_handshake();
64 void Communicator::send(const P &pkt)
66 send_data(protocol.serialize(pkt, out_buf, buf_size));