blob: 5245535bc0a57472a383392e6e452e5b00046cfb [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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
13#include "webrtc/base/common.h"
14
15namespace cricket {
16
17enum {
18 MSG_MONITOR_POLL,
19 MSG_MONITOR_START,
20 MSG_MONITOR_STOP,
21 MSG_MONITOR_SIGNAL
22};
23
24SocketMonitor::SocketMonitor(TransportChannel* channel,
25 rtc::Thread* worker_thread,
26 rtc::Thread* monitor_thread) {
27 channel_ = channel;
28 channel_thread_ = worker_thread;
29 monitoring_thread_ = monitor_thread;
30 monitoring_ = false;
31}
32
33SocketMonitor::~SocketMonitor() {
34 channel_thread_->Clear(this);
35 monitoring_thread_->Clear(this);
36}
37
38void SocketMonitor::Start(int milliseconds) {
39 rate_ = milliseconds;
40 if (rate_ < 250)
41 rate_ = 250;
42 channel_thread_->Post(this, MSG_MONITOR_START);
43}
44
45void SocketMonitor::Stop() {
46 channel_thread_->Post(this, MSG_MONITOR_STOP);
47}
48
49void SocketMonitor::OnMessage(rtc::Message *message) {
50 rtc::CritScope cs(&crit_);
51 switch (message->message_id) {
52 case MSG_MONITOR_START:
53 ASSERT(rtc::Thread::Current() == channel_thread_);
54 if (!monitoring_) {
55 monitoring_ = true;
56 PollSocket(true);
57 }
58 break;
59
60 case MSG_MONITOR_STOP:
61 ASSERT(rtc::Thread::Current() == channel_thread_);
62 if (monitoring_) {
63 monitoring_ = false;
64 channel_thread_->Clear(this);
65 }
66 break;
67
68 case MSG_MONITOR_POLL:
69 ASSERT(rtc::Thread::Current() == channel_thread_);
70 PollSocket(true);
71 break;
72
73 case MSG_MONITOR_SIGNAL: {
74 ASSERT(rtc::Thread::Current() == monitoring_thread_);
75 std::vector<ConnectionInfo> infos = connection_infos_;
76 crit_.Leave();
77 SignalUpdate(this, infos);
78 crit_.Enter();
79 break;
80 }
81 }
82}
83
84void SocketMonitor::PollSocket(bool poll) {
85 ASSERT(rtc::Thread::Current() == channel_thread_);
86 rtc::CritScope cs(&crit_);
87
88 // Gather connection infos
89 channel_->GetStats(&connection_infos_);
90
91 // Signal the monitoring thread, start another poll timer
92 monitoring_thread_->Post(this, MSG_MONITOR_SIGNAL);
93 if (poll)
94 channel_thread_->PostDelayed(rate_, this, MSG_MONITOR_POLL);
95}
96
97} // namespace cricket