12 #ifndef ROC_NETIO_TCP_CONNECTION_PORT_H_
13 #define ROC_NETIO_TCP_CONNECTION_PORT_H_
209 enum ConnectionState {
264 uint64_t rd_wouldblock;
265 uint64_t wr_wouldblock;
283 static const char* conn_state_to_str_(ConnectionState);
285 static void poll_cb_(uv_poll_t* handle,
int status,
int events);
286 static void start_terminate_cb_(uv_async_t* handle);
287 static void finish_terminate_cb_(uv_handle_t* handle);
288 static void close_cb_(uv_handle_t* handle);
290 bool start_polling_();
293 void disconnect_socket_();
297 void set_and_report_writable_();
298 void set_and_report_readable_();
300 ConnectionState get_state_()
const;
301 void switch_and_report_state_(ConnectionState new_state);
302 bool maybe_switch_state_(ConnectionState expected_state,
303 ConnectionState desired_state);
304 void report_state_(ConnectionState state);
306 void set_conn_handler_(IConnHandler& handler);
307 void unset_conn_handler_();
309 void check_usable_(ConnectionState conn_state)
const;
310 void check_usable_for_io_(ConnectionState conn_state)
const;
312 void report_io_stats_();
316 uv_poll_t poll_handle_;
317 bool poll_handle_initialized_;
318 bool poll_handle_started_;
320 uv_async_t terminate_sem_;
321 bool terminate_sem_initialized_;
323 core::SharedPtr<IConnHandler> conn_handler_;
325 ITerminateHandler* terminate_handler_;
326 void* terminate_handler_arg_;
328 ICloseHandler* close_handler_;
329 void* close_handler_arg_;
333 address::SocketAddr local_address_;
334 address::SocketAddr remote_address_;
338 core::Atomic<int32_t> conn_state_;
340 core::Atomic<int32_t> conn_was_established_;
341 core::Atomic<int32_t> conn_was_failed_;
343 core::Atomic<int32_t> writable_status_;
344 core::Atomic<int32_t> readable_status_;
346 bool got_stream_end_;
348 core::Mutex io_mutex_;
351 core::RateLimiter report_limiter_;
IArena & arena() const
Get arena.
Connection event handler interface.
Termination handler interface.
void attach_connection_handler(IConnHandler &handler)
Set connection handler and start reporting events to it.
virtual ssize_t try_read(void *buf, size_t len)
Read len bytes from the the connection to buf.
bool connect(const TcpClientConfig &config)
Establish connection to remote peer (asynchronously).
virtual bool is_failed() const
Return true if there was a failure.
virtual AsyncOperationStatus async_close(ICloseHandler &handler, void *handler_arg)
Asynchronously close TCP connection.
virtual const address::SocketAddr & local_address() const
Return address of the local peer.
virtual bool open()
Open TCP connection.
TcpConnectionPort(TcpConnectionType type, uv_loop_t &loop, core::IArena &arena)
Initialize.
virtual bool is_readable() const
Return true if the connection is readable.
bool accept(const TcpConnectionConfig &config, const address::SocketAddr &server_address, SocketHandle server_socket)
Establish conection by accepting it from listening socket.
virtual ~TcpConnectionPort()
Destroy.
virtual void async_terminate(TerminationMode mode)
Initiate asynchronous graceful shutdown.
virtual void format_descriptor(core::StringBuilder &b)
Format descriptor.
virtual const address::SocketAddr & remote_address() const
Return address of the remote peer.
virtual bool is_writable() const
Return true if the connection is writable.
void attach_terminate_handler(ITerminateHandler &handler, void *handler_arg)
Set termination handler and start using it.
virtual ssize_t try_write(const void *buf, size_t len)
Write buf of size len to the connection.
Connection event handler interface.
Termination handler interface.
int SocketHandle
Platform-specific socket handle.
AsyncOperationStatus
Asynchronous operation status.
TcpConnectionType
TCP connection type.
@ TcpConn_Client
Local peer is client, remote peer is server.
@ TcpConn_Server
Local peer is server, remote peer is client.
TerminationMode
Connection termination mode.
Shared ownership intrusive pointer.
TCP connection parameters.
address::SocketAddr local_address
Local peer address to which we're bound.
address::SocketAddr remote_address
Remote peer address to which we're connected.
TCP connection parameters.
SocketOpts socket_options
Socket options.