Class TClientThread (unit mtsrv2)

Inherits from

TThread

Constructors


constructor Create(ClientHSocket : TSocket);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *} { Create a new thread in the blocked state.


Functions

destructor Destroy;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *} { Destroy the thread.

procedure Release;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *} { This method will be called by the main server thread to terminated a } { client thread.

procedure Execute;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *} { This is the main thread routine.

procedure ServerWSocketDataAvailable(Sender: TObject; Error: Word);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *} { This event handler is called when the client has sent some data to the } { server.

procedure ServerWSocketSessionClosed(Sender: TObject; Error: Word);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *} { This event handler is called when the client connection is closed.

Properties

property ClientWSocket : TWSocket


Events

Variables

FClientHSocket : TSocket;


FClientSocket : TWSocket;



Constructors


constructor Create(ClientHSocket : TSocket);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *} { Create a new thread in the blocked state. This allow the user to register } { the client thread before it actually start working.


Functions


destructor Destroy;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *} { Destroy the thread. Destroy the ClientWSocket if needed.


procedure Release;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *} { This method will be called by the main server thread to terminated a } { client thread.


procedure Execute;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *} { This is the main thread routine. There is not much to do because TWSocket } { is event drive. So everythong to do is done inside an event handler, } { mostly the OnDataAvailable event handler which is triggered each time } { the client send something to the server.


procedure ServerWSocketDataAvailable(Sender: TObject; Error: Word);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *} { This event handler is called when the client has sent some data to the } { server. It is here that we must place the client requests execution } { probably by assembling data in lines, parsing those lines for commands } { and executing the commands. Here for simplicity, we just echo back the } { data sent by the user, without doing anything serious. } { To demonstrate that blocking a thread do not block the others, when a '*' } { is received, we go to Sleep for a few seconds, effectively blocking the } { the client. But as we are multi-threaded, this do not block any other } { client. } { Do not forget to call the Synchronize method if you need to update the } { user interface. Only the main thread can do it (VCL is not thread safe).


procedure ServerWSocketSessionClosed(Sender: TObject; Error: Word);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *} { This event handler is called when the client connection is closed.


Properties


property ClientWSocket : TWSocket


Events


Variables


FClientHSocket : TSocket;


FClientSocket : TWSocket;