Robot Raconteur Core C++ Library
TcpTransport.h
Go to the documentation of this file.
1 
25 #include <boost/shared_array.hpp>
26 
27 #pragma once
28 
29 namespace RobotRaconteur
30 {
36 {
45 };
46 
47 class TcpTransportConnection;
48 namespace detail
49 {
50 class TcpConnector;
51 class TcpWebSocketConnector;
52 class TcpWSSWebSocketConnector;
53 class TcpSocketAcceptor;
54 } // namespace detail
55 
131 class 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 
260  virtual void StartServerUsingPortSharer();
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 
315  void EnableNodeDiscoveryListening(uint32_t flags = (IPNodeDiscoveryFlags_LINK_LOCAL));
316 
321  void DisableNodeDiscoveryListening();
322 
330  void EnableNodeAnnounce(uint32_t flags = (IPNodeDiscoveryFlags_LINK_LOCAL));
331 
336  void DisableNodeAnnounce();
337 
345  int32_t GetNodeAnnouncePeriod();
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 
446  virtual void LoadTlsNodeCertificate();
447 
454  virtual bool IsTlsNodeCertificateLoaded();
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 
583  virtual void AddWebSocketAllowedOrigin(boost::string_ref origin);
584 
592  virtual void RemoveWebSocketAllowedOrigin(boost::string_ref origin);
593 
604  virtual bool GetDisableMessage4();
605 
615  virtual void SetDisableMessage4(bool d);
616 
628  virtual bool GetDisableStringTable();
629 
640  virtual void SetDisableStringTable(bool d);
641 
653  virtual bool GetDisableAsyncMessageIO();
654 
665  virtual void SetDisableAsyncMessageIO(bool d);
666 
667  template <typename T, typename F>
668  boost::signals2::connection AddCloseListener(const RR_SHARED_PTR<T>& t, const F& f)
669  {
670  boost::mutex::scoped_lock lock(closed_lock);
671  if (closed)
672  {
673  lock.unlock();
674  boost::bind(f, t)();
675  return boost::signals2::connection();
676  }
677 
678  return close_signal.connect(boost::signals2::signal<void()>::slot_type(boost::bind(f, t.get())).track(t));
679  }
680 
681  protected:
682  RR_OVIRTUAL void LocalNodeServicesChanged() RR_OVERRIDE;
683 
684  boost::mutex node_discovery_lock;
685  RR_SHARED_PTR<void> node_discovery;
686 
687  std::vector<RR_SHARED_PTR<detail::TcpSocketAcceptor> > acceptors;
688  boost::mutex acceptor_lock;
689 
690  static void handle_accept(const RR_SHARED_PTR<TcpTransport>& parent,
691  const RR_SHARED_PTR<detail::TcpSocketAcceptor>& acceptor,
692  const RR_SHARED_PTR<boost::asio::ip::tcp::socket>& socket,
693  const boost::system::error_code& error);
694 
695  virtual void register_transport(const RR_SHARED_PTR<ITransportConnection>& connection);
696  virtual void erase_transport(const RR_SHARED_PTR<ITransportConnection>& connection);
697  virtual void incoming_transport(const RR_SHARED_PTR<ITransportConnection>& connection);
698 
699  boost::mutex parameter_lock;
700  int32_t heartbeat_period;
701  int32_t default_connect_timeout;
702  int32_t default_receive_timeout;
703  int32_t max_message_size;
704  int32_t max_connection_count;
705  bool disable_message4;
706  bool disable_string_table;
707  bool disable_async_message_io;
708 
709  boost::shared_ptr<void> GetTlsContext();
710 
711  boost::shared_ptr<void> tls_context;
712  boost::mutex tls_context_lock;
713  bool require_tls;
714 
715  RR_SHARED_PTR<void> port_sharer_client;
716  boost::mutex port_sharer_client_lock;
717 
718  bool accept_websockets;
719 
720  std::vector<std::string> allowed_websocket_origins;
721 
722  bool closed;
723  boost::signals2::signal<void()> close_signal;
724  boost::mutex closed_lock;
725 };
726 
727 #ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
729 using TcpTransportPtr = RR_SHARED_PTR<TcpTransport>;
730 #endif
731 
732 } // namespace RobotRaconteur
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
boost::shared_ptr< TcpTransport > TcpTransportPtr
Convenience alias for TcpTransport shared_ptr.
Definition: TcpTransport.h:729
Base class for all Robot Raconteur objects.
Definition: DataTypes.h:227
Base class for Robot Raconteur exceptions.
Definition: Error.h:50
static boost::shared_ptr< RobotRaconteurNode > sp()
Singleton shared_ptr accessor.
Transport for Transport Control Protocol Internet Protocol (TCP/IP) networks.
Definition: TcpTransport.h:132
TcpTransport(const boost::shared_ptr< RobotRaconteurNode > &node=RobotRaconteurNode::sp())
Construct a new TcpTransport.
Base class for transports.
Definition: Transport.h:120