ThServerSocket (HNetworks unit)
ThServerSocket provides TCP and TLS listeners, a message-based UDP listener, and optional request dispatch through a thread pool.
-
Connection Types and Helper
-
ThServerSocket Methods
-
ThServerSocket Properties
ThServerSocket_Connection_Data
Connection context passed to pool callbacks. WinSocket is used for TCP; Chilkat_Socket is used for TLS. Accepted_From identifies the listener, UID identifies the request, and Pool_Index identifies its worker slot (-1 when the pool is full).
Type
ThServerSocket_Connection_Data = record
WinSocket: NativeUInt;
Chilkat_Socket: HCkSocket;
Accepted_From: ThServerSocket;
UID: NativeUInt;
Peer_IP: String;
Pool_Index: Integer;
end;
PhServerSocket_Connection_Data = ^ThServerSocket_Connection_Data;
Trt_ServerSocket_Connection_Helper.Occupied
Returns True when WinSocket is greater than zero or Chilkat_Socket is assigned.
Function Trt_ServerSocket_Connection_Helper.Occupied: Boolean;
Trt_ServerSocket_Connection_Helper.Close_Socket
Closes and clears both socket handles. Other record fields, including UID, remain available after the call. Linger enables the socket linger option.
Procedure Trt_ServerSocket_Connection_Helper.Close_Socket(CONST Linger: Boolean);
Trt_ServerSocket_Connection_Helper.Wait_For_Socket_Writability
Checks whether the active TCP or TLS socket can accept output within TimeOut seconds. The timeout is limited to 0..1800 seconds; returns False when neither handle is present.
Function Trt_ServerSocket_Connection_Helper.Wait_For_Socket_Writability(TimeOut: Double): Boolean;
Trt_ServerSocket_Connection_Helper.Write_Everything_To_Socket
Sends the ThMemoryStream through the active TCP or TLS socket. Returns False if there is no active handle or the send fails. The TCP path does not clear the stream.
Function Trt_ServerSocket_Connection_Helper.Write_Everything_To_Socket(CONST MS: ThMemoryStream): Boolean;
ThServerSocket.Create / Destroy
Create the component with a TComponent owner. Destroy closes the listener, waits for worker threads to finish, and releases the thread pool and its resources.
Constructor ThServerSocket.Create(AOwner: TComponent);
Destructor ThServerSocket.Destroy;
ThServerSocket.Stop
Closes the current TCP, TLS, or UDP listener and releases TLS certificate resources. The thread pool is released when the component is destroyed.
Procedure ThServerSocket.Stop;
ThServerSocket.Connect_UDP_To
Creates a UDP socket connected to _IP and _Port, optionally bound to Bind_NIC_IP. It registers read notifications with this component and returns a socket handle, or 0 on failure. Close the returned handle with HCloseSocket when finished.
Function ThServerSocket.Connect_UDP_To(_IP: RawByteString; _Port: Integer; Bind_NIC_IP: RawByteString = ''): NativeUInt;
ThServerSocket.Open_UDP_Port
Binds a UDP listener to Bind_To_IP and _Port and registers read notifications. An empty or invalid IP binds all local interfaces. Returns True if binding and notification registration succeed. Calling it closes the previous listener on this component.
Function ThServerSocket.Open_UDP_Port(Bind_To_IP: RawByteString; _Port: Integer): Boolean;
ThServerSocket.Listen_To_TCP
Starts a TCP listener and its accept thread. An empty or invalid Bind_To_IP listens on all local interfaces. Returns True after the listener starts. On failure it logs the error; PopupError shows a message, and HALT_On_Error halts the application. If OnTCP_NewConn is assigned, it receives accepted sockets; otherwise they go to the configured thread pool.
Function ThServerSocket.Listen_To_TCP(Bind_To_IP: RawByteString; _Port: Integer; HALT_On_Error, PopupError: Boolean): Boolean;
ThServerSocket.Listen_To_TLS_Chilkat
Starts a TLS listener using Chilkat. It loads the first certificate from tls_root.pem in the application directory. Returns True after the listener and accept thread start. Failure handling follows Listen_To_TCP. If OnTLS_NewConn is assigned, it receives accepted sockets; otherwise they go to the configured thread pool.
Function ThServerSocket.Listen_To_TLS_Chilkat(Bind_To_IP: RawByteString; _Port: Integer; HALT_On_Error, PopupError: Boolean): Boolean;
ThServerSocket.Setup_Thread_Pool
Initializes this instance as a thread pool manager. Call once, with MinAlive at least 1, MaxPool at least 2, and MaxPool no smaller than MinAlive. Returns False if these conditions fail or the pool is already initialized. A busy worker slot can hold one additional pending connection.
Function ThServerSocket.Setup_Thread_Pool(MinAlive, MaxPool: Word): Boolean;
ThServerSocket.Current_Active_Threads
Returns -1 if no pool has been set up. Otherwise counts busy slots and their pending connections, so the result can include queued requests as well as executing workers.
Function ThServerSocket.Current_Active_Threads: Integer;
ThServerSocket.Thread_Pool_Manager
Assign the same manager to multiple listeners when they should share one pool. Set up the manager before accepting connections; its OnNew_Req_Thread handles the requests.
Property ThServerSocket.Thread_Pool_Manager: ThServerSocket Read FThread_Pool_Manager Write FThread_Pool_Manager;
ThServerSocket.OnTCP_NewConn
Runs on the TCP accept thread for each accepted socket. Assigning this callback takes over handling of that socket; it is not automatically sent to the pool.
Type
TSocketEvent = Procedure (Socket: NativeUInt) OF Object;
Property ThServerSocket.OnTCP_NewConn: TSocketEvent Read FOnTCP_NewConn Write FOnTCP_NewConn;
ThServerSocket.OnUDP_DataArrived
Called by the component's window-message handler when its UDP socket is readable. The callback receives the socket handle and must read the datagram itself.
Property ThServerSocket.OnUDP_DataArrived: TSocketEvent Read FOnUDP_DataArrived Write FOnUDP_DataArrived;
ThServerSocket.OnTLS_NewConn
Runs on the TLS accept thread for each accepted Chilkat socket. Assigning this callback takes over handling of that socket instead of forwarding it to the pool.
Type
TChilketSocketEvent = Procedure (ChilkatSocket: Pointer) OF Object;
Property ThServerSocket.OnTLS_NewConn: TChilketSocketEvent Read FonTLS_NewConn Write FonTLS_NewConn;
ThServerSocket.OnNew_Req_Thread
Runs on a worker thread for a pooled TCP or TLS connection. Conn.Peer_IP is populated before the callback, and the pool closes the connection after it returns.
Type
ThServer_New_Req_Event = Procedure (VAR Conn: ThServerSocket_Connection_Data) OF Object;
Property ThServerSocket.OnNew_Req_Thread: ThServer_New_Req_Event Read FonNew_Req_Thread Write FonNew_Req_Thread;
ThServerSocket.NO_Free_Thread_In_Pool
Runs on the accept thread when every pool slot is busy and already has a pending connection. Conn.Pool_Index is -1; Peer_IP is not populated here. Keep this callback brief because it runs while the pool lock is held. The pool closes the connection after the callback, or immediately if the callback is not assigned.
Property ThServerSocket.NO_Free_Thread_In_Pool: ThServer_New_Req_Event
Read FNo_Free_Slop_TP Write FNo_Free_Slop_TP;
Sharing one thread pool
After creating the components, initialize one manager and point both listeners to it. HandleRequest must match ThServer_New_Req_Event.
if not Pool.Setup_Thread_Pool(2, 16) then
Exit;
Pool.OnNew_Req_Thread := HandleRequest;
HttpListener.Thread_Pool_Manager := Pool;
HttpsListener.Thread_Pool_Manager := Pool;
HttpListener.Listen_To_TCP('', 80, False, True);
HttpsListener.Listen_To_TLS_Chilkat('', 443, False, True);
HandleRequest runs on a worker thread; synchronize access to VCL controls.