#ifndef __DeadManTimer_h_ #define __DeadManTimer_h_ #include "MiscSupport.h" #include "SelectableRequestQueue.h" #include "ThreadClass.h" #include "ThreadMonitor.h" ///////////////////////////////////////////////////////////////////// // Use this class to monitor sockets and disconnect them when they // have been inactive for too long. // // In the ax_alert_server project this is mostly a convenience. We // don't want to stay connected unless the client has something to // say. That would be a waste of a socket. // // In the semaphore_manager this is actually part of the primary // functionality. The semaphore manager will try to request a // semaphore, and will report failure after a specified amount of // time by disconnecting the socket. ///////////////////////////////////////////////////////////////////// class DeadManTimer : private ThreadClass { private: enum { mtTouchConnection, mtGrantImmunity, mtQuit }; SelectableRequestQueue _incomingRequests; struct Times { TimeVal orig; // This matches _socketByTime; TimeVal last; // This is when we really need to break the connection. Times(TimeVal o, TimeVal l) : orig(o), last(l) { } Times() : orig(false), last(false) { } }; std::map< SocketInfo *, Times > _timeBySocket; typedef std::pair< TimeVal, SocketInfo * > AbortInfo; std::set< AbortInfo > _socketByTime; std::set< SocketInfo * > _immune; void remove(SocketInfo *socket); int _defaultTimeout; protected: void threadFunction(); public: void touchConnection(SocketInfo *socketInfo, int seconds); void touchConnection(SocketInfo *socketInfo) { touchConnection(socketInfo, _defaultTimeout); } void setDefaultTimeout(int seconds) { _defaultTimeout = seconds; } void grantImmunity(SocketInfo *socketInfo); DeadManTimer(); ~DeadManTimer(); }; #endif