AsyncServerSocket is a listening socket that asynchronously informs a callback whenever a new connection has been accepted.

Synopsis

Declared in <folly/io/async/AsyncServerSocket.h>

class AsyncServerSocket
    : public DelayedDestruction
    , public AsyncSocketBase

Description

Unlike most async interfaces that always invoke their callback in the same EventBase thread, AsyncServerSocket is unusual in that it can distribute the callbacks across multiple EventBase threads.

This supports a common use case for network servers to distribute incoming connections across a number of EventBase threads. (Servers typically run with one EventBase thread per CPU.)

Despite being able to invoke callbacks in multiple EventBase threads, AsyncServerSocket still has one "primary" EventBase. Operations that modify the AsyncServerSocket state may only be performed from the primary EventBase thread.

Base Classes

Name

Description

DelayedDestruction

DelayedDestruction is a helper class to ensure objects are not deleted while they still have functions executing in a higher stack frame.

AsyncSocketBase

Base interface for asynchronous sockets bound to an event base.

Types

Name

Description

AcceptCallback

Interface notified when connections are accepted or accept errors occur.

ConnectionEventCallback

A callback interface to get notified of client socket events.

Destructor

Helper class to allow DelayedDestruction classes to be used with std::shared_ptr.

DestructorGuard

Classes should create a DestructorGuard object on the stack in any function that may invoke callback functions.

IntrusivePtr

This smart pointer is a convenient way to manage a concrete DelayedDestructorBase child. It can replace the equivalent raw pointer and provide automatic memory management.

Type Aliases

Name

Description

CallbackAssignFunction

Function that assigns an accepted socket to a callback index.

IPAddressIfNamePair

Bind to the specified port for the specified addresses/if names.

UniquePtr

Owning pointer type that routes destruction through Destructor.

Member Functions

Name

Description

AsyncServerSocket [constructor]

Constructors

operator= [deleted]

Deleted copy assignment operator.

addAcceptCallback [virtual]

Add an AcceptCallback.

attachEventBase

Attach this AsyncServerSocket to its primary EventBase.

bind

bind overloads

destroy [virtual]

Destroy the socket.

detachEventBase

Detach the AsyncServerSocket from its primary EventBase.

disableTransparentTls

Do not attempt the transparent TLS handshake

getAcceptRateAdjustSpeed

Get the speed of adjusting connection accept rate.

getAccepting

Get whether or not the socket is accepting new connections

getAddress

getAddress overloads

getAddresses

Get all the local addresses to which the socket is bound.

getCloseOnExec

Get whether or not FD_CLOEXEC is enabled on the server socket.

getConnectionEventCallback

Get the ConnectionEventCallback

getDestroyPending

Returns whether destruction has been requested but deferred.

getEventBase [virtual]

Get the EventBase used by this socket.

getIPFreebindEnabled

Get whether or not IP_FREEBIND is enabled on the server socket.

getKeepAliveEnabled

Get whether or not SO_KEEPALIVE is enabled on the server socket.

getListenerTos

Get default TOS for listener socket

getLocalCallbackIndex

Index of the callback with the same EVB as the current AsyncServerSocket instance, if any

getMaxAcceptAtOnce

Get the maximum number of connections that will be accepted each time around the event loop.

getMaxNumMessagesInQueue

Get the maximum number of unprocessed messages which a NotificationQueue can hold.

getNetworkSocket

Backwards compatible getSocket

getNetworkSockets

Return the underlying file descriptor

getNumDroppedConnections

Get the number of connections dropped by the AsyncServerSocket

getNumPendingMessagesInQueue

Get the current number of unprocessed messages in NotificationQueue.

getQueueTimeout

Get the duration after which new connection messages will be dropped from the NotificationQueue if it has not started processing yet.

getReusePortEnabled_

Get whether or not SO_REUSEPORT is enabled on the server socket.

getTosReflect

Get TOS reflection for server socket

listen [virtual]

Begin listening for connections.

pauseAccepting

Pause accepting connections.

removeAcceptCallback

Remove an AcceptCallback.

setAcceptRateAdjustSpeed

Set the speed of adjusting connection accept rate.

setCallbackAssignFunction

sets the callback assign function

setCloseOnExec

Set whether or not the socket should close during exec() (FD_CLOEXEC). By default, this is enabled

setConnectionEventCallback

Set the ConnectionEventCallback

setEnableReuseAddr

Set whether or not SO_REUSEADDR should be enabled on the server socket, allowing multiple sockets binds to the same <address>:<port> It's enabled by default.

setIPFreebind

Set whether or not IP_FREEBIND is enabled on the server socket. Only supported on Linux.

setKeepAliveEnabled

Set whether or not SO_KEEPALIVE should be enabled on the server socket (and thus on all subsequently‐accepted connections). By default, keepalive is enabled.

setListenerTos

Set default TOS for listener socket

setMaxAcceptAtOnce

Set the maximum number of connections that will be accepted each time around the event loop.

setMaxNumMessagesInQueue

Set the maximum number of unprocessed messages in NotificationQueue. No new message will be sent to that NotificationQueue if there are more than such number of unprocessed messages in that queue.

setQueueTimeout

setQueueTimeout overloads

setReusePortEnabled

Set whether or not SO_REUSEPORT should be enabled on the server socket, allowing multiple binds to the same port

setShutdownSocketSet

Sets the ShutdownSocketSet that tracks this socket's file descriptors.

setTFOEnabled

Tries to enable TFO if the machine supports it.

setTosReflect

Enable/Disable TOS reflection for the server socket

setZeroCopy

enable zerocopy support for the server sockets ‐ the s = accept sockets inherit it

startAccepting [virtual]

Begin accepting connctions on this socket.

stopAccepting

Shutdown the listen socket and notify all callbacks that accept has stopped

useExistingSocket

Create a AsyncServerSocket from an existing socket file descriptor.

useExistingSockets

Create a AsyncServerSocket from existing socket file descriptors.

Static Member Functions

Name

Description

newSocket

Helper function to create a shared_ptr<AsyncServerSocket>.

Static Data Members

Name

Description

kDefaultCallbackAcceptAtOnce

Default maximum connections a single callback accepts at once.

kDefaultMaxAcceptAtOnce

Default maximum connections accepted per event loop iteration.

kDefaultMaxMessagesInQueue

Default maximum number of unprocessed messages in the queue.

Protected Member Functions

Name

Description

~AsyncServerSocket [destructor] [virtual]

Protected destructor.

getDestructorGuardCount

Get the number of DestructorGuards currently protecting this object.

onDelayedDestroy [virtual]

Implement onDelayedDestroy in subclasses. onDelayedDestroy() is invoked when the object is potentially being destroyed.

Created with MrDocs