TCtrlSocket

Fonte: 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

DATATypeDescription
aRequestsArrayArray of pending request objects (socket + callbacks)
abEndArrayArray of codeblocks evaluated when each request connection ends
abChanceArrayArray of codeblocks evaluated when each request gets processing time
anSendedArrayArray of byte counts sent for each request
lServerWorkingLogical.T. when the server is actively serving requests

Methods

MethodDescription
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

Veja Também