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.

85 lines
2.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. #include "talk/xmpp/pingtask.h"
  3. #include "talk/base/logging.h"
  4. #include "talk/base/scoped_ptr.h"
  5. #include "talk/xmpp/constants.h"
  6. namespace buzz {
  7. PingTask::PingTask(buzz::XmppTaskParentInterface* parent,
  8. talk_base::MessageQueue* message_queue,
  9. uint32 ping_period_millis,
  10. uint32 ping_timeout_millis)
  11. : buzz::XmppTask(parent, buzz::XmppEngine::HL_SINGLE),
  12. message_queue_(message_queue),
  13. ping_period_millis_(ping_period_millis),
  14. ping_timeout_millis_(ping_timeout_millis),
  15. next_ping_time_(0),
  16. ping_response_deadline_(0) {
  17. ASSERT(ping_period_millis >= ping_timeout_millis);
  18. }
  19. bool PingTask::HandleStanza(const buzz::XmlElement* stanza) {
  20. if (!MatchResponseIq(stanza, Jid(STR_EMPTY), task_id())) {
  21. return false;
  22. }
  23. if (stanza->Attr(buzz::QN_TYPE) != buzz::STR_RESULT &&
  24. stanza->Attr(buzz::QN_TYPE) != buzz::STR_ERROR) {
  25. return false;
  26. }
  27. QueueStanza(stanza);
  28. return true;
  29. }
  30. // This task runs indefinitely and remains in either the start or blocked
  31. // states.
  32. int PingTask::ProcessStart() {
  33. if (ping_period_millis_ < ping_timeout_millis_) {
  34. LOG(LS_ERROR) << "ping_period_millis should be >= ping_timeout_millis";
  35. return STATE_ERROR;
  36. }
  37. const buzz::XmlElement* stanza = NextStanza();
  38. if (stanza != NULL) {
  39. // Received a ping response of some sort (don't care what it is).
  40. ping_response_deadline_ = 0;
  41. }
  42. uint32 now = talk_base::Time();
  43. // If the ping timed out, signal.
  44. if (ping_response_deadline_ != 0 && now >= ping_response_deadline_) {
  45. SignalTimeout();
  46. return STATE_ERROR;
  47. }
  48. // Send a ping if it's time.
  49. if (now >= next_ping_time_) {
  50. talk_base::scoped_ptr<buzz::XmlElement> stanza(
  51. MakeIq(buzz::STR_GET, Jid(STR_EMPTY), task_id()));
  52. stanza->AddElement(new buzz::XmlElement(QN_PING));
  53. SendStanza(stanza.get());
  54. ping_response_deadline_ = now + ping_timeout_millis_;
  55. next_ping_time_ = now + ping_period_millis_;
  56. // Wake ourselves up when it's time to send another ping or when the ping
  57. // times out (so we can fire a signal).
  58. message_queue_->PostDelayed(ping_timeout_millis_, this);
  59. message_queue_->PostDelayed(ping_period_millis_, this);
  60. }
  61. return STATE_BLOCKED;
  62. }
  63. void PingTask::OnMessage(talk_base::Message* msg) {
  64. // Get the task manager to run this task so we can send a ping or signal or
  65. // process a ping response.
  66. Wake();
  67. }
  68. } // namespace buzz