henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 1 | /* |
| 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 Lemur | 76de83e | 2017-07-06 17:44:34 | [diff] [blame] | 13 | #include "webrtc/rtc_base/checks.h" |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 14 | |
| 15 | namespace cricket { |
| 16 | |
| 17 | enum { |
| 18 | MSG_MONITOR_POLL, |
| 19 | MSG_MONITOR_START, |
| 20 | MSG_MONITOR_STOP, |
| 21 | MSG_MONITOR_SIGNAL |
| 22 | }; |
| 23 | |
pthatcher@webrtc.org | 2aa64ca | 2015-03-13 18:25:21 | [diff] [blame] | 24 | ConnectionMonitor::ConnectionMonitor(ConnectionStatsGetter* stats_getter, |
johan | 98a7365 | 2016-10-17 07:54:57 | [diff] [blame] | 25 | rtc::Thread* network_thread, |
pthatcher@webrtc.org | 2aa64ca | 2015-03-13 18:25:21 | [diff] [blame] | 26 | rtc::Thread* monitoring_thread) { |
| 27 | stats_getter_ = stats_getter; |
johan | 98a7365 | 2016-10-17 07:54:57 | [diff] [blame] | 28 | network_thread_ = network_thread; |
pthatcher@webrtc.org | 2aa64ca | 2015-03-13 18:25:21 | [diff] [blame] | 29 | monitoring_thread_ = monitoring_thread; |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 30 | monitoring_ = false; |
| 31 | } |
| 32 | |
pthatcher@webrtc.org | 2aa64ca | 2015-03-13 18:25:21 | [diff] [blame] | 33 | ConnectionMonitor::~ConnectionMonitor() { |
johan | 98a7365 | 2016-10-17 07:54:57 | [diff] [blame] | 34 | network_thread_->Clear(this); |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 35 | monitoring_thread_->Clear(this); |
| 36 | } |
| 37 | |
pthatcher@webrtc.org | 2aa64ca | 2015-03-13 18:25:21 | [diff] [blame] | 38 | void ConnectionMonitor::Start(int milliseconds) { |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 39 | rate_ = milliseconds; |
| 40 | if (rate_ < 250) |
| 41 | rate_ = 250; |
johan | 98a7365 | 2016-10-17 07:54:57 | [diff] [blame] | 42 | network_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_START); |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 43 | } |
| 44 | |
pthatcher@webrtc.org | 2aa64ca | 2015-03-13 18:25:21 | [diff] [blame] | 45 | void ConnectionMonitor::Stop() { |
johan | 98a7365 | 2016-10-17 07:54:57 | [diff] [blame] | 46 | network_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_STOP); |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 47 | } |
| 48 | |
pthatcher@webrtc.org | 2aa64ca | 2015-03-13 18:25:21 | [diff] [blame] | 49 | void ConnectionMonitor::OnMessage(rtc::Message *message) { |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 50 | rtc::CritScope cs(&crit_); |
| 51 | switch (message->message_id) { |
| 52 | case MSG_MONITOR_START: |
nisse | a875ff8 | 2017-01-12 13:15:36 | [diff] [blame] | 53 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 54 | if (!monitoring_) { |
| 55 | monitoring_ = true; |
pthatcher@webrtc.org | 2aa64ca | 2015-03-13 18:25:21 | [diff] [blame] | 56 | PollConnectionStats_w(); |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 57 | } |
| 58 | break; |
| 59 | |
| 60 | case MSG_MONITOR_STOP: |
nisse | a875ff8 | 2017-01-12 13:15:36 | [diff] [blame] | 61 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 62 | if (monitoring_) { |
| 63 | monitoring_ = false; |
johan | 98a7365 | 2016-10-17 07:54:57 | [diff] [blame] | 64 | network_thread_->Clear(this); |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 65 | } |
| 66 | break; |
| 67 | |
| 68 | case MSG_MONITOR_POLL: |
nisse | a875ff8 | 2017-01-12 13:15:36 | [diff] [blame] | 69 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
pthatcher@webrtc.org | 2aa64ca | 2015-03-13 18:25:21 | [diff] [blame] | 70 | PollConnectionStats_w(); |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 71 | break; |
| 72 | |
| 73 | case MSG_MONITOR_SIGNAL: { |
nisse | a875ff8 | 2017-01-12 13:15:36 | [diff] [blame] | 74 | RTC_DCHECK(rtc::Thread::Current() == monitoring_thread_); |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 75 | std::vector<ConnectionInfo> infos = connection_infos_; |
| 76 | crit_.Leave(); |
| 77 | SignalUpdate(this, infos); |
| 78 | crit_.Enter(); |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
pthatcher@webrtc.org | 2aa64ca | 2015-03-13 18:25:21 | [diff] [blame] | 84 | void ConnectionMonitor::PollConnectionStats_w() { |
nisse | a875ff8 | 2017-01-12 13:15:36 | [diff] [blame] | 85 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 86 | rtc::CritScope cs(&crit_); |
| 87 | |
| 88 | // Gather connection infos |
pthatcher@webrtc.org | 2aa64ca | 2015-03-13 18:25:21 | [diff] [blame] | 89 | stats_getter_->GetConnectionStats(&connection_infos_); |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 90 | |
| 91 | // Signal the monitoring thread, start another poll timer |
Taylor Brandstetter | c0bec8f | 2016-06-10 21:17:27 | [diff] [blame] | 92 | monitoring_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_SIGNAL); |
johan | 98a7365 | 2016-10-17 07:54:57 | [diff] [blame] | 93 | network_thread_->PostDelayed(RTC_FROM_HERE, rate_, this, MSG_MONITOR_POLL); |
henrike@webrtc.org | 1a02faa | 2014-10-28 22:20:11 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } // namespace cricket |