Re: How do I gracefully close down a connection?

Giganews Newsgroups
Subject: Re: How do I gracefully close down a connection?
Posted by:  Remy Lebeau (TeamB) (no.spam@no.spam.com)
Date: Thu, 23 Mar 2006

"Alan McFarlane" <alan.mcfarla…@gmail.com> wrote in message
news:F7F412F8F8F1E240alan.mcfarla…@gmail.com...

> Now, occasionally, I need to shut the server down, but if I've
> got any clients connected, it really pi**es them off.

Tough.

> Ideally, what I'd like to is, when I select "Stop Server" my
> application, it should a) shut the server immediatelly if there are
> no clients connected, or b) set a flag 'FStopping' that I can examine
> in the OnConnect event. If it's set, I can just send a "sorry, server
> going offline" reply and disconnect.

Rather than managing it yourself manually, you could assign the reply values
to the server's MaxConnectionReply property, and then set the MaxConnections
property to 1 (0 will disable the MaxConnections handling, so it has to be
at least 1).  This way, all existing connections will remain active, and new
connections are automatically disconnected as soon as they connect.  Once
the last client has discconnected, you can finish deactivating the server.

> whenever a client disconnects, in the OnDisconnect event, I check
> the current client count. If it's zero, I set the servers 'Active'
property
> to false. This always seems lock the application. :(

You cannot set the Active property from inside any of the server's event
handlers.  The handlers are triggered in the context of worker threads.
When setting the Active property to false, it waits for all threads to
terminate, which cannot happen since you have a thread waiting on the Active
property setter to exit.  So you have a deadlock scenerio.

You will have to signal the main thread to finish deactivating the server,
so that the event handler can exit and release the thread that triggered it.

Gambit

Replies

In response to

How do I gracefully close down a connection? posted by Alan McFarlane on Fri, 24 Mar 2006