Robot Raconteur Core C++ Library
Loading...
Searching...
No Matches
ASIOStreamBaseTransport.h
Go to the documentation of this file.
1
23
25#include <boost/bind/placeholders.hpp>
26#include <list>
27#include <boost/bind/protect.hpp>
28#include <boost/shared_array.hpp>
29#include <boost/atomic.hpp>
31
32#ifndef ROBOTRACONTEUR_EMSCRIPTEN
33#include <boost/asio.hpp>
34#include <boost/asio/steady_timer.hpp>
35#endif
36
37#pragma once
38
39namespace RobotRaconteur
40{
41
42namespace detail
43{
44class StringTable;
45
46class ASIOStreamBaseTransport : public ITransportConnection, public RR_ENABLE_SHARED_FROM_THIS<ASIOStreamBaseTransport>
47{
48
49 protected:
50 struct message_queue_entry
51 {
52 RR_INTRUSIVE_PTR<Message> message;
53 boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> callback;
54 };
55
56 RR_BOOST_ASIO_IO_CONTEXT& _io_context;
57
58 boost::atomic<bool> connected;
59
60 boost::shared_array<uint8_t> sendbuf;
61 size_t sendbuf_len;
62
63 boost::mutex send_lock;
64 boost::mutex sync_send_lock;
65 bool sending;
66 bool send_pause_request;
67 bool send_paused;
68 boost::function<void(const boost::system::error_code&)> send_pause_request_handler;
69
70 std::list<message_queue_entry> send_queue;
71 size_t send_message_size;
72 boost::condition_variable send_event;
73
74 boost::atomic<boost::posix_time::ptime> tlastsend;
75 boost::atomic<boost::posix_time::ptime> tlastrecv;
76 // When the last heartbeat was queued (tlastsend only changes when a send completes)
77 boost::atomic<boost::posix_time::ptime> tlastheartbeat;
78
79 boost::array<uint8_t, 8> streammagic;
80 uint32_t recv_message_size;
81 boost::shared_array<uint8_t> recvbuf;
82 size_t recvbuf_len;
83 size_t recvbuf_pos;
84 size_t recvbuf_end;
85 bool recv_pause_request;
86 bool recv_paused;
87 bool receiving;
88 boost::function<void(const boost::system::error_code&)> recv_pause_request_handler;
89
90 boost::mutex recv_lock;
91
92 RR_SHARED_PTR<boost::asio::deadline_timer> heartbeat_timer;
93
94 uint32_t ReceiveTimeout;
95 uint32_t HeartbeatPeriod;
96
97 bool SendHeartbeat;
98
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;
103 std::queue<
104 boost::tuple<std::string, boost::function<void(uint32_t, const RR_SHARED_PTR<RobotRaconteurException>&)> > >
105 CheckStreamCapability_queue;
106
107 RR_SHARED_PTR<boost::asio::deadline_timer> CheckStreamCapability_timer;
108
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>&)>
113 streamop_callback;
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>&)> > >
117 streamop_queue;
118
119 RR_SHARED_PTR<boost::asio::deadline_timer> streamop_timer;
120 NodeID RemoteNodeID;
121 boost::shared_mutex RemoteNodeID_lock;
122
123 NodeID target_nodeid;
124 std::string target_nodename;
125
126 int32_t max_message_size;
127
128 bool send_large_transfer_authorized;
129 bool recv_large_transfer_authorized;
130
131 boost::atomic<bool> send_version4;
132 boost::atomic<bool> use_string_table4;
133
134 RR_SHARED_PTR<detail::StringTable> string_table4;
135 boost::mutex string_table4_lock;
136 std::list<uint32_t> string_table_4_confirming;
137
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;
140
141 std::list<RR_SHARED_PTR<boost::asio::deadline_timer> > string_table_4_timers;
142 bool string_table_4_pause_updates;
143
144 bool string_table_4_closed;
145
146 bool server;
147
148 bool disable_message4;
149 bool disable_string_table;
150 bool disable_async_io;
151
152 mutable_buffers active_recv_bufs;
153
154 RR_SHARED_PTR<AsyncMessageReader> async_reader;
155
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;
160
161 RR_SHARED_PTR<AsyncMessageWriter> async_writer;
162
163 uint16_t async_send_version;
164 const_buffers async_send_bufs;
165
166 uint32_t active_capabilities_message2_basic;
167 uint32_t active_capabilities_message4_basic;
168 uint32_t active_capabilities_message4_stringtable;
169
170 // Server: a CreateConnection request has been accepted (protected by RemoteNodeID_lock)
171 bool create_connection_complete;
172
173 ASIOStreamBaseTransport(const RR_SHARED_PTR<RobotRaconteurNode>& node);
174
175 RR_WEAK_PTR<RobotRaconteurNode> node;
176
177 public:
178 RR_OVIRTUAL RR_SHARED_PTR<RobotRaconteurNode> GetNode() RR_OVERRIDE;
179
180 private:
181 ASIOStreamBaseTransport(const ASIOStreamBaseTransport& that);
182
183 protected:
184 class AsyncAttachStream_args : public RRObject
185 {
186 public:
187 RobotRaconteur::NodeID nodeid;
188 std::string nodename;
189
190 // NOLINTBEGIN(bugprone-throw-keyword-missing)
191 AsyncAttachStream_args(const RobotRaconteur::NodeID& nodeid_, boost::string_ref nodename_)
192 : nodeid(nodeid_), nodename(RR_MOVE(nodename_.to_string()))
193 {}
194 // NOLINTEND(bugprone-throw-keyword-missing)
195
196 RR_OVIRTUAL std::string RRType() RR_OVERRIDE
197 {
198 return "RobotRaconteur::ASIOStreamBaseTransport::AsyncAttachStream_args";
199 }
200 };
201
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);
205
206 public:
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;
211
212 protected:
213 void SimpleAsyncSendMessage(const RR_INTRUSIVE_PTR<Message>& m,
214 const boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
215
216 void SimpleAsyncEndSendMessage(const RR_SHARED_PTR<RobotRaconteurException>& err);
217
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);
221
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);
227
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();
233
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);
236
237 virtual void AsyncPauseSend(const boost::function<void(const boost::system::error_code&)>& handler);
238 virtual void AsyncResumeSend();
239
240 virtual void BeginReceiveMessage1();
241
242 virtual void EndReceiveMessage1(size_t startpos, const boost::system::error_code& error, size_t bytes_transferred);
243
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);
246
247 virtual void EndReceiveMessage3(const RR_INTRUSIVE_PTR<Message>& message);
248 virtual void EndReceiveMessage4();
249
250 virtual void EndReceiveMessage5(const boost::system::error_code& error, size_t bytes_transferred);
251
252 virtual void AsyncPauseReceive(const boost::function<void(const boost::system::error_code&)>& handler);
253 virtual void AsyncResumeReceive();
254
255 RR_OVIRTUAL void Close() RR_OVERRIDE;
256
257 virtual void heartbeat_timer_func(const boost::system::error_code& e);
258
259 boost::mutex heartbeat_timer_lock;
260
261 public:
262 virtual void MessageReceived(const RR_INTRUSIVE_PTR<Message>& m) = 0;
263
264 virtual bool IsConnected();
265
266 virtual uint32_t StreamCapabilities(boost::string_ref name);
267
268 virtual void AsyncCheckStreamCapability(
269 boost::string_ref name,
270 const boost::function<void(uint32_t, const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
271
272 protected:
273 virtual void BeginCheckStreamCapability(
274 boost::string_ref name,
275 const boost::function<void(uint32_t, const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
276
277 void CheckStreamCapability_EndSendMessage(const RR_SHARED_PTR<RobotRaconteurException>& err);
278
279 static void CheckStreamCapability_timercallback(RR_WEAK_PTR<ASIOStreamBaseTransport> t,
280 const boost::system::error_code& e);
281
282 void CheckStreamCapability_MessageReceived(const RR_INTRUSIVE_PTR<Message>& m);
283
284 public:
285 void AsyncStreamOp(
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>&)>&);
288
289 virtual void PeriodicCleanupTask();
290
291 RR_OVIRTUAL NodeID GetRemoteNodeID() RR_OVERRIDE;
292
293 protected:
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>&)>&);
297
298 virtual RR_INTRUSIVE_PTR<MessageEntry> PackStreamOpRequest(boost::string_ref command,
299 const RR_SHARED_PTR<RRObject>& args);
300
301 virtual void StreamOp_EndSendMessage(const RR_SHARED_PTR<RobotRaconteurException>& err);
302
303 static void StreamOp_timercallback(RR_WEAK_PTR<ASIOStreamBaseTransport> t, const boost::system::error_code& e);
304
305 virtual void StreamOpMessageReceived(const RR_INTRUSIVE_PTR<Message>& m);
306
307 virtual RR_INTRUSIVE_PTR<MessageEntry> ProcessStreamOpRequest(const RR_INTRUSIVE_PTR<MessageEntry>& request,
308 const RR_INTRUSIVE_PTR<MessageHeader>& header);
309
310 virtual RR_SHARED_PTR<RRObject> UnpackStreamOpResponse(const RR_INTRUSIVE_PTR<MessageEntry>& response,
311 const RR_INTRUSIVE_PTR<MessageHeader>& header);
312
313 virtual void async_write_some(
314 const_buffers& b,
315 const boost::function<void(const boost::system::error_code& error, size_t bytes_transferred)>& handler) = 0;
316
317 virtual void async_read_some(
318 mutable_buffers& b,
319 const boost::function<void(const boost::system::error_code& error, size_t bytes_transferred)>& handler) = 0;
320
321 virtual size_t available() = 0;
322
323 virtual bool IsLargeTransferAuthorized();
324
325 public:
326 virtual bool GetDisableMessage4();
327 virtual void SetDisableMessage4(bool d);
328
329 virtual bool GetDisableStringTable();
330 virtual void SetDisableStringTable(bool d);
331
332 RR_OVIRTUAL bool CheckCapabilityActive(uint32_t cap) RR_OVERRIDE;
333};
334
335} // namespace detail
336} // namespace RobotRaconteur