16#include <cx/net/net_shared.h>
23typedef struct NetQueue_WeakRef NetQueue_WeakRef;
26#define _sti_NetQueue _sti_object
27#define SType_NetQueue NetQueue*
28#define STStorageType_NetQueue NetQueue*
29#define STypeArg_NetQueue(type, val) stgeneric(object, (ObjInst*)objInstCheckClass(NetQueue, val))
30#define STypeArgPtr_NetQueue(type, val) (stgeneric*)objInstCheckClassPtr(NetQueue, val)
31#define STypeCheckedArg_NetQueue(type, val) stType(type), stArg(type, val)
32#define STypeCheckedPtrArg_NetQueue(type, val) stType(type), stArgPtr(type, val)
36typedef struct NetQueue_ClassIf {
41 bool (*addSocket)(_In_
void* self,
NetSocket* socket);
42 bool (*removeSocket)(_In_
void* self,
NetSocket* socket);
64 void (*connectArm)(_In_
void* self,
NetSocket* sock);
72 void (*acceptArm)(_In_
void* self,
NetSocket* sock);
73 bool (*shutdown)(_In_
void* self, int64 timeout);
74 bool (*tick)(_In_
void* self, int64 wait);
76extern NetQueue_ClassIf NetQueue_ClassIf_tmpl;
164#define NetQueue(inst) objInstCheckClass(NetQueue, inst)
165#define NetQueueNone ((NetQueue*)NULL)
167typedef struct NetQueue_WeakRef {
170 void* _is_NetQueue_WeakRef;
171 void* _is_ObjInst_WeakRef;
173 atomic(uintptr) _ref;
176#define NetQueue_WeakRef(inst) objWeakRefCheckClass(NetQueue, inst)
187#define netqueuePresetClient(conf) NetQueue_presetClient(conf)
198#define netqueuePresetServer(conf) NetQueue_presetServer(conf)
200_Ret_maybenull_
NetSocket* NetQueue_connect(_In_
NetQueue* self, _In_opt_ strref host, uint16 port, _In_opt_
const NetHandlers* handlers, _In_opt_
void* ctx);
224#define netqueueConnect(self, host, port, handlers, ctx) NetQueue_connect(NetQueue(self), host, port, handlers, ctx)
254#define netqueueConnectPrep(self, host, port, handlers, ctx, prep, prepctx) NetQueue_connectPrep(NetQueue(self), host, port, handlers, ctx, prep, prepctx)
282#define netqueueListen(self, addr, backlog, handlers, ctx) NetQueue_listen(NetQueue(self), addr, backlog, handlers, ctx)
284void NetQueue_setHandlers(_In_
NetQueue* self, _In_opt_
const NetHandlers* handlers, _In_opt_
void* ctx);
295#define netqueueSetHandlers(self, handlers, ctx) NetQueue_setHandlers(NetQueue(self), handlers, ctx)
306#define netqueueSetHandlersObj(self, handlers, ctx) NetQueue_setHandlersObj(NetQueue(self), handlers, ObjInst(ctx))
324#define netqueuePromoteFlow(self, sock, peer) NetQueue_promoteFlow(NetQueue(self), sock, peer)
326uint32 NetQueue_droppedNoBuf(_In_
NetQueue* self);
336#define netqueueDroppedNoBuf(self) NetQueue_droppedNoBuf(NetQueue(self))
351#define netqueue_applyConfig(self, conf) NetQueue__applyConfig(NetQueue(self), conf)
353void NetQueue__startWorkers(_In_
NetQueue* self, int32 n);
362#define netqueue_startWorkers(self, n) NetQueue__startWorkers(NetQueue(self), n)
364void NetQueue__stopWorkers(_In_
NetQueue* self, int64 timeout);
370#define netqueue_stopWorkers(self, timeout) NetQueue__stopWorkers(NetQueue(self), timeout)
384#define netqueue_ingestDatagram(self, sock, peer, info, buf) NetQueue__ingestDatagram(NetQueue(self), sock, peer, info, buf)
391#define netqueue_submit(self, flow, msg) NetQueue__submit(NetQueue(self), flow, msg)
393bool NetQueue__dispatch(_In_
NetQueue* self);
402#define netqueue_dispatch(self) NetQueue__dispatch(NetQueue(self))
413#define netqueue_deliver(self, sock, flow, ev) NetQueue__deliver(NetQueue(self), sock, flow, ev)
415void NetQueue__maint(_In_
NetQueue* self);
423#define netqueue_maint(self) NetQueue__maint(NetQueue(self))
425NetTimerId NetQueue__addTimer(_In_
NetQueue* self, _Inout_
NetFlow* flow, int64 delay, flags_t flags, NetTimerFn fn, _In_opt_
void* ctx);
434#define netqueue_addTimer(self, flow, delay, flags, fn, ctx) NetQueue__addTimer(NetQueue(self), flow, delay, flags, fn, ctx)
442#define netqueue_cancelTimer(self, id) NetQueue__cancelTimer(NetQueue(self), id)
449#define netqueue_rearmTimer(self, id, delay) NetQueue__rearmTimer(NetQueue(self), id, delay)
451void NetQueue__cancelFlowTimers(_In_
NetQueue* self, _Inout_
NetFlow* flow);
456#define netqueue_cancelFlowTimers(self, flow) NetQueue__cancelFlowTimers(NetQueue(self), flow)
458void NetQueue__timerSweep(_In_
NetQueue* self);
464#define netqueue_timerSweep(self) NetQueue__timerSweep(NetQueue(self))
466int64 NetQueue__nextDeadline(_In_
NetQueue* self);
472#define netqueue_nextDeadline(self) NetQueue__nextDeadline(NetQueue(self))
483#define netqueue_findFlow(self, sock, peer, create) NetQueue__findFlow(NetQueue(self), sock, peer, create)
490#define netqueue_admitFlow(self, sock, peer) NetQueue__admitFlow(NetQueue(self), sock, peer)
503#define netqueue_admitFlowObj(self, sock, flow) NetQueue__admitFlowObj(NetQueue(self), sock, flow)
511#define netqueue_reclaimFlows(self, sock) NetQueue__reclaimFlows(NetQueue(self), sock)
522#define netqueueAddSocket(self, socket) (self)->_->addSocket(NetQueue(self), socket)
532#define netqueueRemoveSocket(self, socket) (self)->_->removeSocket(NetQueue(self), socket)
541#define netqueueSocket(self, type) (self)->_->socket(NetQueue(self), type)
556#define netqueueConnectBegin(self, sock, addr) (self)->_->connectBegin(NetQueue(self), sock, addr)
566#define netqueueConnectArm(self, sock) (self)->_->connectArm(NetQueue(self), sock)
576#define netqueueAcceptArm(self, sock) (self)->_->acceptArm(NetQueue(self), sock)
587#define netqueueShutdown(self, timeout) (self)->_->shutdown(NetQueue(self), timeout)
599#define netqueueTick(self, wait) (self)->_->tick(NetQueue(self), wait)
#define saDeclarePtr(name)
void(* NetConnectPrepCB)(NetSocket *sock, void *ctx)
uint64 NetTimerId
Handle to an armed timer, unique for the lifetime of its queue.
NetSocketType
Socket types.
CX Struct System - Introspectable, serializable POD structures in C.
Threading system aggregated header.
CX Object System - Object-oriented programming in C.
A single ordering domain: one connection, or one datagram peer.
Set of event handlers, registered per flow, per socket, or queue-wide.
A received packet, as delivered to a handler.
Per-datagram information the IP layer carries alongside the payload.
Shared, capped pool of network message buffers and headers.
NetQueue manages one or more sockets and a thread pool of workers.
NetTimerEntry * timers
Armed timers across every flow on this queue, kept as a min-heap ordered by deadline.
PrQueue runq
Flows with work pending, waiting for a worker to claim them.
Weak(ObjInst) *handlerWeak
Context passed to queue-wide handlers, set by setHandlersObj() – NULL when handlerCtx is in use inste...
hashtable timerIdx
Maps a timer id to its current index in timers
atomic(uint32) shutdownReq
Set when shutdown begins, so ingest stops producing and addSocket is refused.
uint64 timerSerial
Source of NetTimerId values; never reused, never 0.
atomic(uint32) droppedNoBuf
Datagrams dropped for lack of a receive buffer.
atomic(uint32) timersLive
Number of armed timers, readable without taking timerLock
NetHandlers handlers
Queue-wide fallback handlers.
atomic(uint32) gcLastLo
Timestamp of the last opportunistic GC pass, used to rate-limit it.
hashtable sockets
Sockets this queue is managing, keyed by pointer identity.
NetPool * pool
Shared, capped storage every NetMessage on this queue is drawn from.
NetWakeFn wake
Backend hook that wakes a parked wait so it can recompute its timeout.
Mutex timerLock
Guards the timer heap and every flow's list of armed timer ids.
uint32 timersCap
Entries allocated in timers
NetSendPumpFn sendPump
Backend hook invoked when the send path leaves outbound data queued on a socket.
sa_Thread workers
Dispatch worker threads draining the runqueue (empty in polled mode)
RWLock handlerLock
Guards handlers/handlerCtx/handlerWeak against a concurrent setHandlers()/setHandlersObj()
uint32 ntimers
Entries in use in timers
Semaphore runqSema
Posted once per flow enqueued; dispatch workers wait on it when the runqueue is dry.
void * wakeCtx
Context passed to wake, normally the derived queue.
Counting semaphore synchronization primitive.