You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/p2p/client/socketmonitor.h"
  28. #include "talk/base/common.h"
  29. namespace cricket {
  30. enum {
  31. MSG_MONITOR_POLL,
  32. MSG_MONITOR_START,
  33. MSG_MONITOR_STOP,
  34. MSG_MONITOR_SIGNAL
  35. };
  36. SocketMonitor::SocketMonitor(TransportChannel* channel,
  37. talk_base::Thread* worker_thread,
  38. talk_base::Thread* monitor_thread) {
  39. channel_ = channel;
  40. channel_thread_ = worker_thread;
  41. monitoring_thread_ = monitor_thread;
  42. monitoring_ = false;
  43. }
  44. SocketMonitor::~SocketMonitor() {
  45. channel_thread_->Clear(this);
  46. monitoring_thread_->Clear(this);
  47. }
  48. void SocketMonitor::Start(int milliseconds) {
  49. rate_ = milliseconds;
  50. if (rate_ < 250)
  51. rate_ = 250;
  52. channel_thread_->Post(this, MSG_MONITOR_START);
  53. }
  54. void SocketMonitor::Stop() {
  55. channel_thread_->Post(this, MSG_MONITOR_STOP);
  56. }
  57. void SocketMonitor::OnMessage(talk_base::Message *message) {
  58. talk_base::CritScope cs(&crit_);
  59. switch (message->message_id) {
  60. case MSG_MONITOR_START:
  61. ASSERT(talk_base::Thread::Current() == channel_thread_);
  62. if (!monitoring_) {
  63. monitoring_ = true;
  64. PollSocket(true);
  65. }
  66. break;
  67. case MSG_MONITOR_STOP:
  68. ASSERT(talk_base::Thread::Current() == channel_thread_);
  69. if (monitoring_) {
  70. monitoring_ = false;
  71. channel_thread_->Clear(this);
  72. }
  73. break;
  74. case MSG_MONITOR_POLL:
  75. ASSERT(talk_base::Thread::Current() == channel_thread_);
  76. PollSocket(true);
  77. break;
  78. case MSG_MONITOR_SIGNAL: {
  79. ASSERT(talk_base::Thread::Current() == monitoring_thread_);
  80. std::vector<ConnectionInfo> infos = connection_infos_;
  81. crit_.Leave();
  82. SignalUpdate(this, infos);
  83. crit_.Enter();
  84. break;
  85. }
  86. }
  87. }
  88. void SocketMonitor::PollSocket(bool poll) {
  89. ASSERT(talk_base::Thread::Current() == channel_thread_);
  90. talk_base::CritScope cs(&crit_);
  91. // Gather connection infos
  92. channel_->GetStats(&connection_infos_);
  93. // Signal the monitoring thread, start another poll timer
  94. monitoring_thread_->Post(this, MSG_MONITOR_SIGNAL);
  95. if (poll)
  96. channel_thread_->PostDelayed(rate_, this, MSG_MONITOR_POLL);
  97. }
  98. } // namespace cricket