henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 18 | namespace 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. |
| 28 | class PingTask : public buzz::XmppTask, private rtc::MessageHandler { |
| 29 | public: |
| 30 | PingTask(buzz::XmppTaskParentInterface* parent, |
| 31 | rtc::MessageQueue* message_queue, uint32 ping_period_millis, |
| 32 | uint32 ping_timeout_millis); |
| 33 | |
| 34 | virtual bool HandleStanza(const buzz::XmlElement* stanza); |
| 35 | virtual int ProcessStart(); |
| 36 | |
| 37 | // Raised if there is no response to a ping within ping_timeout_millis. |
| 38 | // The task is automatically aborted after a timeout. |
| 39 | sigslot::signal0<> SignalTimeout; |
| 40 | |
| 41 | private: |
| 42 | // Implementation of MessageHandler. |
| 43 | virtual void OnMessage(rtc::Message* msg); |
| 44 | |
| 45 | rtc::MessageQueue* message_queue_; |
| 46 | uint32 ping_period_millis_; |
| 47 | uint32 ping_timeout_millis_; |
| 48 | uint32 next_ping_time_; |
| 49 | uint32 ping_response_deadline_; // 0 if the response has been received |
| 50 | }; |
| 51 | |
| 52 | } // namespace buzz |
| 53 | |
| 54 | #endif // WEBRTC_LIBJINGLE_XMPP_PINGTASK_H_ |