CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
NetSocket Struct Reference

#include <socket.h>

Public Member Functions

 Weak (NetQueue) *queue
 NetQueue this socket is registered to (may be NULL)
 
 atomic (uint32) state
 See NetSocketState enum.
 
 atomic (bool) canSend
 Send buffer empty; can send immediately.
 
 Weak (ObjInst) *handlerWeak
 Context passed to per-socket handlers, set by setHandlersObj() – NULL when handlerCtx is in use instead.
 
 atomic (uint32) connectGen
 Attempt generation, bumped once per connect attempt.
 

Data Fields

NetSocketType type
 
NetSockHandle handle
 Platform-neutral OS handle, set by the platform factory.
 
NetAddr local
 Local end, filled in by bind()
 
NetAddr remote
 Remote address (for connected sockets only)
 
uint32 mru
 
bool recvInfo
 The OS is reporting per-datagram IP information; see setRecvInfo()
 
NetSocketBufs bufs
 Type-specific send/receive buffers; which arm is live depends on type
 
Mutex recvLock
 Exclusive access to the socket's receive buffer.
 
Mutex sendLock
 
size_t sendHigh
 Send fails over this many bytes queued.
 
size_t sendLow
 NET_SendReady fires when the queue drains below this.
 
size_t sendQueued
 Bytes waiting in the datagram send queue, guarded by sendLock.
 
bool sendBlocked
 A send returned false at the high watermark and is waiting for NET_SendReady.
 
bool sendPending
 A completion backend has an overlapped send in flight for this socket.
 
NetHandlershandlers
 Per-socket handler overrides, optional.
 
void * handlerCtx
 
RWLock handlerLock
 Guards handlers/handlerCtx/handlerWeak against a concurrent setHandlers()/setHandlersObj()
 
hashtable flows
 Flows keyed by peer address (datagram) or stream id (QUIC). Not used by stream sockets.
 
RWLock flowLock
 
NetFlowflow
 Stream: the single flow for this connection. QUIC: the connection control flow. Not used by datagram sockets.
 
sa_NetFilter filters
 Filters attached to this socket, ordered application -> wire, or empty for none.
 
NetConnectPref connectPref
 Per-socket address-family preference override; NCP_Default inherits the queue's setting when the socket is added (see NetQueue::connectPref)
 
sa_NetAddr connQueue
 List of fallback addresses to try to connect to if pending connect fails.
 
int32 connectIdx
 
NetTimerId connectTimer
 Timer arming the in-flight connect attempt's deadline, or 0 when none is armed.
 
Mutex connectLock
 Serializes the connect-advance transition; see connectTimer.
 

Detailed Description

Network Socket This is a base class; the actual socket implemention will be a derived class provided by the OS abstraction layer.

Definition at line 67 of file socket.h.

Member Function Documentation

◆ atomic()

NetSocket::atomic ( uint32  )

Attempt generation, bumped once per connect attempt.

Lets a completion whose attempt already timed out and was superseded recognize that and just clean itself up instead of acting on a stale result.

Field Documentation

◆ bufs

NetSocketBufs NetSocket::bufs

Type-specific send/receive buffers; which arm is live depends on type

A stream socket has a receive ring and a send chain; a datagram socket has neither and just a send queue (its packets are pooled buffers on flow inboxes instead). See NetSocketBufs – constructed and torn down by hand in NetSocket_init() / _destroy(), since generated code cannot see into a union.

Definition at line 118 of file socket.h.

◆ connectIdx

int32 NetSocket::connectIdx

Index of the next connQueue address to try

Definition at line 199 of file socket.h.

◆ connectLock

Mutex NetSocket::connectLock

Serializes the connect-advance transition; see connectTimer.

Lock order is connectLock -> the queue's timerLock, never the reverse.

Definition at line 216 of file socket.h.

◆ connectTimer

NetTimerId NetSocket::connectTimer

Timer arming the in-flight connect attempt's deadline, or 0 when none is armed.

Three things can complete one connect attempt – the backend, netsocketClose(), and the attempt timing out – and cancelling this timer is what arbitrates between them. A cancel succeeds only if the timer had not already been popped for delivery, so exactly one of the three wins and the losers see either a zeroed field or a failed cancel. Set when an attempt begins, cleared when it resolves.

Definition at line 207 of file socket.h.

◆ connQueue

sa_NetAddr NetSocket::connQueue

List of fallback addresses to try to connect to if pending connect fails.

Filled in by the resolver, then reordered by family per connectPref. The connect state machine tries each address in turn, falling through to the next entry whenever an attempt fails or times out.

Definition at line 198 of file socket.h.

◆ filters

sa_NetFilter NetSocket::filters

Filters attached to this socket, ordered application -> wire, or empty for none.

These are factories, not data-plane stages: each one creates a NetFlowFilter for every flow the socket owns, and the transform itself happens on the flow. See Filters.

Definition at line 189 of file socket.h.

◆ flow

NetFlow* NetSocket::flow

Stream: the single flow for this connection. QUIC: the connection control flow. Not used by datagram sockets.

Created automatically during socket init, so stream consumers never construct a flow themselves. A QUIC socket has this and a flow table: connection-level work – packet decryption, ACK generation, loss recovery, the handshake – is ordered on this one flow, while each of its streams gets a flow of its own that can run on another worker.

Definition at line 173 of file socket.h.

◆ flowLock

RWLock NetSocket::flowLock

Protects the datagram flow table

Definition at line 165 of file socket.h.

◆ flows

hashtable NetSocket::flows

Flows keyed by peer address (datagram) or stream id (QUIC). Not used by stream sockets.

Holds strong references – the socket owns its flows, and the flow's back-pointer to the socket is weak to avoid a reference cycle. Which key a socket's table uses is fixed by type, so NetFlow::key is only meaningful on a QUIC socket's flows.

Definition at line 164 of file socket.h.

◆ handle

NetSockHandle NetSocket::handle

Platform-neutral OS handle, set by the platform factory.

Lets shared code in cx/net/ read a socket's handle to add it to a select set or issue a recv without knowing the derived platform type. NET_INVALID_HANDLE until the factory assigns it.

Definition at line 84 of file socket.h.

◆ handlerCtx

void* NetSocket::handlerCtx

Context passed to per-socket handlers, set by setHandlers()

Definition at line 153 of file socket.h.

◆ handlers

NetHandlers* NetSocket::handlers

Per-socket handler overrides, optional.

Resolved per field between the flow's set and the queue-wide set; see Event Handlers.

Definition at line 152 of file socket.h.

◆ local

NetAddr NetSocket::local

Local end, filled in by bind()

Read back from the socket rather than copied from what was asked for, so a socket bound to port 0 reports the port the OS actually chose.

Definition at line 104 of file socket.h.

◆ mru

uint32 NetSocket::mru

Maximum size of received datagrams

Definition at line 106 of file socket.h.

◆ recvInfo

bool NetSocket::recvInfo

The OS is reporting per-datagram IP information; see setRecvInfo()

Datagram sockets only. While set, the ingest path asks the OS for the local address and ECN mark of every datagram and delivers them on NetMessage::info.

Definition at line 111 of file socket.h.

◆ sendBlocked

bool NetSocket::sendBlocked

A send returned false at the high watermark and is waiting for NET_SendReady.

Set when netsocketSend() refuses because the backlog is over sendHigh, cleared once the queue drains back below sendLow and the event fires. Guarded by sendLock.

Definition at line 141 of file socket.h.

◆ sendHigh

size_t NetSocket::sendHigh

Send fails over this many bytes queued.

On a QUIC socket this is per stream rather than per socket: how much one stream will hold for the application before netflowSend() starts refusing.

Definition at line 125 of file socket.h.

◆ sendLock

Mutex NetSocket::sendLock

Exclusive access to send data on this socket

Definition at line 120 of file socket.h.

◆ sendLow

size_t NetSocket::sendLow

NET_SendReady fires when the queue drains below this.

On a QUIC socket this reads the other way round, since a stream's backlog is bounded by what the peer allows rather than by the queue: NET_SendReady fires once the stream has this many bytes of room. A refused send always sets the bar at least as high as what it asked for, so this only matters when it asked for less.

Definition at line 132 of file socket.h.

◆ sendPending

bool NetSocket::sendPending

A completion backend has an overlapped send in flight for this socket.

Set by the IOCP backend while a WSASend/WSASendTo it posted is outstanding, cleared when that completion arrives. While set, the normal synchronous flush is suppressed so it can't touch bytes the in-flight send already owns – doing so could send the same bytes twice and corrupt the connection. Always false on readiness backends. Guarded by sendLock.

Definition at line 148 of file socket.h.

◆ sendQueued

size_t NetSocket::sendQueued

Bytes waiting in the datagram send queue, guarded by sendLock.

Unused for stream sockets, which track their backlog on the send chain itself.

Definition at line 136 of file socket.h.

◆ type

NetSocketType NetSocket::type

Connection or connectionless, immutable

Definition at line 78 of file socket.h.


The documentation for this struct was generated from the following file: