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>
7 #include "mspnet_api.h"
15 class MSPNET_API sequence_error: public invalid_state
18 sequence_error(const std::string &w): invalid_state(w) { }
21 class MSPNET_API incompatible_protocol: public std::runtime_error
24 incompatible_protocol(const std::string &w): std::runtime_error(w) { }
28 class MSPNET_API Communicator: public NonCopyable
31 sigc::signal<void> signal_handshake_done;
32 sigc::signal<void, const std::exception &> signal_error;
36 const Protocol &protocol;
37 ReceiverBase &receiver;
38 int handshake_status = 0;
39 std::size_t buf_size = 65536;
40 char *in_buf = nullptr;
41 char *in_begin = nullptr;
42 char *in_end = nullptr;
43 char *out_buf = nullptr;
47 Communicator(StreamSocket &, const Protocol &, ReceiverBase &);
50 void initiate_handshake();
51 bool is_handshake_done() const { return handshake_status==2; }
57 void send_data(std::size_t);
59 void data_available();
60 bool receive_packet(const Protocol &, ReceiverBase &);
61 void send_handshake();
65 void Communicator::send(const P &pkt)
67 send_data(protocol.serialize(pkt, out_buf, buf_size));