Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

tcp_watchdog.hpp 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // tcp_watchdog.hpp
  2. // Copyright (C) 2006 - 2009 MicroNeil Research Corporation
  3. // Watchdog timer for TCP connections.
  4. // Closes the connection if it times out.
  5. // Theoretically, when a socet closes, anything blocked on that socket
  6. // will receive an exception and will deal with that appropriately by
  7. // stopping what it is doing... Can't work on a closed socket ;-)
  8. // This allows blocking sockets to be used safely in that the application
  9. // won't "hang" on a stopped / broken socket.
  10. #ifndef tcp_watchdog_included
  11. #define tcp_watchdog_included
  12. #include "timing.hpp"
  13. #include "threading.hpp"
  14. #include "networking.hpp"
  15. class TCPWatchdog : private Thread {
  16. private:
  17. Socket& MySocket; // Socket to watch.
  18. Timeout MyTimeout; // Timeout value.
  19. void myTask(); // Watchdog task.
  20. volatile bool StillAlive; // True if we're watching.
  21. public:
  22. TCPWatchdog(Socket& SocketToWatch, int Milliseconds); // Create with a socket and a time limit.
  23. ~TCPWatchdog(); // Destroy by stopping the task.
  24. void reset(); // Reset the watchdog - everything is ok.
  25. void reset(int Milliseconds); // Reset the watchdog - use a new time.
  26. void stop(); // Stop the watchdog - all done here.
  27. const static ThreadType Type; // The thread's type.
  28. const static ThreadState Watching; // State when waiting to fire.
  29. const static ThreadState KilledSocket; // Killed The Socket.
  30. const static ThreadState LiveAndLetBe; // Shutdown without incident.
  31. };
  32. #endif