Indy 9
|
TIdTCPServer = class(TIdComponent)
TIdTCPServer provides options that allow configuration of the server listener threads, including:
TIdTCPServer also provides properties and methods to control protocol specific options, including:
The TIdTCPServer architecture also implements two mechanisms for servicing peer connections. The first method uses event handlers that execute in the context of the threaded peer connection. The server implementation can assign procedures to the event handlers to respond to the following events:
The other method uses a collection of TIdCommandHandler objects to recognizing valid server commands, and provides methods and properties needed to handle parameters, perform actions, and formulate error and success responses. The properties and method realted to CommandHandler usgae include the following:
Server implementors can choose between using server-based event handlers and command handlers to provide finer control over the operation of the server and protocol.
TIdTCPServer can be used as a base class to create custom TCP server descendants that support specific protocols. Many of the Indy server components, such as TIdChargenServer, TIdDayTimeServer, TIdDICTServer, TIdEchoServer, TIdFingerServer, TIdGopherServer, TIdHostNameServer, TIdHTTPServer, TIdIRCServer, TIdNNTPServer, TIdQUOTDServer, TIdTelnetServer, and TIdWhoisServer, are descendants of the TIdTCPServer component.
property Active: Boolean;
When Active is True, the server will listen for client connections, allocate threads and socket handles for connections, and respond to thread execution in the manner prescribed by the TIdTCPServer or descendant. When Active is False, connections and socket handles will be closed, peer threads will be terminated and freed, the thread manager will be released, and the listener thread will be terminated and freed.
Active uses the protected write procedure SetActive to implement changes in the value of the property.
property Bindings: TIdSocketHandles;
property CommandHandlers: TIdCommandHandlers;
Use OnExecute to provide alternate event handling for the execution of peer threads.
property CommandHandlersEnabled: boolean;
Use OnExecute, when CommandHandlersEnabled is False, to provide alternate event handling for the execution of peer threads.
property DefaultPort: integer;
TIdTCPServer uses the DefaultPort number to bind allocated sockets handles when the Active property to set to True.
property Greeting: TIdRFCReply;
Greeting is initialized in the Create constructor, and freed in Destroy.
property ImplicitIOHandler: Boolean;
ImplicitIOHandler is used in the Destroy destructor to determine if the object instance must free the resources allocated in server IOHandler prior to destruction.
property ImplicitThreadMgr: Boolean;
ImplicitThreadMgr is also used when the serve becomes inactive to determine if the object instance must free the resources allocated in ThreadMgr prior to stopping the server.
property Intercept: TIdServerIntercept;
Intercept allows a developer to create TIdIntercept descendants to encrypt and decrypt data or to compress and decompress messages. One common use of TIdIntercept is to provide support for the Secure Socket Layer (SSL) protocol, using a TIdServerInterceptOpenSSL instance, in TIdTCPServer and descendants.
Intercept is used by TIdListenerThread to insure that the peer TIdTCPServerConnection receives an appropriate socket handle binding from Intercept.Accept. The peer connection should receive a TIdConnectionIntercept descendant appropriate to the TIdServerIntercept that accepts the connection, or the socket connection will not be able to communicate properly.
property IOHandler: TIdServerIOHandler;
TIdListenerThread uses the IOHandler for the server when the listener thread is executed.
property ListenQueue: integer;
Use OnListenException to provide an event handler for notifications of exceptions raised when ListenQueue is exceeded.
The default value for ListenQueue is IdListenQueueDefault as assigned in the Create constructor.
property MaxConnectionReply: TIdRFCReply;
Use MaxConnections to establish the connection limit for the server implementation.
property MaxConnections: Integer;
MaxConnections is used when a TIdListenerThread for the server detects a new connection request. If the MaxConnections limit has been exceeed, the MaxConnectionReply message is written to the peer connection and the socket is Disconnected.
property ReplyExceptionCode: Integer;
ReplyExceptionCode and the text from the exception message are written to the client connection when the exception is trapped.
property ReplyTexts: TIdRFCReplies;
ReplyTexts is initializecd in Create, and freed in Destroy. ReplyTexts is used to generate the TIdTCPServer.Greeting message senst when a threaded client is connected to the server. ReplyTexts is also used in TIdCommand.SendReply to update the TIdCommand.Reply descriptive text written in response to a TIdCommandHandler used in the server implementation.
property ReplyUnknownCommand: TIdRFCReply;
property ReuseSocket: TIdReuseSocket;
The default value for ReuseSocket is rsOSDependent, as assigned in the Create constructor.
property TerminateWaitTime: Integer;
The default value for TerminateWaitTime is 5000 (5 seconds). Use a larger value for TerminateWaitTime when the server handles a large number of peer connection threads, or when OnDisconnected event handling for the peer connection involves time intensive operations.
property ThreadClass: TIdThreadClass;
property ThreadMgr: TIdThreadMgr;
ThreadMgr can accept an explicit reference to one of the TIdThreadMgr descendants, such as TIdThreadMgrDefault or TIdThreadMgrPooled. ThreadMgr will be implicitly created, using a TIdThreadMgrDefault instance, if no other thread manager is assigned when Active is set to True.
ThreadMgr is used by TIdListenerThread.Run get the TIdPeerThread that will act as the thread of execution for new client connections. ThreadMgr uses ThreadClass to determine the class instance returned to the listener thread.
ThreadMgr is used by TIdPeerThread.AfterRun to perform the TIdThreadMgr.ReleaseThread method.
property Threads: TThreadList;
constructor Create(AOwner: TComponent); override;
Create also sets the default values for the following properties:
destructor Destroy; override;
procedure Loaded; override;
Loaded intercepts any change to the Active property until all other properties and sibling components have been read from the component stream.
If the TIdTCPServer should be active, Loaded then sets the Active property to True to force initialization of Bingings and ThreadMgr properties, as well as the TIdTListenerThread used by TIdTCPServer.
property OnAfterCommandHandler: TIdAfterCommandHandlerEvent;
Use OnNoCommandHandler to respond to event notifications when a command handler cannot be located for a client command.
property OnBeforeCommandHandler: TIdBeforeCommandHandlerEvent;
Use OnNoCommandHandler to respond to event notifications when a command handler cannot be located for a client command. Use OnAfterCommandHandler to respond to event notifications after command handler useage has been completed.
property OnConnect: TIdServerThreadEvent;
OnConnect receives AThread as a parameter, representing the TIdPeerThread thread that is requesting the connection.
Assign a TIdServerThreadEvent event handler procedure to OnConnect.
property OnDisconnect: TIdServerThreadEvent;
Note: Do not call Synchronized method in the OnDisconnect event handler. Only inherited Notification calls are safe for use in OnDisconnect. This is important because OnDisconnect is called for active client threads when the server sets Active to False. Using synchronized methods in OnDisconnect can result in a deadlock condition, caused when the thread is suspended while the method is executing in the main VCL thread.
TIdTCPServer will detect the deadlock condition, but a timeout error will be raised while trying to terminate the active threads. Use Thread.Notification to act on the notification that a component is being inserted or removed. For example, if a component has object fields or properties that contain references to other components, it can check the notifications of component removals and invalidate those references as needed.
Assign a TIdServerThreadEvent event handler procedure to OnDisconnect to allow responding to the event notification.
property OnException: TIdServerThreadExceptionEvent;
Applications must assign a procedure to the event handler to allow a response to the event notification.
property OnExecute: TIdServerThreadEvent;
Assign a TIdServerThreadEvent event handler procedure to OnExecute to respond to the event notification.
Use CommandHandlers and CommandHandlersEnabled to provide finer control over commands executed for a peer thread connection.
property OnListenException: TIdListenExceptionEvent;
Applications must assign a procedure to the event handler to allow a response to the event notification.
property OnNoCommandHandler: TIdNoCommandHandlerEvent;
Applications must assign a procedure to the event handler to allow a response to the event notification.