You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_XMPP_PINGTASK_H_
  28. #define TALK_XMPP_PINGTASK_H_
  29. #include "talk/base/messagehandler.h"
  30. #include "talk/base/messagequeue.h"
  31. #include "talk/xmpp/xmpptask.h"
  32. namespace buzz {
  33. // Task to periodically send pings to the server to ensure that the network
  34. // connection is valid, implementing XEP-0199.
  35. //
  36. // This is especially useful on cellular networks because:
  37. // 1. It keeps the connections alive through the cellular network's NATs or
  38. // proxies.
  39. // 2. It detects when the server has crashed or any other case in which the
  40. // connection has broken without a fin or reset packet being sent to us.
  41. class PingTask : public buzz::XmppTask, private talk_base::MessageHandler {
  42. public:
  43. PingTask(buzz::XmppTaskParentInterface* parent,
  44. talk_base::MessageQueue* message_queue, uint32 ping_period_millis,
  45. uint32 ping_timeout_millis);
  46. virtual bool HandleStanza(const buzz::XmlElement* stanza);
  47. virtual int ProcessStart();
  48. // Raised if there is no response to a ping within ping_timeout_millis.
  49. // The task is automatically aborted after a timeout.
  50. sigslot::signal0<> SignalTimeout;
  51. private:
  52. // Implementation of MessageHandler.
  53. virtual void OnMessage(talk_base::Message* msg);
  54. talk_base::MessageQueue* message_queue_;
  55. uint32 ping_period_millis_;
  56. uint32 ping_timeout_millis_;
  57. uint32 next_ping_time_;
  58. uint32 ping_response_deadline_; // 0 if the response has been received
  59. };
  60. } // namespace buzz
  61. #endif // TALK_XMPP_PINGTASK_H_