37 #ifndef __PROTOBUF_COMM_PEER_H_ 38 #define __PROTOBUF_COMM_PEER_H_ 40 #include <protobuf_comm/frame_header.h> 41 #include <protobuf_comm/message_register.h> 42 #include <protobuf_comm/queue_entry.h> 44 #include <boost/asio.hpp> 45 #include <boost/signals2.hpp> 46 #include <google/protobuf/message.h> 57 class BufferEncryptor;
58 class BufferDecryptor;
63 enum { max_packet_length = 1024 };
67 unsigned short recv_on_port);
69 std::vector<std::string> &proto_path);
71 unsigned short recv_on_port, std::vector<std::string> &proto_path);
75 frame_header_version_t header_version = PB_FRAME_V2);
77 const std::string crypto_key,
const std::string cipher =
"aes-128-ecb");
79 const std::string crypto_key,
const std::string cipher =
"aes-128-ecb");
81 unsigned short recv_on_port,
82 const std::string crypto_key,
const std::string cipher =
"aes-128-ecb");
85 const std::string crypto_key,
const std::string cipher =
"aes-128-ecb");
90 void send(uint16_t component_id, uint16_t msg_type,
91 google::protobuf::Message &m);
92 void send(uint16_t component_id, uint16_t msg_type,
93 std::shared_ptr<google::protobuf::Message> m);
94 void send(std::shared_ptr<google::protobuf::Message> m);
95 void send(google::protobuf::Message &m);
99 void setup_crypto(
const std::string &key,
const std::string &cipher);
105 {
return *message_register_; }
109 boost::signals2::signal<void (boost::asio::ip::udp::endpoint &, uint16_t, uint16_t,
110 std::shared_ptr<google::protobuf::Message>)>
115 boost::signals2::signal<void (boost::asio::ip::udp::endpoint &,
frame_header_t &,
121 boost::signals2::signal<void (boost::asio::ip::udp::endpoint &, std::string)>
126 boost::signals2::signal<void (std::string)>
133 {
return sig_rcvd_; }
141 {
return sig_rcvd_raw_; }
147 {
return sig_recv_error_; }
153 {
return sig_send_error_; }
157 void ctor(
const std::string &address,
unsigned int send_to_port,
158 const std::string crypto_key =
"",
const std::string cipher =
"aes-128-ecb",
159 frame_header_version_t = PB_FRAME_V2);
160 void determine_local_endpoints();
164 void handle_resolve(
const boost::system::error_code& err,
165 boost::asio::ip::udp::resolver::iterator endpoint_iterator);
166 void handle_sent(
const boost::system::error_code& error,
168 void handle_recv(
const boost::system::error_code& error,
size_t bytes_rcvd);
171 boost::asio::io_service io_service_;
172 boost::asio::ip::udp::resolver resolver_;
173 boost::asio::ip::udp::socket socket_;
175 std::list<boost::asio::ip::udp::endpoint> local_endpoints_;
182 std::string send_to_address_;
184 std::queue<QueueEntry *> outbound_queue_;
185 std::mutex outbound_mutex_;
186 bool outbound_active_;
188 boost::asio::ip::udp::endpoint outbound_endpoint_;
189 boost::asio::ip::udp::endpoint in_endpoint_;
193 size_t in_data_size_;
194 size_t enc_in_data_size_;
198 std::thread asio_thread_;
200 bool own_message_register_;
202 frame_header_version_t frame_header_version_;
boost::signals2::signal< void(boost::asio::ip::udp::endpoint &, std::string)> signal_recv_error_type
Boost signal for an error during receiving a message.
void send(uint16_t component_id, uint16_t msg_type, google::protobuf::Message &m)
Send a message to other peers.
void send_raw(const frame_header_t &frame_header, const void *data, size_t data_size)
Send a raw message.
Register to map msg type numbers to Protobuf messages.
boost::signals2::signal< void(boost::asio::ip::udp::endpoint &, frame_header_t &, void *, size_t)> signal_received_raw_type
Boost signal for a received raw message.
void setup_crypto(const std::string &key, const std::string &cipher)
Setup encryption.
Communicate by broadcasting protobuf messages.
boost::signals2::signal< void(std::string)> signal_send_error_type
Boost signal for an error during sending a message.
signal_send_error_type & signal_send_error()
Signal that is invoked when sending a message failed.
signal_received_type & signal_received()
Signal that is invoked when a message has been received.
Decrypt buffers encrypted with BufferEncryptor.
ProtobufBroadcastPeer(const std::string address, unsigned short port)
Constructor.
boost::signals2::signal< void(boost::asio::ip::udp::endpoint &, uint16_t, uint16_t, std::shared_ptr< google::protobuf::Message >)> signal_received_type
Boost signal for a received message.
void set_filter_self(bool filter)
Set if to filter out own messages.
signal_received_raw_type & signal_received_raw()
Signal that is invoked when a message has been received.
signal_recv_error_type & signal_recv_error()
Signal that is invoked when receiving a message failed.
~ProtobufBroadcastPeer()
Destructor.
Encrypt buffers using AES128 in ECB mode.
MessageRegister & message_register()
Get the server's message register.