| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/p2p/client/socketmonitor.h" | 11 #include "webrtc/p2p/client/socketmonitor.h" |
| 12 | 12 |
| 13 #include "webrtc/base/common.h" | 13 #include "webrtc/base/common.h" |
| 14 | 14 |
| 15 namespace cricket { | 15 namespace cricket { |
| 16 | 16 |
| 17 enum { | 17 enum { |
| 18 MSG_MONITOR_POLL, | 18 MSG_MONITOR_POLL, |
| 19 MSG_MONITOR_START, | 19 MSG_MONITOR_START, |
| 20 MSG_MONITOR_STOP, | 20 MSG_MONITOR_STOP, |
| 21 MSG_MONITOR_SIGNAL | 21 MSG_MONITOR_SIGNAL |
| 22 }; | 22 }; |
| 23 | 23 |
| 24 ConnectionMonitor::ConnectionMonitor(ConnectionStatsGetter* stats_getter, | 24 ConnectionMonitor::ConnectionMonitor(ConnectionStatsGetter* stats_getter, |
| 25 rtc::Thread* worker_thread, | 25 rtc::Thread* network_thread, |
| 26 rtc::Thread* monitoring_thread) { | 26 rtc::Thread* monitoring_thread) { |
| 27 stats_getter_ = stats_getter; | 27 stats_getter_ = stats_getter; |
| 28 worker_thread_ = worker_thread; | 28 network_thread_ = network_thread; |
| 29 monitoring_thread_ = monitoring_thread; | 29 monitoring_thread_ = monitoring_thread; |
| 30 monitoring_ = false; | 30 monitoring_ = false; |
| 31 } | 31 } |
| 32 | 32 |
| 33 ConnectionMonitor::~ConnectionMonitor() { | 33 ConnectionMonitor::~ConnectionMonitor() { |
| 34 worker_thread_->Clear(this); | 34 network_thread_->Clear(this); |
| 35 monitoring_thread_->Clear(this); | 35 monitoring_thread_->Clear(this); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void ConnectionMonitor::Start(int milliseconds) { | 38 void ConnectionMonitor::Start(int milliseconds) { |
| 39 rate_ = milliseconds; | 39 rate_ = milliseconds; |
| 40 if (rate_ < 250) | 40 if (rate_ < 250) |
| 41 rate_ = 250; | 41 rate_ = 250; |
| 42 worker_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_START); | 42 network_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_START); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void ConnectionMonitor::Stop() { | 45 void ConnectionMonitor::Stop() { |
| 46 worker_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_STOP); | 46 network_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_STOP); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void ConnectionMonitor::OnMessage(rtc::Message *message) { | 49 void ConnectionMonitor::OnMessage(rtc::Message *message) { |
| 50 rtc::CritScope cs(&crit_); | 50 rtc::CritScope cs(&crit_); |
| 51 switch (message->message_id) { | 51 switch (message->message_id) { |
| 52 case MSG_MONITOR_START: | 52 case MSG_MONITOR_START: |
| 53 ASSERT(rtc::Thread::Current() == worker_thread_); | 53 ASSERT(rtc::Thread::Current() == network_thread_); |
| 54 if (!monitoring_) { | 54 if (!monitoring_) { |
| 55 monitoring_ = true; | 55 monitoring_ = true; |
| 56 PollConnectionStats_w(); | 56 PollConnectionStats_w(); |
| 57 } | 57 } |
| 58 break; | 58 break; |
| 59 | 59 |
| 60 case MSG_MONITOR_STOP: | 60 case MSG_MONITOR_STOP: |
| 61 ASSERT(rtc::Thread::Current() == worker_thread_); | 61 ASSERT(rtc::Thread::Current() == network_thread_); |
| 62 if (monitoring_) { | 62 if (monitoring_) { |
| 63 monitoring_ = false; | 63 monitoring_ = false; |
| 64 worker_thread_->Clear(this); | 64 network_thread_->Clear(this); |
| 65 } | 65 } |
| 66 break; | 66 break; |
| 67 | 67 |
| 68 case MSG_MONITOR_POLL: | 68 case MSG_MONITOR_POLL: |
| 69 ASSERT(rtc::Thread::Current() == worker_thread_); | 69 ASSERT(rtc::Thread::Current() == network_thread_); |
| 70 PollConnectionStats_w(); | 70 PollConnectionStats_w(); |
| 71 break; | 71 break; |
| 72 | 72 |
| 73 case MSG_MONITOR_SIGNAL: { | 73 case MSG_MONITOR_SIGNAL: { |
| 74 ASSERT(rtc::Thread::Current() == monitoring_thread_); | 74 ASSERT(rtc::Thread::Current() == monitoring_thread_); |
| 75 std::vector<ConnectionInfo> infos = connection_infos_; | 75 std::vector<ConnectionInfo> infos = connection_infos_; |
| 76 crit_.Leave(); | 76 crit_.Leave(); |
| 77 SignalUpdate(this, infos); | 77 SignalUpdate(this, infos); |
| 78 crit_.Enter(); | 78 crit_.Enter(); |
| 79 break; | 79 break; |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 } | 82 } |
| 83 | 83 |
| 84 void ConnectionMonitor::PollConnectionStats_w() { | 84 void ConnectionMonitor::PollConnectionStats_w() { |
| 85 ASSERT(rtc::Thread::Current() == worker_thread_); | 85 ASSERT(rtc::Thread::Current() == network_thread_); |
| 86 rtc::CritScope cs(&crit_); | 86 rtc::CritScope cs(&crit_); |
| 87 | 87 |
| 88 // Gather connection infos | 88 // Gather connection infos |
| 89 stats_getter_->GetConnectionStats(&connection_infos_); | 89 stats_getter_->GetConnectionStats(&connection_infos_); |
| 90 | 90 |
| 91 // Signal the monitoring thread, start another poll timer | 91 // Signal the monitoring thread, start another poll timer |
| 92 monitoring_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_SIGNAL); | 92 monitoring_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_SIGNAL); |
| 93 worker_thread_->PostDelayed(RTC_FROM_HERE, rate_, this, MSG_MONITOR_POLL); | 93 network_thread_->PostDelayed(RTC_FROM_HERE, rate_, this, MSG_MONITOR_POLL); |
| 94 } | 94 } |
| 95 | 95 |
| 96 } // namespace cricket | 96 } // namespace cricket |
| OLD | NEW |