TCtrlSocket
Source: source/classes/tctrsock.prg
Standalone class
TCtrlSocket implements a multiplexed socket server that manages multiple client connections through a request queue. Instead of dedicating one thread per client, it maintains an array of pending requests (aRequests) and serves them in sequence. Each request entry stores the socket object and optional callback blocks for connection end (abEnd) and processing chance (abChance). The class tracks sent byte counts (anSended) and the server running state (lServerWorking).
Key DATA Members
| DATA | Type | Description |
|---|---|---|
aRequests | Array | Array of pending request objects (socket + callbacks) |
abEnd | Array | Array of codeblocks evaluated when each request connection ends |
abChance | Array | Array of codeblocks evaluated when each request gets processing time |
anSended | Array | Array of byte counts sent for each request |
lServerWorking | Logical | .T. when the server is actively serving requests |
Methods
| Method | Description |
|---|---|
New() | Create a new multiplexed socket controller |
AddRequest( oSocket, bEnd, bChance ) | Queue a socket with optional end and chance callbacks |
ServeClients() | Process all pending requests in round-robin fashion |
Example: 2-Socket Multiplexed Server
#include "FiveWin.ch"
function Main()
local oCtrl, oSocket1, oSocket2
// Create the multiplexed socket controller
oCtrl := TCtrlSocket():New()
// Create two client sockets
oSocket1 := TSocket():New()
oSocket2 := TSocket():New()
// Add requests with callbacks
oCtrl:AddRequest( oSocket1, ;
{ || QOut( "Socket 1 ended" ) }, ;
{ || QOut( "Socket 1 processing" ) } )
oCtrl:AddRequest( oSocket2, ;
{ || QOut( "Socket 2 ended" ) }, ;
{ || QOut( "Socket 2 processing" ) } )
// Serve clients in a loop
do while oCtrl:lServerWorking
oCtrl:ServeClients()
// Yield to Windows message queue
SysRefresh()
enddo
return nil
Notes
- TCtrlSocket is designed for cooperative multitasking within a single thread. Call
ServeClients()periodically to give each queued socket a chance to read/write data. - Use the
abChancecallback to handle data processing for each socket. This callback receives the socket object and can callRecv()/Send()to communicate. - The
abEndcallback fires when a connection terminates, allowing cleanup of per-socket resources. anSendedtracks cumulative bytes sent per request, useful for progress monitoring.