Class TAppServer (unit ApServer)

Inherits from

TComponent

:TAppServer component is responsible for client connection, communication and management. It is constitued of a listening TWSocket receiving client connections. For each connection a new TClientWSocket is instanciated. TAppServer work with a linked TRequestBroker which manage to execute client request.

Constructors


constructor Create(AOwner: TComponent);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


Functions

destructor Destroy;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

procedure DisconnectAll;

:DisconnectAll will disconnect every connected client.

procedure Start;

:The start procedure will start the server.

procedure Stop;

:The Stop procedure will stop the server which will no more accept new clients, but will not disconnect already connected clients.

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

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

procedure CliTimeout(Sender : TObject; var CanClose : Boolean);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

function GetClientCount : Integer;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

function GetClientWSocket(nIndex : Integer) : TClientWSocket;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

procedure Notification(AComponent: TComponent; operation: TOperation);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

procedure ProcessClientCommand(Sender : TObject; CmdBuf : PChar; CmdLen : Integer);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

procedure SendResponseToClient(Dest : TObject; Status : Integer; Response : PChar; Len : Integer);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

procedure SetClientTimeout(newValue : LongInt);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

procedure SetTimeoutInterval(newValue : LongInt);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

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

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

procedure TimerTimer(Sender : TObject);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

procedure WndProc(var MsgRec: TMessage);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Properties

property Banner : String

:The banner property is the text that is sent to the client when the connection has been established.

property ClientTimeout : LongInt

:ClientTimeout gives the time in seconds before the server disconnect a client without activity.

property Port : String

:The port property gives the port number used by the server to listen for client connection.

property RequestBroker : TRequestBroker

:The server component receive client request, data parameters and send replies to client.

property TimeoutInterval : LongInt

:The server periodically check for client timeout.

property ClientCount : Integer

:ClientCount gives the actual numlber of connected clients.

property ClientWSocket : TClientWSocket

:ClientWSocket is an indexed property whose value is the reference to each connected client.

property Handle : HWND

:The Handle property is the windows handle for the hidden window the component uses for internal messages.

property SrvWSocket : TWSocket

:SrvWSocket is the underlayng TWSocket component.

Events

event OnAfterProcessRequest : TProcessRequestEvent

:The OnAfterProcessRequest is called when a request has been transformed to a command for execution.

event OnAfterSendReply : TClientEvent

:The OnAfterSendReply event is called once the reply header and body has ben written to the internal buffer for sending in the background.

event OnBeforeProcessRequest : TProcessRequestEvent

:The OnBeforeProcessRequest event is triggered just before anything is done with a request received from the client.

event OnBeforeSendReply : TClientEvent

:The OnBeforeSendReply event is called when a reply is ready to be transmitted to the client.

event OnClientClosed : TClientEvent

:When a client disconnect, the OnClientClosed event is triggered.

event OnClientCommand : TClientCommandEvent

:Clients connects to the server to send commands (also called requests) and wait for server responses.

event OnClientConnected : TClientEvent

:This event is triggered when a client connect to the server.

event OnClientTimeout : TClientTimeoutEvent

:When a client had no more activity during some time specified by the ClientTimeout property, it is automatically disconnected.

event OnDisplay : TDisplayEvent

:Sometimes, the server can display some infos about his internal working.

Variables

FBanner : String;


FClientList : TList;


FClientTimeout : LongInt;


FHandle : HWND;


FOnAfterProcessRequest : TProcessRequestEvent;


FOnAfterSendReply : TClientEvent;


FOnBeforeProcessRequest : TProcessRequestEvent;


FOnBeforeSendReply : TClientEvent;


FOnClientClosed : TClientEvent;


FOnClientCommand : TClientCommandEvent;


FOnClientConnected : TClientEvent;


FOnClientTimeout : TClientTimeoutEvent;


FOnDisplay : TDisplayEvent;


FPort : String;


FRequestBroker : TRequestBroker;


FSrvWSocket : TWSocket;


FTimeoutInterval : LongInt;

in seconds

FTimer : TTimer;

in seconds


Constructors


constructor Create(AOwner: TComponent);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


Functions


destructor Destroy;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


procedure DisconnectAll;

:DisconnectAll will disconnect every connected client. Do not confuse with the Stop procedure.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


procedure Start;

:The start procedure will start the server. The server will accept new client connections.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


procedure Stop;

:The Stop procedure will stop the server which will no more accept new clients, but will not disconnect already connected clients.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


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

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


procedure CliTimeout(Sender : TObject; var CanClose : Boolean);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


function GetClientCount : Integer;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


function GetClientWSocket(nIndex : Integer) : TClientWSocket;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


procedure Notification(AComponent: TComponent; operation: TOperation);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


procedure ProcessClientCommand(Sender : TObject; CmdBuf : PChar; CmdLen : Integer);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


procedure SendResponseToClient(Dest : TObject; Status : Integer; Response : PChar; Len : Integer);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


procedure SetClientTimeout(newValue : LongInt);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


procedure SetTimeoutInterval(newValue : LongInt);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


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

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


procedure TimerTimer(Sender : TObject);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


procedure WndProc(var MsgRec: TMessage);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


Properties


property Banner : String

:The banner property is the text that is sent to the client when the connection has been established. This banner can be anything because the client ignore it.


property ClientTimeout : LongInt

:ClientTimeout gives the time in seconds before the server disconnect a client without activity. The default value is 300 seconds.


property Port : String

:The port property gives the port number used by the server to listen for client connection. It can be a numeric value or a string value which must be present in the 'services' file. You should not use any port number already used in your computer. This default value is 2106.


property RequestBroker : TRequestBroker

:The server component receive client request, data parameters and send replies to client. But it delegate the request dispatching to a dedicated component: the RequestBroker component.


property TimeoutInterval : LongInt

:The server periodically check for client timeout. It uses a single TTimer component for this check. The TimeoutInterval is this timer interval and efault to 30 seconds. This means that every 30", the server will goes thru the client list and check for timeout. A smaller value gives better accuracy in timeout detection, but produce some overhead, specially if the number of client is large.


property ClientCount : Integer

:ClientCount gives the actual numlber of connected clients.


property ClientWSocket : TClientWSocket

:ClientWSocket is an indexed property whose value is the reference to each connected client.


property Handle : HWND

:The Handle property is the windows handle for the hidden window the component uses for internal messages.


property SrvWSocket : TWSocket

:SrvWSocket is the underlayng TWSocket component.


Events


event OnAfterProcessRequest : TProcessRequestEvent

:The OnAfterProcessRequest is called when a request has been transformed to a command for execution. It main purpose is to cleanup resources allocated in the OnBeforeProcessRequest event.


event OnAfterSendReply : TClientEvent

:The OnAfterSendReply event is called once the reply header and body has ben written to the internal buffer for sending in the background. It's the right place to deallocate any resource allocated in the OnBeforeSendReply.


event OnBeforeProcessRequest : TProcessRequestEvent

:The OnBeforeProcessRequest event is triggered just before anything is done with a request received from the client. This is the right place to add code for decryption/decompression. If needed, the event handler can allocate memory or resources and change the values on the arguments (passed by var) to fit the requirement. Allocated resources must be freed from the OnAfterProcessCommand.


event OnBeforeSendReply : TClientEvent

:The OnBeforeSendReply event is called when a reply is ready to be transmitted to the client. A reply is made of a header and a body which are accessible thru the CliWSocket properties. The event has the possibility to process the header and answer to encrypt or compress them. It can even allocate some memory for holding the processed header and body. Use the OnAfterSendReply to free any allocated memory. The processed data must *not* contains CR/LF pair as it is used by the client to delimit the reply. If processed data has CR/LF, then it must be escaped in some way.


event OnClientClosed : TClientEvent

:When a client disconnect, the OnClientClosed event is triggered. The event handler could be used to update the server's user interface or to do any other post-processing or cleaning task.


event OnClientCommand : TClientCommandEvent

:Clients connects to the server to send commands (also called requests) and wait for server responses. The OnClientCommand is triggered when such a command arrives, before it gets executed. The event handler can use the command for any purpose, it can even change it.


event OnClientConnected : TClientEvent

:This event is triggered when a client connect to the server. It could be used to disconnect unwanted client or any other processing that must be done when a new client connect, such as updating the server user interface to show how many clients are connected.


event OnClientTimeout : TClientTimeoutEvent

:When a client had no more activity during some time specified by the ClientTimeout property, it is automatically disconnected. If this occurs, the OnClientTimeout event is triggred.


event OnDisplay : TDisplayEvent

:Sometimes, the server can display some infos about his internal working. Each time the server wants to display that info, it triggers the OnDisplay event. There is no need to have an event handler connected to this event. If you ad an event handler, it is probably to display the messages on the server's user interface, or just to log it into a file. Messages to be displayed can be generate by the TAppServer component or any TServerObject used in the server.


Variables


FBanner : String;


FClientList : TList;


FClientTimeout : LongInt;


FHandle : HWND;


FOnAfterProcessRequest : TProcessRequestEvent;


FOnAfterSendReply : TClientEvent;


FOnBeforeProcessRequest : TProcessRequestEvent;


FOnBeforeSendReply : TClientEvent;


FOnClientClosed : TClientEvent;


FOnClientCommand : TClientCommandEvent;


FOnClientConnected : TClientEvent;


FOnClientTimeout : TClientTimeoutEvent;


FOnDisplay : TDisplayEvent;


FPort : String;


FRequestBroker : TRequestBroker;


FSrvWSocket : TWSocket;


FTimeoutInterval : LongInt;

in seconds


FTimer : TTimer;

in seconds