FAQ.Timeout

From Overbyte
Jump to navigation Jump to search

Main page -> FAQ -> Timeout

Implementing a Timeout

Implementing a timeout with async socket is just a matter of testing since how long you haven't received a read request (OnDataAvailable event) from the remote site, or a write request (OnSendData), or a connect status (OnSessionConnected), or a disconnect status (OnSessionClosed) and so on.

It is easy to implement timeouts at the application level using a simple and single TTimer for all timeouts. The idea is to have a list of timeout structures (to be created). This structure could be the time the last activity happened and a callback (a kind of event) to be called when the timeout occur. Of course each structure has to be updated each time some activity occurs.

When the timer event is triggered, you scan the list to see the items having no activity since a given timeout, and then call their callback. From hte callback you do whatever action is needed, for example closing the connection.