25#include <boost/bind/placeholders.hpp>
27#include <boost/bind/protect.hpp>
28#include <boost/shared_array.hpp>
29#include <boost/atomic.hpp>
32#ifndef ROBOTRACONTEUR_EMSCRIPTEN
33#include <boost/asio.hpp>
34#include <boost/asio/steady_timer.hpp>
39namespace RobotRaconteur
46class ASIOStreamBaseTransport :
public ITransportConnection,
public RR_ENABLE_SHARED_FROM_THIS<ASIOStreamBaseTransport>
50 struct message_queue_entry
52 RR_INTRUSIVE_PTR<Message> message;
53 boost::function<void(
const RR_SHARED_PTR<RobotRaconteurException>&)> callback;
56 RR_BOOST_ASIO_IO_CONTEXT& _io_context;
58 boost::atomic<bool> connected;
60 boost::shared_array<uint8_t> sendbuf;
63 boost::mutex send_lock;
64 boost::mutex sync_send_lock;
66 bool send_pause_request;
68 boost::function<void(
const boost::system::error_code&)> send_pause_request_handler;
70 std::list<message_queue_entry> send_queue;
71 size_t send_message_size;
72 boost::condition_variable send_event;
74 boost::atomic<boost::posix_time::ptime> tlastsend;
75 boost::atomic<boost::posix_time::ptime> tlastrecv;
77 boost::atomic<boost::posix_time::ptime> tlastheartbeat;
79 boost::array<uint8_t, 8> streammagic;
80 uint32_t recv_message_size;
81 boost::shared_array<uint8_t> recvbuf;
85 bool recv_pause_request;
88 boost::function<void(
const boost::system::error_code&)> recv_pause_request_handler;
90 boost::mutex recv_lock;
92 RR_SHARED_PTR<boost::asio::deadline_timer> heartbeat_timer;
94 uint32_t ReceiveTimeout;
95 uint32_t HeartbeatPeriod;
99 bool CheckStreamCapability_closed;
100 bool CheckStreamCapability_waiting;
101 boost::mutex CheckStreamCapability_lock;
102 boost::function<void(uint32_t,
const RR_SHARED_PTR<RobotRaconteurException>&)> CheckStreamCapability_callback;
104 boost::tuple<std::string, boost::function<void(uint32_t,
const RR_SHARED_PTR<RobotRaconteurException>&)> > >
105 CheckStreamCapability_queue;
107 RR_SHARED_PTR<boost::asio::deadline_timer> CheckStreamCapability_timer;
109 bool streamop_closed;
110 bool streamop_waiting;
111 boost::mutex streamop_lock;
112 boost::function<void(
const RR_SHARED_PTR<RRObject>&,
const RR_SHARED_PTR<RobotRaconteurException>&)>
114 std::queue<boost::tuple<
115 std::string, RR_SHARED_PTR<RRObject>,
116 boost::function<void(
const RR_SHARED_PTR<RRObject>&,
const RR_SHARED_PTR<RobotRaconteurException>&)> > >
119 RR_SHARED_PTR<boost::asio::deadline_timer> streamop_timer;
121 boost::shared_mutex RemoteNodeID_lock;
123 NodeID target_nodeid;
124 std::string target_nodename;
126 int32_t max_message_size;
128 bool send_large_transfer_authorized;
129 bool recv_large_transfer_authorized;
131 boost::atomic<bool> send_version4;
132 boost::atomic<bool> use_string_table4;
134 RR_SHARED_PTR<detail::StringTable> string_table4;
135 boost::mutex string_table4_lock;
136 std::list<uint32_t> string_table_4_confirming;
138 RR_UNORDERED_MAP<uint32_t, boost::tuple<std::vector<uint32_t>, boost::posix_time::ptime> > string_table_4_requests;
139 uint32_t string_table_4_requestid;
141 std::list<RR_SHARED_PTR<boost::asio::deadline_timer> > string_table_4_timers;
142 bool string_table_4_pause_updates;
144 bool string_table_4_closed;
148 bool disable_message4;
149 bool disable_string_table;
150 bool disable_async_io;
152 mutable_buffers active_recv_bufs;
154 RR_SHARED_PTR<AsyncMessageReader> async_reader;
156 size_t async_recv_size;
157 size_t async_recv_pos;
158 uint16_t async_recv_version;
159 size_t async_recv_continue_buf_count;
161 RR_SHARED_PTR<AsyncMessageWriter> async_writer;
163 uint16_t async_send_version;
164 const_buffers async_send_bufs;
166 uint32_t active_capabilities_message2_basic;
167 uint32_t active_capabilities_message4_basic;
168 uint32_t active_capabilities_message4_stringtable;
171 bool create_connection_complete;
173 ASIOStreamBaseTransport(
const RR_SHARED_PTR<RobotRaconteurNode>& node);
175 RR_WEAK_PTR<RobotRaconteurNode> node;
178 RR_OVIRTUAL RR_SHARED_PTR<RobotRaconteurNode> GetNode() RR_OVERRIDE;
181 ASIOStreamBaseTransport(const ASIOStreamBaseTransport& that);
184 class AsyncAttachStream_args : public RRObject
187 RobotRaconteur::NodeID nodeid;
188 std::string nodename;
191 AsyncAttachStream_args(
const RobotRaconteur::NodeID& nodeid_, boost::string_ref nodename_)
192 : nodeid(nodeid_), nodename(RR_MOVE(nodename_.to_string()))
196 RR_OVIRTUAL std::string RRType() RR_OVERRIDE
198 return "RobotRaconteur::ASIOStreamBaseTransport::AsyncAttachStream_args";
202 virtual void AsyncAttachStream(
203 bool server,
const NodeID& target_nodeid, boost::string_ref target_nodename,
204 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
207 RR_OVIRTUAL
void AsyncSendMessage(
208 const RR_INTRUSIVE_PTR<Message>& m,
209 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback) RR_OVERRIDE;
210 RR_OVIRTUAL
void SendMessage(
const RR_INTRUSIVE_PTR<Message>& m) RR_OVERRIDE;
213 void SimpleAsyncSendMessage(
const RR_INTRUSIVE_PTR<Message>& m,
214 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
216 void SimpleAsyncEndSendMessage(
const RR_SHARED_PTR<RobotRaconteurException>& err);
218 virtual void AsyncAttachStream1(
219 const RR_SHARED_PTR<RRObject>& parameter,
const RR_SHARED_PTR<RobotRaconteurException>& err,
220 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
222 virtual void BeginSendMessage(
const RR_INTRUSIVE_PTR<Message>& m,
223 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
224 virtual void BeginSendMessage1(
225 const RR_INTRUSIVE_PTR<Message>& m,
226 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
228 virtual void EndSendMessage(
size_t startpos,
const boost::system::error_code& error,
size_t bytes_transferred,
229 const RR_INTRUSIVE_PTR<Message>& m,
size_t m_len,
230 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback,
231 const boost::shared_array<uint8_t>& buf);
232 virtual void EndSendMessage1();
234 virtual void EndSendMessage2(
const boost::system::error_code& error,
size_t bytes_transferred,
235 const boost::function<
void(
const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
237 virtual void AsyncPauseSend(
const boost::function<
void(
const boost::system::error_code&)>& handler);
238 virtual void AsyncResumeSend();
240 virtual void BeginReceiveMessage1();
242 virtual void EndReceiveMessage1(
size_t startpos,
const boost::system::error_code& error,
size_t bytes_transferred);
244 virtual void EndReceiveMessage2(
size_t startpos,
const boost::system::error_code& error,
size_t bytes_transferred,
245 size_t message_size,
const boost::shared_array<uint8_t>& buf);
247 virtual void EndReceiveMessage3(
const RR_INTRUSIVE_PTR<Message>& message);
248 virtual void EndReceiveMessage4();
250 virtual void EndReceiveMessage5(
const boost::system::error_code& error,
size_t bytes_transferred);
252 virtual void AsyncPauseReceive(
const boost::function<
void(
const boost::system::error_code&)>& handler);
253 virtual void AsyncResumeReceive();
255 RR_OVIRTUAL
void Close() RR_OVERRIDE;
257 virtual
void heartbeat_timer_func(const boost::system::error_code& e);
259 boost::mutex heartbeat_timer_lock;
262 virtual
void MessageReceived(const RR_INTRUSIVE_PTR<Message>& m) = 0;
264 virtual
bool IsConnected();
266 virtual uint32_t StreamCapabilities(boost::string_ref name);
268 virtual
void AsyncCheckStreamCapability(
269 boost::string_ref name,
270 const boost::function<
void(uint32_t, const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
273 virtual
void BeginCheckStreamCapability(
274 boost::string_ref name,
275 const boost::function<
void(uint32_t, const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
277 void CheckStreamCapability_EndSendMessage(const RR_SHARED_PTR<RobotRaconteurException>& err);
279 static
void CheckStreamCapability_timercallback(RR_WEAK_PTR<ASIOStreamBaseTransport> t,
280 const boost::system::error_code& e);
282 void CheckStreamCapability_MessageReceived(const RR_INTRUSIVE_PTR<Message>& m);
286 boost::string_ref command, const RR_SHARED_PTR<RRObject>& args,
287 boost::function<
void(const RR_SHARED_PTR<RRObject>&, const RR_SHARED_PTR<RobotRaconteurException>&)>&);
289 virtual
void PeriodicCleanupTask();
291 RR_OVIRTUAL NodeID GetRemoteNodeID() RR_OVERRIDE;
294 virtual
void BeginStreamOp(
295 boost::string_ref command, const RR_SHARED_PTR<RRObject>& args,
296 boost::function<
void(const RR_SHARED_PTR<RRObject>&, const RR_SHARED_PTR<RobotRaconteurException>&)>&);
298 virtual RR_INTRUSIVE_PTR<MessageEntry> PackStreamOpRequest(boost::string_ref command,
299 const RR_SHARED_PTR<RRObject>& args);
301 virtual
void StreamOp_EndSendMessage(const RR_SHARED_PTR<RobotRaconteurException>& err);
303 static
void StreamOp_timercallback(RR_WEAK_PTR<ASIOStreamBaseTransport> t, const boost::system::error_code& e);
305 virtual
void StreamOpMessageReceived(const RR_INTRUSIVE_PTR<Message>& m);
307 virtual RR_INTRUSIVE_PTR<MessageEntry> ProcessStreamOpRequest(const RR_INTRUSIVE_PTR<MessageEntry>& request,
308 const RR_INTRUSIVE_PTR<MessageHeader>& header);
310 virtual RR_SHARED_PTR<RRObject> UnpackStreamOpResponse(const RR_INTRUSIVE_PTR<MessageEntry>& response,
311 const RR_INTRUSIVE_PTR<MessageHeader>& header);
313 virtual
void async_write_some(
315 const boost::function<
void(const boost::system::error_code& error,
size_t bytes_transferred)>& handler) = 0;
317 virtual
void async_read_some(
319 const boost::function<
void(const boost::system::error_code& error,
size_t bytes_transferred)>& handler) = 0;
321 virtual
size_t available() = 0;
323 virtual
bool IsLargeTransferAuthorized();
326 virtual
bool GetDisableMessage4();
327 virtual
void SetDisableMessage4(
bool d);
329 virtual
bool GetDisableStringTable();
330 virtual
void SetDisableStringTable(
bool d);
332 RR_OVIRTUAL
bool CheckCapabilityActive(uint32_t cap) RR_OVERRIDE;