OpenDDS  Snapshot(2023/04/28-20:55)
TcpConnection.h
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #ifndef OPENDDS_DCPS_TRANSPORT_TCP_TCPCONNECTION_H
9 #define OPENDDS_DCPS_TRANSPORT_TCP_TCPCONNECTION_H
10 
11 #include "TcpInst_rch.h"
12 #ifdef __BORLANDC__
13 # include "TcpDataLink.h"
14 #endif
15 #include "TcpDataLink_rch.h"
16 #include "TcpConnection_rch.h"
17 #include "TcpSendStrategy_rch.h"
18 #include "TcpReceiveStrategy_rch.h"
19 #include "TcpTransport_rch.h"
20 
21 #include "dds/DCPS/RcObject.h"
22 #include "dds/DCPS/PoolAllocator.h"
23 #include "dds/DCPS/ReactorTask.h"
25 #include "dds/DCPS/TimeTypes.h"
26 
27 #include "ace/SOCK_Stream.h"
28 #include "ace/Svc_Handler.h"
29 #include "ace/INET_Addr.h"
30 #include "ace/Synch_Traits.h"
31 
33 
34 namespace OpenDDS {
35 namespace DCPS {
36 
38  : public virtual RcObject
39  , public ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> {
40 public:
41 
42  /// States are used during reconnecting.
51  };
52 
53  /// Passive side constructor (acceptor)
54  TcpConnection();
55 
56  /// Active side constructor (connector)
57  TcpConnection(const ACE_INET_Addr& remote_address,
59  const TcpInst_rch& config);
60 
61  virtual ~TcpConnection();
62 
63  std::size_t& id();
64 
65  /// Protocol setup (handshake) on the active side.
66  /// The local address is sent to the remote (passive) side to
67  /// identify ourselves to the remote side.
68  int active_open();
70 
71  int passive_open(void*);
72 
73  /// This will be called by the DataLink (that "owns" us) when
74  /// the TcpTransport has been told to shutdown(), or when
75  /// the DataLink finds itself no longer needed, and is
76  /// "self-releasing".
77  void disconnect();
78 
79  // Note that the acceptor or connector that calls the open() method will pass
80  // itself in as a void*.
81  virtual int open(void* arg);
82 
85 
86  /// We pass this "event" along to the receive_strategy.
87  virtual int handle_input(ACE_HANDLE);
88 
89  /// Handle back pressure when sending.
90  virtual int handle_output(ACE_HANDLE);
91 
92  virtual int close(u_long);
93  virtual int handle_close(ACE_HANDLE, ACE_Reactor_Mask);
94 
95  void set_sock_options(const TcpInst_rch& tcp_config);
96 
97  /// Return true if the object represents the connector side, otherwise
98  /// it's the acceptor side. The acceptor/connector role is not changed
99  /// when re-establishing the connection.
100  bool is_connector() const;
101 
102  void transfer(TcpConnection* connection);
103 
104  int handle_timeout(const ACE_Time_Value &tv, const void *arg);
105 
106  /// Cache the reference to the datalink object for lost connection
107  /// callbacks.
108  void set_datalink(const TcpDataLink_rch& link);
109 
111 
113 
114  /// Reconnect initiated by send strategy
115  void relink_from_send(bool do_suspend);
116 
117  /// Reconnect initiated by receive strategy
118  void relink_from_recv(bool do_suspend);
119 
120  /// Called by the reconnect task to inform us that the
121  /// link & any associated data can be torn down.
122  /// This call is done with no DCPS/transport locks held.
123  void tear_link();
124 
125  void shutdown();
126 
128 
129  /// Access TRANSPORT_PRIORITY.value policy value if set.
132 
135 
137 
138 private:
139 
140  /// Handle the logic after an active connection has been established
142 
143  void active_reconnect_i();
144  void passive_reconnect_i();
145 
146  void notify_connection_lost();
148 
149  /// During the connection setup phase, the passive side sets passive_setup_,
150  /// redirecting handle_input() events here (there is no recv strategy yet).
151  int handle_setup_input(ACE_HANDLE h);
152 
153  const std::string& config_name() const;
154 
157 
158  /// Lock to synchronize state between reactor and non-reactor threads.
159  LockType reconnect_lock_;
160 
161  /// Flag indicate this connection object is the connector or acceptor.
163 
164  /// Remote address.
166 
167  /// Local address.
169 
170  /// The configuration used by this connection.
172 
173  /// Datalink object which is needed for connection lost callback.
175 
176  /// Impl object which is needed for connection objects and reconnect task
178 
179  /// The state indicates each step of the reconnecting.
181 
182  /// TRANSPORT_PRIORITY.value policy value.
184 
185  /// shutdown flag
186  bool shutdown_;
187 
191 
192  /// Small unique identifying value.
193  std::size_t id_;
195 
196  /// Get name of the current reconnect state as a string.
197  const char* reconnect_state_string() const;
198 };
199 
200 } // namespace DCPS
201 } // namespace OpenDDS
202 
204 
205 #if defined (__ACE_INLINE__)
206 #include "TcpConnection.inl"
207 #endif /* __ACE_INLINE__ */
208 
209 #endif /* OPENDDS_TCPCONNECTION_H */
#define ACE_SYNCH_MUTEX
unsigned long ACE_Reactor_Mask
TcpDataLink_rch link_
Datalink object which is needed for connection lost callback.
LockType reconnect_lock_
Lock to synchronize state between reactor and non-reactor threads.
bool shutdown_
shutdown flag
std::size_t id_
Small unique identifying value.
ACE_Guard< LockType > GuardType
virtual ACE_Event_Handler::Reference_Count add_reference()
TcpReceiveStrategy_rch receive_strategy()
ACE_Message_Block passive_setup_buffer_
const std::string & config_name() const
Priority transport_priority_
TRANSPORT_PRIORITY.value policy value.
TcpConnection()
Passive side constructor (acceptor)
virtual int open(void *arg)
TcpTransport * transport_during_setup_
ReconnectState
States are used during reconnecting.
Definition: TcpConnection.h:43
Priority & transport_priority()
Access TRANSPORT_PRIORITY.value policy value if set.
virtual int handle_close(ACE_HANDLE, ACE_Reactor_Mask)
ACE_INET_Addr get_remote_address()
int on_active_connection_established()
Handle the logic after an active connection has been established.
virtual ACE_Event_Handler::Reference_Count remove_reference()
virtual int handle_output(ACE_HANDLE)
Handle back pressure when sending.
TcpTransport_rch impl_
Impl object which is needed for connection objects and reconnect task.
virtual int priority(void) const
void transfer(TcpConnection *connection)
void relink_from_send(bool do_suspend)
Reconnect initiated by send strategy.
ACE_CDR::Long Priority
WeakRcHandle< TcpInst > tcp_config_
The configuration used by this connection.
#define OPENDDS_POOL_ALLOCATION_FWD
int handle_timeout(const ACE_Time_Value &tv, const void *arg)
ACE_INET_Addr remote_address_
Remote address.
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
ReconnectState reconnect_state_
The state indicates each step of the reconnecting.
void relink_from_recv(bool do_suspend)
Reconnect initiated by receive strategy.
int handle_setup_input(ACE_HANDLE h)
TcpSendStrategy_rch send_strategy()
const char * reconnect_state_string() const
Get name of the current reconnect state as a string.
void set_sock_options(const TcpInst_rch &tcp_config)
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
virtual int handle_input(ACE_HANDLE)
We pass this "event" along to the receive_strategy.
bool is_connector_
Flag indicate this connection object is the connector or acceptor.
ACE_INET_Addr local_address_
Local address.
void set_datalink(const TcpDataLink_rch &link)