Robot Raconteur Core C++ Library
Loading...
Searching...
No Matches
TcpTransport.h
Go to the documentation of this file.
1
23
25#include <boost/shared_array.hpp>
26
27#pragma once
28
29namespace RobotRaconteur
30{
46
47class TcpTransportConnection;
48namespace detail
49{
50class TcpConnector;
51class TcpWebSocketConnector;
52class TcpWSSWebSocketConnector;
53class TcpSocketAcceptor;
54} // namespace detail
55
131class ROBOTRACONTEUR_CORE_API TcpTransport : public Transport, public RR_ENABLE_SHARED_FROM_THIS<TcpTransport>
132{
133 friend class TcpTransportConnection;
134 friend class detail::TcpConnector;
135 friend class detail::TcpWebSocketConnector;
136 friend class detail::TcpWSSWebSocketConnector;
137
138 private:
139 int32_t m_Port;
140
141 public:
142 RR_UNORDERED_MAP<uint32_t, RR_SHARED_PTR<ITransportConnection> > TransportConnections;
143 boost::mutex TransportConnections_lock;
144
145 std::list<RR_WEAK_PTR<ITransportConnection> > closing_TransportConnections;
146 std::list<RR_WEAK_PTR<ITransportConnection> > incoming_TransportConnections;
147
161 TcpTransport(const RR_SHARED_PTR<RobotRaconteurNode>& node = RobotRaconteurNode::sp());
162
163 RR_OVIRTUAL ~TcpTransport() RR_OVERRIDE;
164
165 RR_OVIRTUAL bool IsServer() const RR_OVERRIDE;
166 RR_OVIRTUAL bool IsClient() const RR_OVERRIDE;
167
178 virtual int32_t GetDefaultReceiveTimeout();
179
190 virtual void SetDefaultReceiveTimeout(int32_t milliseconds);
191
200 virtual int32_t GetDefaultConnectTimeout();
201
212 virtual void SetDefaultConnectTimeout(int32_t milliseconds);
213
214 RR_OVIRTUAL std::string GetUrlSchemeString() const RR_OVERRIDE;
215
216 virtual int32_t GetListenPort();
217
218 RR_OVIRTUAL void SendMessage(const RR_INTRUSIVE_PTR<Message>& m) RR_OVERRIDE;
219
220 RR_OVIRTUAL void AsyncSendMessage(
221 const RR_INTRUSIVE_PTR<Message>& m,
222 const boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)>& handler) RR_OVERRIDE;
223
224 RR_OVIRTUAL void AsyncCreateTransportConnection(
225 boost::string_ref url, const RR_SHARED_PTR<Endpoint>& e,
226 boost::function<void(const RR_SHARED_PTR<ITransportConnection>&,
227 const RR_SHARED_PTR<RobotRaconteurException>&)>& callback) RR_OVERRIDE;
228
229 RR_OVIRTUAL RR_SHARED_PTR<ITransportConnection> CreateTransportConnection(
230 boost::string_ref url, const RR_SHARED_PTR<Endpoint>& e) RR_OVERRIDE;
231
232 RR_OVIRTUAL void CloseTransportConnection(const RR_SHARED_PTR<Endpoint>& e) RR_OVERRIDE;
233
234 protected:
235 virtual void CloseTransportConnection_timed(const boost::system::error_code& err, const RR_SHARED_PTR<Endpoint>& e,
236 const RR_SHARED_PTR<void>& timer);
237
238 public:
249 virtual void StartServer(int32_t porte, bool localhost_only = false,
250 boost::function<bool(const boost::asio::ip::tcp::endpoint&)> accept_filter = 0);
251
261
268 virtual bool IsPortSharerRunning();
269
278 virtual void StartServer(const std::vector<boost::asio::ip::tcp::endpoint>& listen_endpoints,
279 boost::function<bool(const boost::asio::ip::tcp::endpoint&)> accept_filter = 0);
280
286 virtual std::vector<boost::asio::ip::tcp::endpoint> GetListenEndpoints();
287
293 virtual std::vector<boost::asio::ip::tcp::endpoint> GetResolvedListenEndpoints();
294
300 RR_OVIRTUAL std::vector<std::string> GetServerListenUrls() RR_OVERRIDE;
301
302 RR_OVIRTUAL bool CanConnectService(boost::string_ref url) RR_OVERRIDE;
303
304 RR_OVIRTUAL void Close() RR_OVERRIDE;
305
306 RR_OVIRTUAL void CheckConnection(uint32_t endpoint) RR_OVERRIDE;
307
316
322
331
337
346
354 void SetNodeAnnouncePeriod(int32_t millis);
355
356 void SendDiscoveryRequest();
357
358 RR_OVIRTUAL void PeriodicCleanupTask() RR_OVERRIDE;
359
360 RR_OVIRTUAL uint32_t TransportCapability(boost::string_ref name) RR_OVERRIDE;
361
362 static void GetLocalAdapterIPAddresses(std::vector<boost::asio::ip::address>& addresses);
363
364 RR_OVIRTUAL void MessageReceived(const RR_INTRUSIVE_PTR<Message>& m) RR_OVERRIDE;
365
376 virtual int32_t GetDefaultHeartbeatPeriod();
387 virtual void SetDefaultHeartbeatPeriod(int32_t milliseconds);
388
396 virtual int32_t GetMaxMessageSize();
397
405 virtual void SetMaxMessageSize(int32_t size);
406
414 virtual int32_t GetMaxConnectionCount();
422 virtual void SetMaxConnectionCount(int32_t count);
423
430 virtual bool GetRequireTls();
431
437 virtual void SetRequireTls(bool require_tls);
438
447
455
466 virtual bool IsTransportConnectionSecure(uint32_t endpoint);
467
469 virtual bool IsTransportConnectionSecure(const RR_SHARED_PTR<Endpoint>& endpoint);
470
472 virtual bool IsTransportConnectionSecure(const RR_SHARED_PTR<RRObject>& obj);
473
475 virtual bool IsTransportConnectionSecure(const RR_SHARED_PTR<ITransportConnection>& transport);
476
491 virtual bool IsSecurePeerIdentityVerified(uint32_t endpoint);
492
494 virtual bool IsSecurePeerIdentityVerified(const RR_SHARED_PTR<Endpoint>& endpoint);
496 virtual bool IsSecurePeerIdentityVerified(const RR_SHARED_PTR<RRObject>& obj);
498 virtual bool IsSecurePeerIdentityVerified(const RR_SHARED_PTR<ITransportConnection>& transport);
499
509 virtual std::string GetSecurePeerIdentity(uint32_t endpoint);
510
512 virtual std::string GetSecurePeerIdentity(const RR_SHARED_PTR<Endpoint>& endpoint);
514 virtual std::string GetSecurePeerIdentity(const RR_SHARED_PTR<RRObject>& obj);
516 virtual std::string GetSecurePeerIdentity(const RR_SHARED_PTR<ITransportConnection>& transport);
517
526 virtual bool GetAcceptWebSockets();
527
535 virtual void SetAcceptWebSockets(bool value);
536
544 virtual std::vector<std::string> GetWebSocketAllowedOrigins();
545
579 virtual void AddWebSocketAllowedOrigin(boost::string_ref origin);
580
588 virtual void RemoveWebSocketAllowedOrigin(boost::string_ref origin);
589
600 virtual bool GetDisableMessage4();
601
611 virtual void SetDisableMessage4(bool d);
612
624 virtual bool GetDisableStringTable();
625
636 virtual void SetDisableStringTable(bool d);
637
650
661 virtual void SetDisableAsyncMessageIO(bool d);
662
663 template <typename T, typename F>
664 boost::signals2::connection AddCloseListener(const RR_SHARED_PTR<T>& t, const F& f)
665 {
666 boost::mutex::scoped_lock lock(closed_lock);
667 if (closed)
668 {
669 lock.unlock();
670 boost::bind(f, t)();
671 return boost::signals2::connection();
672 }
673
674 return close_signal.connect(boost::signals2::signal<void()>::slot_type(boost::bind(f, t.get())).track(t));
675 }
676
677 protected:
678 RR_OVIRTUAL void LocalNodeServicesChanged() RR_OVERRIDE;
679
680 boost::mutex node_discovery_lock;
681 RR_SHARED_PTR<void> node_discovery;
682
683 std::vector<RR_SHARED_PTR<detail::TcpSocketAcceptor> > acceptors;
684 boost::mutex acceptor_lock;
685
686 static void handle_accept(const RR_SHARED_PTR<TcpTransport>& parent,
687 const RR_SHARED_PTR<detail::TcpSocketAcceptor>& acceptor,
688 const RR_SHARED_PTR<boost::asio::ip::tcp::socket>& socket,
689 const boost::system::error_code& error);
690
691 virtual void register_transport(const RR_SHARED_PTR<ITransportConnection>& connection);
692 virtual void erase_transport(const RR_SHARED_PTR<ITransportConnection>& connection);
693 virtual void incoming_transport(const RR_SHARED_PTR<ITransportConnection>& connection);
694
695 boost::mutex parameter_lock;
696 int32_t heartbeat_period;
697 int32_t default_connect_timeout;
698 int32_t default_receive_timeout;
699 int32_t max_message_size;
700 int32_t max_connection_count;
701 bool disable_message4;
702 bool disable_string_table;
703 bool disable_async_message_io;
704
705 boost::shared_ptr<void> GetTlsContext();
706
707 boost::shared_ptr<void> tls_context;
708 boost::mutex tls_context_lock;
709 bool require_tls;
710
711 RR_SHARED_PTR<void> port_sharer_client;
712 boost::mutex port_sharer_client_lock;
713
714 bool accept_websockets;
715
716 std::vector<std::string> allowed_websocket_origins;
717
718 bool closed;
719 boost::signals2::signal<void()> close_signal;
720 boost::mutex closed_lock;
721};
722
723#ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
725using TcpTransportPtr = RR_SHARED_PTR<TcpTransport>;
726#endif
727
728} // namespace RobotRaconteur
boost::shared_ptr< TcpTransport > TcpTransportPtr
Convenience alias for TcpTransport shared_ptr.
Definition TcpTransport.h:725
IPNodeDiscoveryFlags
Configuration flags for IP network node discovery.
Definition TcpTransport.h:36
@ IPNodeDiscoveryFlags_IPV4_BROADCAST
Use IPv4 255.255.255.255 broadcast addresses.
Definition TcpTransport.h:44
@ IPNodeDiscoveryFlags_LINK_LOCAL
Use IPv6 FF02:: link local multicast addresses.
Definition TcpTransport.h:40
@ IPNodeDiscoveryFlags_NODE_LOCAL
Use IPv6 FF01:: node local multicast addresses.
Definition TcpTransport.h:38
@ IPNodeDiscoveryFlags_SITE_LOCAL
Use IPv6 FF05:: site local multicast addresses.
Definition TcpTransport.h:42
Base class for all Robot Raconteur objects.
Definition DataTypes.h:229
Base class for Robot Raconteur exceptions.
Definition Error.h:53
static boost::shared_ptr< RobotRaconteurNode > sp()
Singleton shared_ptr accessor.
Transport for Transport Control Protocol Internet Protocol (TCP/IP) networks.
Definition TcpTransport.h:132
virtual void StartServer(int32_t porte, bool localhost_only=false, boost::function< bool(const boost::asio::ip::tcp::endpoint &)> accept_filter=0)
Start the server on the specified TCP port.
virtual bool GetRequireTls()
Get if all connections require TLS.
virtual void SetMaxMessageSize(int32_t size)
Set the maximum serialized message size.
virtual std::vector< boost::asio::ip::tcp::endpoint > GetResolvedListenEndpoints()
Get the TCP endpoints the server is listening with "all" addresses resolved to specific addresses.
virtual std::vector< boost::asio::ip::tcp::endpoint > GetListenEndpoints()
Get the TCP endpoints the server is listening on.
virtual bool IsPortSharerRunning()
Check if the port sharer is running.
RR_OVIRTUAL std::vector< std::string > GetServerListenUrls() RR_OVERRIDE
Get the TCP endpoints the server is listening on as Robot Raconteur candidate URLs.
virtual bool IsTransportConnectionSecure(uint32_t endpoint)
Check if specified endpoint is using TLS for its transport.
virtual void SetDisableStringTable(bool d)
Set disable string table.
virtual void SetDefaultReceiveTimeout(int32_t milliseconds)
Set the default receive timeout.
virtual void LoadTlsNodeCertificate()
Load the TLS certificate from the system.
TcpTransport(const boost::shared_ptr< RobotRaconteurNode > &node=RobotRaconteurNode::sp())
Construct a new TcpTransport.
void DisableNodeAnnounce()
Disable node discovery announce.
virtual int32_t GetMaxMessageSize()
Get the maximum serialized message size.
virtual bool GetDisableStringTable()
Get disable string table.
virtual bool GetAcceptWebSockets()
Get if the transport will accept incoming HTTP WebSocket connections.
virtual std::string GetSecurePeerIdentity(uint32_t endpoint)
Get the identity of the peer if secured using TLS.
virtual void SetDefaultConnectTimeout(int32_t milliseconds)
Set the default connect timeout.
virtual void SetMaxConnectionCount(int32_t count)
Set the maximum number of concurrent connections.
virtual int32_t GetDefaultReceiveTimeout()
Get the default receive timeout.
int32_t GetNodeAnnouncePeriod()
Get the period between node announce.
virtual void SetDefaultHeartbeatPeriod(int32_t milliseconds)
Set the default heartbeat period.
void EnableNodeAnnounce(uint32_t flags=(IPNodeDiscoveryFlags_LINK_LOCAL))
Enable node discovery announce.
virtual void AddWebSocketAllowedOrigin(boost::string_ref origin)
Add a WebSocket allowed origin.
virtual void StartServerUsingPortSharer()
Start the server using the TCP port sharer.
void DisableNodeDiscoveryListening()
Disable node discovery listening.
virtual int32_t GetDefaultHeartbeatPeriod()
Get the default heartbeat period.
virtual void SetRequireTls(bool require_tls)
Set if all connections require TLS.
void SetNodeAnnouncePeriod(int32_t millis)
Set the period between node announce.
virtual int32_t GetMaxConnectionCount()
Get the maximum number of concurrent connections.
void EnableNodeDiscoveryListening(uint32_t flags=(IPNodeDiscoveryFlags_LINK_LOCAL))
Enable node discovery listening.
virtual bool IsSecurePeerIdentityVerified(uint32_t endpoint)
Check if specified endpoint peer is using TLS and has been verified using a certificate.
virtual void SetAcceptWebSockets(bool value)
Set if the transport will accept incoming HTTP websocket connections.
virtual bool GetDisableMessage4()
Get disable Message Format Version 4.
virtual int32_t GetDefaultConnectTimeout()
Get the default connect timeout.
virtual void SetDisableAsyncMessageIO(bool d)
Set if async message io is disabled.
virtual std::vector< std::string > GetWebSocketAllowedOrigins()
Get the currently configured WebSocket origins.
virtual void RemoveWebSocketAllowedOrigin(boost::string_ref origin)
Remove a previously added WebSocket origin.
virtual bool GetDisableAsyncMessageIO()
Get if async message io is disabled.
virtual bool IsTlsNodeCertificateLoaded()
Check if TLS certificate is loaded.
virtual void SetDisableMessage4(bool d)
Set disable Message Format Version 4.