blob: 4e26e427e1df0e91ec238423896e118cd236cd6d [file] [log] [blame]
henrike@webrtc.org1a02faa2014-10-28 22:20:111/*
2 * Copyright 2004 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#include "webrtc/p2p/client/socketmonitor.h"
12
Edward Lemur76de83e2017-07-06 17:44:3413#include "webrtc/rtc_base/checks.h"
henrike@webrtc.org1a02faa2014-10-28 22:20:1114
15namespace cricket {
16
17enum {
18 MSG_MONITOR_POLL,
19 MSG_MONITOR_START,
20 MSG_MONITOR_STOP,
21 MSG_MONITOR_SIGNAL
22};
23
pthatcher@webrtc.org2aa64ca2015-03-13 18:25:2124ConnectionMonitor::ConnectionMonitor(ConnectionStatsGetter* stats_getter,
johan98a73652016-10-17 07:54:5725 rtc::Thread* network_thread,
pthatcher@webrtc.org2aa64ca2015-03-13 18:25:2126 rtc::Thread* monitoring_thread) {
27 stats_getter_ = stats_getter;
johan98a73652016-10-17 07:54:5728 network_thread_ = network_thread;
pthatcher@webrtc.org2aa64ca2015-03-13 18:25:2129 monitoring_thread_ = monitoring_thread;
henrike@webrtc.org1a02faa2014-10-28 22:20:1130 monitoring_ = false;
31}
32
pthatcher@webrtc.org2aa64ca2015-03-13 18:25:2133ConnectionMonitor::~ConnectionMonitor() {
johan98a73652016-10-17 07:54:5734 network_thread_->Clear(this);
henrike@webrtc.org1a02faa2014-10-28 22:20:1135 monitoring_thread_->Clear(this);
36}
37
pthatcher@webrtc.org2aa64ca2015-03-13 18:25:2138void ConnectionMonitor::Start(int milliseconds) {
henrike@webrtc.org1a02faa2014-10-28 22:20:1139 rate_ = milliseconds;
40 if (rate_ < 250)
41 rate_ = 250;
johan98a73652016-10-17 07:54:5742 network_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_START);
henrike@webrtc.org1a02faa2014-10-28 22:20:1143}
44
pthatcher@webrtc.org2aa64ca2015-03-13 18:25:2145void ConnectionMonitor::Stop() {
johan98a73652016-10-17 07:54:5746 network_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_STOP);
henrike@webrtc.org1a02faa2014-10-28 22:20:1147}
48
pthatcher@webrtc.org2aa64ca2015-03-13 18:25:2149void ConnectionMonitor::OnMessage(rtc::Message *message) {
henrike@webrtc.org1a02faa2014-10-28 22:20:1150 rtc::CritScope cs(&crit_);
51 switch (message->message_id) {
52 case MSG_MONITOR_START:
nissea875ff82017-01-12 13:15:3653 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org1a02faa2014-10-28 22:20:1154 if (!monitoring_) {
55 monitoring_ = true;
pthatcher@webrtc.org2aa64ca2015-03-13 18:25:2156 PollConnectionStats_w();
henrike@webrtc.org1a02faa2014-10-28 22:20:1157 }
58 break;
59
60 case MSG_MONITOR_STOP:
nissea875ff82017-01-12 13:15:3661 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org1a02faa2014-10-28 22:20:1162 if (monitoring_) {
63 monitoring_ = false;
johan98a73652016-10-17 07:54:5764 network_thread_->Clear(this);
henrike@webrtc.org1a02faa2014-10-28 22:20:1165 }
66 break;
67
68 case MSG_MONITOR_POLL:
nissea875ff82017-01-12 13:15:3669 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
pthatcher@webrtc.org2aa64ca2015-03-13 18:25:2170 PollConnectionStats_w();
henrike@webrtc.org1a02faa2014-10-28 22:20:1171 break;
72
73 case MSG_MONITOR_SIGNAL: {
nissea875ff82017-01-12 13:15:3674 RTC_DCHECK(rtc::Thread::Current() == monitoring_thread_);
henrike@webrtc.org1a02faa2014-10-28 22:20:1175 std::vector<ConnectionInfo> infos = connection_infos_;
76 crit_.Leave();
77 SignalUpdate(this, infos);
78 crit_.Enter();
79 break;
80 }
81 }
82}
83
pthatcher@webrtc.org2aa64ca2015-03-13 18:25:2184void ConnectionMonitor::PollConnectionStats_w() {
nissea875ff82017-01-12 13:15:3685 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org1a02faa2014-10-28 22:20:1186 rtc::CritScope cs(&crit_);
87
88 // Gather connection infos
pthatcher@webrtc.org2aa64ca2015-03-13 18:25:2189 stats_getter_->GetConnectionStats(&connection_infos_);
henrike@webrtc.org1a02faa2014-10-28 22:20:1190
91 // Signal the monitoring thread, start another poll timer
Taylor Brandstetterc0bec8f2016-06-10 21:17:2792 monitoring_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_SIGNAL);
johan98a73652016-10-17 07:54:5793 network_thread_->PostDelayed(RTC_FROM_HERE, rate_, this, MSG_MONITOR_POLL);
henrike@webrtc.org1a02faa2014-10-28 22:20:1194}
95
96} // namespace cricket