12#ifndef ROC_NETIO_TCP_CONNECTION_PORT_H_
13#define ROC_NETIO_TCP_CONNECTION_PORT_H_
210 enum ConnectionState {
265 uint64_t rd_wouldblock;
266 uint64_t wr_wouldblock;
284 static const char* conn_state_to_str_(ConnectionState);
286 static void poll_cb_(uv_poll_t* handle,
int status,
int events);
287 static void start_terminate_cb_(uv_async_t* handle);
288 static void finish_terminate_cb_(uv_handle_t* handle);
289 static void close_cb_(uv_handle_t* handle);
291 bool start_polling_();
294 void disconnect_socket_();
298 void set_and_report_writable_();
299 void set_and_report_readable_();
301 ConnectionState get_state_()
const;
302 void switch_and_report_state_(ConnectionState new_state);
303 bool maybe_switch_state_(ConnectionState expected_state,
304 ConnectionState desired_state);
305 void report_state_(ConnectionState state);
307 void set_conn_handler_(IConnHandler& handler);
308 void unset_conn_handler_();
310 void check_usable_(ConnectionState conn_state)
const;
311 void check_usable_for_io_(ConnectionState conn_state)
const;
313 void report_io_stats_();
317 uv_poll_t poll_handle_;
318 bool poll_handle_initialized_;
319 bool poll_handle_started_;
321 uv_async_t terminate_sem_;
322 bool terminate_sem_initialized_;
324 IConnHandler* conn_handler_;
326 ITerminateHandler* terminate_handler_;
327 void* terminate_handler_arg_;
329 ICloseHandler* close_handler_;
330 void* close_handler_arg_;
334 address::SocketAddr local_address_;
335 address::SocketAddr remote_address_;
339 core::Atomic<int32_t> conn_state_;
341 core::Atomic<int32_t> conn_was_established_;
342 core::Atomic<int32_t> conn_was_failed_;
344 core::Atomic<int32_t> writable_status_;
345 core::Atomic<int32_t> readable_status_;
347 bool got_stream_end_;
349 core::Mutex io_mutex_;
352 core::RateLimiter report_limiter_;
Memory allocator interface.
IAllocator & allocator() const
Get allocator.
Connection event handler interface.
Termination handler interface.
void attach_connection_handler(IConnHandler &handler)
Set connection handler and start reporting events to it.
virtual const address::SocketAddr & local_address() const
Return address of the local peer.
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.
TcpConnectionPort(TcpConnectionType type, uv_loop_t &loop, core::IAllocator &allocator)
Initialize.
virtual bool open()
Open TCP connection.
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 const address::SocketAddr & remote_address() const
Return address of the remote peer.
virtual void format_descriptor(core::StringBuilder &b)
Format descriptor.
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.
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.
SocketOptions socket_options
Socket options.