eve.aio.windows.tcp

Async TCP listener and connection primitives for Windows.

This module provides the Windows implementation of TCP networking primitives using Winsock2 API.

Types 14

Connection-side buffer configuration.

Fields
size_t highWaterMarkMaximum queued bytes before `send` starts reporting pressure.
size_t lowWaterMarkQueue threshold below which `onWritable` may fire again.
Methods
bool isValid() @property const pure @safe nothrow @nogcValidate the config.

Listener-side socket configuration.

Fields
int backlogPending accept backlog passed to `listen`.
Methods
bool isValid() @property const pure @safe nothrow @nogcValidate the config.
private aliasConnectCallback = void delegate(ref TcpConnection connection) @safe
private aliasDataCallback = void delegate(ref TcpConnection connection, scope const(ubyte)[] data) @safe
private aliasWritableCallback = void delegate(ref TcpConnection connection) @safe
private aliasCloseCallback = void delegate(ref TcpConnection connection, SocketCloseReason reason) @safe
private aliasErrorCallback = void delegate(ref TcpConnection connection, int errorNumber) @safe
private aliasAcceptCallback = void delegate(ref TcpListener listener, Handle clientHandle) @safe
private aliasConnectionCallback = void delegate(ref TcpListener listener, ref TcpConnection connection) @safe

Async TCP connection wrapper.

Wraps a non-blocking TCP socket and integrates with the Layer 1 event loop for async read/write operations with backpressure support.

Fields
Methods
TcpConnection create(TcpConnectionConfig config = TcpConnectionConfig.init) @trusted nothrowCreate a detached connection wrapper.
void onConnect(ConnectCallback callback) @property @trustedSet the connect callback.
void onData(DataCallback callback) @property @trustedSet the inbound data callback.
void onWritable(WritableCallback callback) @property @trustedSet the writable callback.
void onClose(CloseCallback callback) @property @trustedSet the close callback.
void onError(ErrorCallback callback) @property @trustedSet the error callback.
bool isOpen() @property const @safe nothrow @nogcReport whether the wrapper currently owns an open socket.
SocketState state() @property const @safe nothrow @nogcReport the current connection state.
size_t sendQueueLen() @property const @safe nothrow @nogcReport the current queued send size.
bool isWritable() @property const @safe nothrow @nogcReport whether the connection is currently writable from the caller's perspective.
IpAddress remoteAddress() @property const @safe nothrow @nogcReport the remote address of the connection.
IpAddress localAddress() @property const @safe nothrow @nogcReport the local address of the connection.
AdoptResult adopt(ref EventLoop loop, Handle clientHandle) @trusted nothrowAdopt an already-accepted client socket.
ConnectResult connect(ref EventLoop loop, scope const(char)[] host, ushort port, CancelToken cancel = CancelToken .invalid) @trusted nothrowStart a non-blocking connection attempt.
SendResult send(scope const(ubyte)[] data) @trustedSend bytes over the connection.
void pauseReading() @trusted nothrowPause socket read delivery.
void resumeReading() @trusted nothrowResume socket read delivery.
void close() @trusted nothrowClose the connection.
void dispose() @trusted nothrowDispose the connection and its watcher registration.
bool track(Coord)(ref Coord coordinator) @trusted nothrowTrack this connection for graceful shutdown.
TcpConnectionState mutableState() @trusted nothrow

Async TCP listener wrapper.

Wraps a non-blocking TCP listening socket and integrates with the Layer 1 event loop for async accept operations.

Fields
Methods
TcpListener create(TcpListenerConfig config = TcpListenerConfig.init) @trustedCreate a detached listener wrapper.
void onAccept(AcceptCallback callback) @property @trustedSet the accept callback.
void onConnection(ConnectionCallback callback) @property @trustedSet the high-level connection callback.
bool isOpen() @property const @safe nothrow @nogcReport whether the listener currently owns an open socket.
ushort localPort() @property const @safe nothrow @nogcReport the effective local port once listening.
ListenResult listen(ref EventLoop loop, scope const(char)[] host, ushort port) @trustedStart listening on the requested endpoint.
void close() @trusted nothrowClose the listener.
void dispose() @trusted nothrowDispose the listener and its watcher registration.
bool track(Coord)(ref Coord coordinator) @trusted nothrowTrack this listener for graceful shutdown.
TcpListenerState mutableState() @trusted nothrow
Fields
ConnectCallback onConnect
WritableCallback onWritable
EventLoop * _loop
SOCKET _socket
Token _token
bool _readingPaused
bool _watchingWrite
bool _awaitingWritable
bool _closeDelivered
ubyte[] _sendQueue
int _lastError
IpAddress _remoteAddress
IpAddress _localAddress
CancelToken _cancelToken
CancelToken _shutdownToken
bool _draining
bool _useOverlappedIoWhether this connection uses overlapped I/O mode.
Token _prepareTokenPrepare token for polling IOCP completion status.
OVERLAPPED _readOverlappedOverlapped structure for pending read operations.
OVERLAPPED _writeOverlappedOverlapped structure for pending write operations.
ubyte[TCP_READ_BUFFER_SIZE] _readBufferRead buffer — must remain valid during overlapped read.
ubyte[TCP_READ_BUFFER_SIZE] _writeBufferWrite buffer — must remain valid during overlapped write.
bool _readInProgressWhether a read operation is currently in progress.
bool _writeInProgressWhether a write operation is currently in progress.
size_t _pendingWriteLenNumber of bytes in the current pending write.
ConnectExFunc _connectExConnectEx function pointer (loaded on first use).
OVERLAPPED _connectOverlappedOverlapped structure for pending connect operation.
bool _connectInProgressWhether a connect operation is currently in progress.
ubyte[] _pausedReceiveBufferBuffer for data received while reading is paused (IOCP mode only). When paused, incoming data is stored here instead of being delivered.
Methods
void initiateShutdown(CancelToken token) @safe nothrow
void performDrain() @safe nothrow
void forceAbort() @safe nothrow
bool isClosed() @property const @safe nothrow @nogc
bool isOpen() @property const @safe nothrow @nogc
size_t sendQueueLen() @property const @safe nothrow @nogc
bool isWritable() @property const @safe nothrow @nogc
IpAddress remoteAddress() @property const @safe nothrow @nogc
IpAddress localAddress() @property const @safe nothrow @nogc
AdoptResult adopt(ref EventLoop loop, Handle clientHandle) @trusted nothrow
ConnectResult connect(ref EventLoop loop, scope const(char)[] host, ushort port, CancelToken cancel = CancelToken .invalid) @trusted nothrow
private int bindToAnyAddress(SOCKET sock, int family) @trusted nothrowBind socket to any available local address.
private int submitConnectEx(const(sockaddr) * addr, int addrlen) @trusted nothrowSubmit a ConnectEx operation.
SendResult send(scope const(ubyte)[] data) @trusted nothrowSend data on the connection.
void pauseReading() @trusted nothrow
void resumeReading() @trusted nothrow
void closeExplicitly() @trusted nothrow
void handleIo(ref EventLoop loop, Token token, IoReady ready) @safe nothrow
TcpConnection owner() @safe nothrow
void finishConnect() @trusted nothrow
void readAvailable() @trusted nothrow
void flushSendQueue() @trusted nothrow
int refreshInterest() @trusted nothrow
void fail(int errorNumber) @trusted nothrow
void closeInternal(SocketCloseReason reason, int errorNumber, bool invokeCallback) @trusted nothrow
void closeWithoutCallback() @trusted nothrow
void invokeConnect() @trusted nothrow
void invokeData(scope const(ubyte)[] data) @trusted nothrow
void invokeWritable() @trusted nothrow
void invokeError(int errorNumber) @trusted nothrow
void invokeClose(SocketCloseReason reason) @trusted nothrow
private IpAddress toIpAddress(const sockaddr * addr) @trusted nothrow @nogc
void handlePrepare(ref EventLoop loop, Token token) @trusted nothrowHandle prepare phase callback from the event loop.
void processConnectCompletion() @trusted nothrowProcess a completed ConnectEx operation.
int associateWithIocp() @trusted nothrowAssociate the socket with the event loop's IOCP.
void submitRecv() @trusted nothrowSubmit an overlapped receive operation.
void processRecvCompletion() @trusted nothrowProcess a completed receive operation.
void submitSend() @trusted nothrowSubmit an overlapped send operation.
void processSendCompletion() @trusted nothrowProcess a completed send operation.
Constructors
Fields
ConnectionCallback onConnection
EventLoop * _loop
SOCKET _socket
Token _token
ushort _localPort
int _lastError
CancelToken _shutdownToken
bool _draining
bool _useOverlappedIoWhether this listener uses overlapped I/O mode (AcceptEx).
Token _prepareTokenPrepare token for polling IOCP completion status.
AcceptExFunc _acceptExAcceptEx function pointer.
GetAcceptExSockaddrsFunc _getAcceptExSockaddrsGetAcceptExSockaddrs function pointer.
int _addressFamilyAddress family of the listen socket (AFINET or AFINET6).
SOCKET _acceptSocketPre-created socket for the pending accept operation.
OVERLAPPED _acceptOverlappedOverlapped structure for pending accept operation.
ubyte[ACCEPT_BUFFER_SIZE] _acceptBufferBuffer for AcceptEx output (local + remote addresses).
bool _acceptInProgressWhether an accept operation is currently in progress.
Methods
void initiateShutdown(CancelToken token) @safe nothrow
void performDrain() @safe nothrow
void forceAbort() @safe nothrow
bool isClosed() @property const @safe nothrow @nogc
bool isOpen() @property const @safe nothrow @nogc
ListenResult listen(ref EventLoop loop, scope const(char)[] host, ushort port) @trusted nothrow
void close() @trusted nothrow
void handleIo(ref EventLoop loop, Token token, IoReady ready) @safe nothrow
TcpListener owner() @safe nothrow
void acceptAvailable() @trusted nothrow
void invokeAccept(Handle clientHandle) @trusted nothrow
void handlePrepare(ref EventLoop loop, Token token) @trusted nothrowHandle prepare phase callback from the event loop.
int associateWithIocp() @trusted nothrowAssociate the listen socket with the event loop's IOCP.
void submitAccept() @trusted nothrowSubmit an AcceptEx operation.
void processAcceptCompletion() @trusted nothrowProcess a completed AcceptEx operation.
Constructors
private structAddrinfoRange

Range adapter for iterating over addrinfo linked list.

Fields
addrinfo * current
Methods
bool empty() @property const pure @safe nothrow @nogcCheck if the range is exhausted.
addrinfo * front() @property pure @safe nothrow @nogcGet the current addrinfo entry.
void popFront() pure @safe nothrow @nogcAdvance to the next entry.

Functions 13

private fnListenResult mapListenError(int err) pure @safe nothrow @nogcMap Winsock error code to ListenResult.
private fnConnectResult mapConnectError(int err) pure @safe nothrow @nogcMap Winsock error code to ConnectResult.
private fnAdoptResult mapAdoptError(int err) pure @safe nothrow @nogcMap Winsock error code to AdoptResult.
private fnTcpConnectionConfig validated(TcpConnectionConfig config) pure @safe nothrow @nogcValidate a connection config, returning defaults if invalid.
private fnTcpListenerConfig validated(TcpListenerConfig config) pure @safe nothrow @nogcValidate a listener config, returning defaults if invalid.
private fnconst(char) * copyStringz(scope const(char)[] source, scope char[] buffer) @trusted nothrowCopy a D string slice to a null-terminated C string buffer.
private fnconst(char) * portToString(ushort port, scope char[] buffer) @trusted nothrowConvert a port number to a null-terminated string.
private fnint setNonBlocking(SOCKET sock) @trusted nothrowSet a socket to non-blocking mode.
private fnbool wouldBlock() @trusted nothrowCheck if the last Winsock error indicates the operation would block.
private fnint socketError(SOCKET sock) @trusted nothrowQuery the socket error status.
private fnint mapWsaError(int wsaError) pure @safe nothrow @nogcMap Winsock error code to POSIX-style errno.
private fnushort queryLocalPort(SOCKET sock) @trusted nothrowQuery the local port number for a bound socket.
private fnAddrinfoRange addrinfoRange(addrinfo * first) pure @safe nothrow @nogcCreate a range over an addrinfo linked list.

Variables 4

private enumvarSOMAXCONN = 0x7fffffff

Maximum listen backlog (not in druntime's winsock2).

private enumvarTCP_READ_BUFFER_SIZE = 8192

Size of the internal read/write buffer for overlapped I/O.

private enumvarACCEPT_ADDR_LEN = SOCKADDR_STORAGE.sizeof + 16

Size of address buffer for AcceptEx (sockaddr_storage + 16 bytes padding).

private enumvarACCEPT_BUFFER_SIZE = ACCEPT_ADDR_LEN * 2

Total buffer size for AcceptEx (local + remote addresses).