blob: b070a1ccf1ebbc83b5c4acd9c1b7a544098300e6 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_LIBJINGLE_XMPP_PINGTASK_H_
12#define WEBRTC_LIBJINGLE_XMPP_PINGTASK_H_
13
14#include "webrtc/libjingle/xmpp/xmpptask.h"
15#include "webrtc/base/messagehandler.h"
16#include "webrtc/base/messagequeue.h"
17
18namespace buzz {
19
20// Task to periodically send pings to the server to ensure that the network
21// connection is valid, implementing XEP-0199.
22//
23// This is especially useful on cellular networks because:
24// 1. It keeps the connections alive through the cellular network's NATs or
25// proxies.
26// 2. It detects when the server has crashed or any other case in which the
27// connection has broken without a fin or reset packet being sent to us.
28class PingTask : public buzz::XmppTask, private rtc::MessageHandler {
29 public:
30 PingTask(buzz::XmppTaskParentInterface* parent,
Peter Boström0c4e06b2015-10-07 12:23:21 +020031 rtc::MessageQueue* message_queue,
32 uint32_t ping_period_millis,
33 uint32_t ping_timeout_millis);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000034
35 virtual bool HandleStanza(const buzz::XmlElement* stanza);
36 virtual int ProcessStart();
37
38 // Raised if there is no response to a ping within ping_timeout_millis.
39 // The task is automatically aborted after a timeout.
40 sigslot::signal0<> SignalTimeout;
41
42 private:
43 // Implementation of MessageHandler.
44 virtual void OnMessage(rtc::Message* msg);
45
46 rtc::MessageQueue* message_queue_;
Peter Boström0c4e06b2015-10-07 12:23:21 +020047 uint32_t ping_period_millis_;
48 uint32_t ping_timeout_millis_;
Honghai Zhang82d78622016-05-06 11:29:15 -070049 int64_t next_ping_time_;
50 int64_t ping_response_deadline_; // 0 if the response has been received
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051};
52
53} // namespace buzz
54
55#endif // WEBRTC_LIBJINGLE_XMPP_PINGTASK_H_