boost::corosio::tcp_acceptor

An asynchronous TCP acceptor for coroutine I/O.

Synopsis

class tcp_acceptor
    : public io_object

Description

This class provides asynchronous TCP accept operations that return awaitable types. The acceptor binds to a local endpoint and listens for incoming connections.

Each accept operation participates in the affine awaitable protocol, ensuring coroutines resume on the correct executor.

Thread Safety

Distinct objects: Safe. Shared objects: Unsafe. An acceptor must not have concurrent accept operations.

Semantics

Wraps the platform TCP listener. Operations dispatch to OS accept APIs via the io_context reactor.

Example

// Convenience constructor: open + SO_REUSEADDR + bind + listen
io_context ioc;
tcp_acceptor acc( ioc, endpoint( 8080 ) );

tcp_socket peer( ioc );
auto [ec] = co_await acc.accept( peer );
if ( !ec ) {
    // peer is now a connected socket
    auto [ec2, n] = co_await peer.read_some( buf );
}

Example

// Fine-grained setup
tcp_acceptor acc( ioc );
acc.open( tcp::v6() );
acc.set_option( socket_option::reuse_address( true ) );
acc.set_option( socket_option::v6_only( true ) );
if ( auto ec = acc.bind( endpoint( ipv6_address::any(), 8080 ) ) )
    return ec;
if ( auto ec = acc.listen() )
    return ec;

Base Classes

Name

Description

io_object

Base class for platform I/O objects.

Types

Name

Description

handle

RAII wrapper for I/O object implementation lifetime.

implementation

Define backend hooks for TCP acceptor operations.

io_service

Service interface for I/O object lifecycle management.

Member Functions

Name

Description

tcp_acceptor [constructor] [deleted]

Constructors

~tcp_acceptor [destructor] [virtual]

Destructor.

operator= [deleted]

Move assignment operator.

accept

accept overloads

bind

Bind to a local endpoint.

cancel

Cancel any pending asynchronous operations.

close

Close the acceptor.

context

Return the execution context.

get_option

Get a socket option from the acceptor.

is_open

Check if the acceptor is listening.

listen

Start listening for incoming connections.

local_endpoint

Get the local endpoint of the acceptor.

open

Create the acceptor socket without binding or listening.

set_option

Set a socket option on the acceptor.

wait

Wait for an incoming connection or readiness condition.

Protected Member Functions

Name

Description

tcp_acceptor [constructor]

Construct from handle

operator= [deleted]

Move assign from another I/O object.

Protected Static Member Functions

Name

Description

create_handle

Create a handle bound to a service found in the context.

reset_peer_impl

Transfer accepted peer impl to the peer socket.

Protected Data Members

Name

Description

h_

The platform I/O handle owned by this object.

Derived Classes

Name

Description

native_tcp_acceptor

An asynchronous TCP acceptor with devirtualized accept operations.

Created with MrDocs