Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Oct : Closing down a Indy 9/10 IdTCPServer

www.cryer.info
Managed Newsgroup Archive

Closing down a Indy 9/10 IdTCPServer

Subject:Closing down a Indy 9/10 IdTCPServer
Posted by:"Matt Mazanec" (tsus..@gmail.com)
Date:20 Oct 2006 06:20:14

I'm looking for a proper way to do the shutdown by killing all connections nicely (I have disconnection methods for the clients).

well I know I cannot do just an active := false;, because all my clients time out and exceptions are raised that I cannot control.

I'm currently doing a loop that goes through the list to tell the clients to disconnect, and to sleep while waiting for them to time out, but sometimes it still does the time out exception.

Indy 9 code (I know i got the exception wrong (this is older code as i do not have fixed on hand) but I hope you understand what I am getting at.  Proper D/C of threaded clients before I turn the server off.  Not sure if it works with clients that login in the meantime though....thats what I am scared of.  Just need to deny new requests, disconnect, and shutdown)

procedure ForceTCPServerDeactivation(var AServer : TIdTCPServer);
var
    List : TList;
    idx : integer;
begin
    List := AServer.Threads.LockList;
    try
        for idx := 0 to List.Count - 1 do
        begin
            try
                TIdPeerThread(List.Items[idx]).Connection.Disconnect;
            except
                on E: Exception do
                begin
                    TIdPeerThread(List.Items[idx]).Stop;
                end;
            end;
        end;
    finally
        AServer.Threads.UnlockList;
    end;
    //Sleep is needed else d/c Timeout exceptions will occur (multithreading)
    Sleep(500);
    {[2006/09/08] Tsusai - Reduced to 500 miliseconds, from 5 whole ones
        it does work on my server, not sure on a whole bunch of them}
    AServer.Active := false;
    AServer.Bindings.Clear;
end;

Replies:

www.cryer.info
Managed Newsgroup Archive