re-arm, close
Feature notes: TLS implementation notes (RFC map) · Async TLS socket server architecture
inherits from Exception
(no public members)
(no public members)
Data: PByte;
Len: Integer;
inherits from TInterfacedObject
Abstract per-connection object the reactor drives. Reference-counted (tInterfacedObject) so an in-flight overlapped op can pin it alive through an IInterface anchor held in its I/O context. The reactor does NOT own the connection's lifetime: the caller (server connection table / test) must keep a reference for as long as the connection is attached.
procedure HandleReceived(aData: Pointer; aLen: Integer); virtual; abstract;
function ReceiveEnded: Boolean; virtual;
constructor Create(aReactor: tAsyncReactor; aHandle: TSocketHandle);
destructor Destroy; override;
procedure Enter;
procedure Leave;
procedure EnqueueSend(aData: Pointer; aLen: Integer);
procedure SendCompleted(aByteLen: Integer); virtual;
procedure RequestClose;
procedure CloseFromReactor;
function RequestNudge: Boolean;
function RequestWork: Boolean;
procedure SetReceivePaused(aPaused: Boolean);
procedure ResumeReceive;
property ReceivePaused: Boolean read fReceivePaused;
procedure DeliverNudge;
procedure DeliverQueuedWake;
property CloseRequested: Boolean read GetCloseRequested;
property NudgePending: Boolean read GetNudgePending;
procedure NoteRecvPosted;
procedure NoteRecvCompleted;
property Handle: TSocketHandle read fHandle;
property Reactor: tAsyncReactor read fReactor;
property RecvInFlight: Integer read fRecvInFlight;
property RecvHighWater: Integer read fRecvHighWater;
property Closed: Integer read fClosed;
function SendCredit: Integer;
property SendQueueBytes: Integer read fSendQueueBytes;
property SendInFlightBytes: Integer read fSendInFlightBytes;
property SendPendingBytes: Integer read GetSendPendingBytes;
property SendInFlight: Boolean read fSendInFlight;
property SendQueueHighWater: Integer read fSendQueueHighWater;
property MaxSendQueueBytes: Integer read fMaxSendQueueBytes write fMaxSendQueueBytes;
inherits from TInterfacedObject
A bound UDP socket the reactor drives (stSocket.OpenDatagramHandle makes one). Reference-counted for the same reason as tReactorConnection, and, like it, kept alive by its owner while attached.
RECEIVING. The reactor reads each datagram whole into its own buffer and calls HandleDatagram with it and its sender. HandleDatagram runs on an I/O thread with NO lock of this class held, and must neither keep aData nor take long: the next receive is posted after it returns.
- ReceiveDepth = 1 (the default): calls are SERIALIZED, one at a time, in the order the system delivered the datagrams.
- ReceiveDepth > 1: that many receives are kept posted, so on a completion backend (IOCP) calls may run CONCURRENTLY on several I/O threads and in any order - for a server whose handler dispatches to per-peer state with its own locks. A readiness backend (epoll, kqueue) still serializes; a handler written for > 1 is correct there. A receive that fails - an ICMP report about an earlier send, a datagram larger than the buffer, which cannot happen with the reactor's 64 KiB buffer - is counted in ReceiveErrors and the endpoint goes on receiving. An exception raised by HandleDatagram is counted in HandlerFaults and swallowed: one bad datagram from one peer must not take the socket down for the others.
SENDING. SendTo is safe from any thread, HandleDatagram included, and never waits. The datagram is handed to the system at once, or dropped and counted in SendsDropped: a full send buffer, a datagram too large for the path, an unreachable network. There is no queue - a datagram protocol recovers from loss itself, and a queue here would only be a second, unbounded, buffer behind the system's.
CLOSING. Close is safe from any thread, HandleDatagram included. The socket is closed once no system call is using it - a send or receive in progress on another thread finishes first - and DoClose runs once. A HandleDatagram already running on another thread may still be running when Close returns. An endpoint freed without Close (one never attached) closes its handle without running DoClose.
ECN. SetEcnReception(True), before AttachDatagram, has the backend read the ECN field of every datagram it receives; it then calls HandleDatagramEcn with the marking (deUnknown where the system supplied none), whose default passes the datagram on to HandleDatagram. SendToEcn and SendSegmentsToEcn send with the ECN bits set. The backends in this repository do both on Windows, Linux, Android, macOS and iOS, where the system allows it.
constructor Create(aReactor: tAsyncReactor; aHandle: TSocketHandle);
destructor Destroy; override;
procedure HandleDatagram(aData: Pointer; aLen: Integer; const aFrom: tSocketAddress); virtual; abstract;
procedure HandleDatagramEcn(aData: Pointer; aLen: Integer; const aFrom: tSocketAddress; aEcn: tDatagramEcn); virtual;
function SendTo(aData: Pointer; aLen: Integer; const aTo: tSocketAddress): Boolean;
function SendSegmentsTo(aData: Pointer; aLen, aSegmentSize: Integer; const aTo: tSocketAddress; out aError: Integer): Integer;
function SendToEcn(aData: Pointer; aLen: Integer; const aTo: tSocketAddress; aEcn: tDatagramEcn; out aError: Integer): Boolean;
function SendSegmentsToEcn(aData: Pointer; aLen, aSegmentSize: Integer; const aTo: tSocketAddress; aEcn: tDatagramEcn; out aError: Integer): Integer;
function SetEcnReception(aEnabled: Boolean; out aError: Integer): Boolean;
procedure Close;
function AcquireHandle(out aHandle: TSocketHandle): Boolean;
procedure ReleaseHandle;
procedure NoteReceived;
procedure NoteReceiveError(aError: Integer);
procedure NoteHandlerFault;
property Reactor: tAsyncReactor read fReactor;
property Handle: TSocketHandle read fHandle;
property Closed: Integer read fClosed;
property EcnReception: Boolean read fEcnReception;
property ReceiveDepth: Integer read fReceiveDepth write SetReceiveDepth;
property DatagramsReceived: Int64 read GetDatagramsReceived;
property DatagramsSent: Int64 read GetDatagramsSent;
property SendsDropped: Int64 read GetSendsDropped;
property ReceiveErrors: Int64 read GetReceiveErrors;
property HandlerFaults: Int64 read GetHandlerFaults;
property LastError: Integer read fLastError;
Abstract reactor. Concrete backends implement the platform I/O multiplexer.
constructor Create(aThreadCount: Integer); virtual;
procedure Start; virtual; abstract;
procedure Stop; virtual; abstract;
procedure CloseSocketHandle(aHandle: TSocketHandle); virtual; abstract;
procedure Attach(aConnection: tReactorConnection); virtual; abstract;
procedure Detach(aConnection: tReactorConnection); virtual; abstract;
function PostReceive(aConnection: tReactorConnection): Boolean; virtual; abstract;
function PostSend(aConnection: tReactorConnection; aData: Pointer; aLen: Integer): Boolean; virtual; abstract;
function Nudge(aConnection: tReactorConnection): Boolean; virtual;
function SupportsApplicationWork: Boolean; virtual;
function ConnectionCount: Integer; virtual; abstract;
function SnapshotConnections(out aConns: TArray<tReactorConnection>; out aHold: TArray<IInterface>): Integer; virtual; abstract;
function SupportsDatagrams: Boolean; virtual;
procedure AttachDatagram(aEndpoint: tReactorDatagramEndpoint); virtual;
procedure DetachDatagram(aEndpoint: tReactorDatagramEndpoint); virtual;
function SendDatagram(aEndpoint: tReactorDatagramEndpoint; aData: Pointer; aLen: Integer; const aTo: tSocketAddress; out aError: Integer): Boolean; virtual;
function SendDatagramSegments(aEndpoint: tReactorDatagramEndpoint; aData: Pointer; aLen, aSegmentSize: Integer; const aTo: tSocketAddress; out aError: Integer): Integer; virtual;
function SetDatagramEcnReception(aEndpoint: tReactorDatagramEndpoint; aEnabled: Boolean; out aError: Integer): Boolean; virtual;
function SendDatagramEcn(aEndpoint: tReactorDatagramEndpoint; aData: Pointer; aLen: Integer; const aTo: tSocketAddress; aEcn: tDatagramEcn; out aError: Integer): Boolean; virtual;
function SendDatagramSegmentsEcn(aEndpoint: tReactorDatagramEndpoint; aData: Pointer; aLen, aSegmentSize: Integer; const aTo: tSocketAddress; aEcn: tDatagramEcn; out aError: Integer): Integer; virtual;
function DatagramEndpointCount: Integer; virtual;
property Kind: tReactorKind read GetKind;
property ThreadCount: Integer read fThreadCount;
property EnforceSingleRecv: Boolean read fEnforceSingleRecv write fEnforceSingleRecv;
inherits from Exception
(no public members)
One queued outbound buffer awaiting its turn in a connection's send FIFO.
Constructs one backend instance. Registration rather than a hard-coded case statement, because this unit must NOT reference the backends - they reference it. Each backend registers itself from its initialization section, so simply having the unit in the program makes that backend available.