37 #ifndef _PROTOBUF_COMM_SERVER_H_
38 #define _PROTOBUF_COMM_SERVER_H_
40 #include <google/protobuf/message.h>
41 #include <protobuf_comm/frame_header.h>
42 #include <protobuf_comm/message_register.h>
43 #include <protobuf_comm/queue_entry.h>
45 #include <boost/asio.hpp>
46 #include <boost/enable_shared_from_this.hpp>
47 #include <boost/signals2.hpp>
49 #ifndef _GLIBCXX_USE_SCHED_YIELD
50 # define _GLIBCXX_USE_SCHED_YIELD
55 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
59 namespace protobuf_comm {
73 send(
ClientID client, uint16_t component_id, uint16_t msg_type, google::protobuf::Message &m);
75 uint16_t component_id,
77 std::shared_ptr<google::protobuf::Message> m);
78 void send(
ClientID client, std::shared_ptr<google::protobuf::Message> m);
79 void send(
ClientID client, google::protobuf::Message &m);
81 void send_to_all(uint16_t component_id, uint16_t msg_type, google::protobuf::Message &m);
84 std::shared_ptr<google::protobuf::Message> m);
85 void send_to_all(std::shared_ptr<google::protobuf::Message> m);
96 return *message_register_;
102 boost::signals2::signal<
103 void(
ClientID, uint16_t, uint16_t, std::shared_ptr<google::protobuf::Message>)> &
112 boost::signals2::signal<void(
ClientID, uint16_t, uint16_t, std::string)> &
115 return sig_recv_failed_;
121 boost::signals2::signal<void(
ClientID, boost::asio::ip::tcp::endpoint &)> &
124 return sig_connected_;
130 boost::signals2::signal<void(
ClientID,
const boost::system::error_code &)> &
133 return sig_disconnected_;
137 class Session :
public boost::enable_shared_from_this<Session>
141 typedef boost::shared_ptr<Session> Ptr;
148 boost::asio::ip::tcp::socket &
163 boost::asio::ip::tcp::endpoint &
166 return remote_endpoint_;
169 void start_session();
171 void send(uint16_t component_id, uint16_t msg_type, google::protobuf::Message &m);
175 void handle_read_message(
const boost::system::error_code &error);
176 void handle_read_header(
const boost::system::error_code &error);
177 void handle_write(
const boost::system::error_code &error,
184 boost::asio::ip::tcp::socket socket_;
185 boost::asio::ip::tcp::endpoint remote_endpoint_;
187 frame_header_t in_frame_header_;
188 size_t in_data_size_;
191 std::queue<QueueEntry *> outbound_queue_;
192 std::mutex outbound_mutex_;
193 bool outbound_active_;
199 void handle_accept(Session::Ptr new_session,
const boost::system::error_code &error);
201 void disconnected(boost::shared_ptr<Session> session,
const boost::system::error_code &error);
204 boost::asio::io_service io_service_;
205 boost::asio::ip::tcp::acceptor acceptor_;
206 boost::signals2::signal<
207 void(
ClientID, uint16_t, uint16_t, std::shared_ptr<google::protobuf::Message>)>
209 boost::signals2::signal<void(
ClientID, uint16_t, uint16_t, std::string)> sig_recv_failed_;
210 boost::signals2::signal<void(
ClientID, boost::asio::ip::tcp::endpoint &)> sig_connected_;
211 boost::signals2::signal<void(
ClientID,
const boost::system::error_code &)> sig_disconnected_;
213 std::thread asio_thread_;
215 std::map<ClientID, boost::shared_ptr<Session>> sessions_;
217 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
218 std::atomic<ClientID> next_cid_;
221 std::mutex next_cid_mutex_;
224 MessageRegister *message_register_;
225 bool own_message_register_;