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.

246 lines
8.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, 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/base/portallocatorsessionproxy.h"
  28. #include "talk/base/thread.h"
  29. #include "talk/p2p/base/portallocator.h"
  30. #include "talk/p2p/base/portproxy.h"
  31. namespace cricket {
  32. enum {
  33. MSG_SEND_ALLOCATION_DONE = 1,
  34. MSG_SEND_ALLOCATED_PORTS,
  35. };
  36. typedef talk_base::TypedMessageData<PortAllocatorSessionProxy*> ProxyObjData;
  37. PortAllocatorSessionMuxer::PortAllocatorSessionMuxer(
  38. PortAllocatorSession* session)
  39. : worker_thread_(talk_base::Thread::Current()),
  40. session_(session),
  41. candidate_done_signal_received_(false) {
  42. session_->SignalPortReady.connect(
  43. this, &PortAllocatorSessionMuxer::OnPortReady);
  44. session_->SignalCandidatesAllocationDone.connect(
  45. this, &PortAllocatorSessionMuxer::OnCandidatesAllocationDone);
  46. }
  47. PortAllocatorSessionMuxer::~PortAllocatorSessionMuxer() {
  48. for (size_t i = 0; i < session_proxies_.size(); ++i)
  49. delete session_proxies_[i];
  50. SignalDestroyed(this);
  51. }
  52. void PortAllocatorSessionMuxer::RegisterSessionProxy(
  53. PortAllocatorSessionProxy* session_proxy) {
  54. session_proxies_.push_back(session_proxy);
  55. session_proxy->SignalDestroyed.connect(
  56. this, &PortAllocatorSessionMuxer::OnSessionProxyDestroyed);
  57. session_proxy->set_impl(session_.get());
  58. // Populate new proxy session with the information available in the actual
  59. // implementation.
  60. if (!ports_.empty()) {
  61. worker_thread_->Post(
  62. this, MSG_SEND_ALLOCATED_PORTS, new ProxyObjData(session_proxy));
  63. }
  64. if (candidate_done_signal_received_) {
  65. worker_thread_->Post(
  66. this, MSG_SEND_ALLOCATION_DONE, new ProxyObjData(session_proxy));
  67. }
  68. }
  69. void PortAllocatorSessionMuxer::OnCandidatesAllocationDone(
  70. PortAllocatorSession* session) {
  71. candidate_done_signal_received_ = true;
  72. }
  73. void PortAllocatorSessionMuxer::OnPortReady(PortAllocatorSession* session,
  74. PortInterface* port) {
  75. ASSERT(session == session_.get());
  76. ports_.push_back(port);
  77. port->SignalDestroyed.connect(
  78. this, &PortAllocatorSessionMuxer::OnPortDestroyed);
  79. }
  80. void PortAllocatorSessionMuxer::OnPortDestroyed(PortInterface* port) {
  81. std::vector<PortInterface*>::iterator it =
  82. std::find(ports_.begin(), ports_.end(), port);
  83. if (it != ports_.end())
  84. ports_.erase(it);
  85. }
  86. void PortAllocatorSessionMuxer::OnSessionProxyDestroyed(
  87. PortAllocatorSession* proxy) {
  88. std::vector<PortAllocatorSessionProxy*>::iterator it =
  89. std::find(session_proxies_.begin(), session_proxies_.end(), proxy);
  90. if (it != session_proxies_.end()) {
  91. session_proxies_.erase(it);
  92. }
  93. if (session_proxies_.empty()) {
  94. // Destroy PortAllocatorSession and its associated muxer object if all
  95. // proxies belonging to this session are already destroyed.
  96. delete this;
  97. }
  98. }
  99. void PortAllocatorSessionMuxer::OnMessage(talk_base::Message *pmsg) {
  100. ProxyObjData* proxy = static_cast<ProxyObjData*> (pmsg->pdata);
  101. switch (pmsg->message_id) {
  102. case MSG_SEND_ALLOCATION_DONE:
  103. SendAllocationDone_w(proxy->data());
  104. delete proxy;
  105. break;
  106. case MSG_SEND_ALLOCATED_PORTS:
  107. SendAllocatedPorts_w(proxy->data());
  108. delete proxy;
  109. break;
  110. default:
  111. ASSERT(false);
  112. break;
  113. }
  114. }
  115. void PortAllocatorSessionMuxer::SendAllocationDone_w(
  116. PortAllocatorSessionProxy* proxy) {
  117. std::vector<PortAllocatorSessionProxy*>::iterator iter =
  118. std::find(session_proxies_.begin(), session_proxies_.end(), proxy);
  119. if (iter != session_proxies_.end()) {
  120. proxy->OnCandidatesAllocationDone(session_.get());
  121. }
  122. }
  123. void PortAllocatorSessionMuxer::SendAllocatedPorts_w(
  124. PortAllocatorSessionProxy* proxy) {
  125. std::vector<PortAllocatorSessionProxy*>::iterator iter =
  126. std::find(session_proxies_.begin(), session_proxies_.end(), proxy);
  127. if (iter != session_proxies_.end()) {
  128. for (size_t i = 0; i < ports_.size(); ++i) {
  129. PortInterface* port = ports_[i];
  130. proxy->OnPortReady(session_.get(), port);
  131. // If port already has candidates, send this to the clients of proxy
  132. // session. This can happen if proxy is created later than the actual
  133. // implementation.
  134. if (!port->Candidates().empty()) {
  135. proxy->OnCandidatesReady(session_.get(), port->Candidates());
  136. }
  137. }
  138. }
  139. }
  140. PortAllocatorSessionProxy::~PortAllocatorSessionProxy() {
  141. std::map<PortInterface*, PortProxy*>::iterator it;
  142. for (it = proxy_ports_.begin(); it != proxy_ports_.end(); it++)
  143. delete it->second;
  144. SignalDestroyed(this);
  145. }
  146. void PortAllocatorSessionProxy::set_impl(
  147. PortAllocatorSession* session) {
  148. impl_ = session;
  149. impl_->SignalCandidatesReady.connect(
  150. this, &PortAllocatorSessionProxy::OnCandidatesReady);
  151. impl_->SignalPortReady.connect(
  152. this, &PortAllocatorSessionProxy::OnPortReady);
  153. impl_->SignalCandidatesAllocationDone.connect(
  154. this, &PortAllocatorSessionProxy::OnCandidatesAllocationDone);
  155. }
  156. void PortAllocatorSessionProxy::GetInitialPorts() {
  157. ASSERT(impl_ != NULL);
  158. // Since all proxies share a common PortAllocatorSession, this check will
  159. // prohibit sending multiple STUN ping messages to the stun server, which
  160. // is a problem on Chrome. GetInitialPorts() and StartGetAllPorts() called
  161. // from the worker thread and are called together from TransportChannel,
  162. // checking for IsGettingAllPorts() for GetInitialPorts() will not be a
  163. // problem.
  164. if (!impl_->IsGettingAllPorts()) {
  165. impl_->GetInitialPorts();
  166. }
  167. }
  168. void PortAllocatorSessionProxy::StartGetAllPorts() {
  169. ASSERT(impl_ != NULL);
  170. if (!impl_->IsGettingAllPorts()) {
  171. impl_->StartGetAllPorts();
  172. }
  173. }
  174. void PortAllocatorSessionProxy::StopGetAllPorts() {
  175. ASSERT(impl_ != NULL);
  176. if (impl_->IsGettingAllPorts()) {
  177. impl_->StopGetAllPorts();
  178. }
  179. }
  180. bool PortAllocatorSessionProxy::IsGettingAllPorts() {
  181. ASSERT(impl_ != NULL);
  182. return impl_->IsGettingAllPorts();
  183. }
  184. void PortAllocatorSessionProxy::OnPortReady(PortAllocatorSession* session,
  185. PortInterface* port) {
  186. ASSERT(session == impl_);
  187. PortProxy* proxy_port = new PortProxy();
  188. proxy_port->set_impl(port);
  189. proxy_ports_[port] = proxy_port;
  190. SignalPortReady(this, proxy_port);
  191. }
  192. void PortAllocatorSessionProxy::OnCandidatesReady(
  193. PortAllocatorSession* session,
  194. const std::vector<Candidate>& candidates) {
  195. ASSERT(session == impl_);
  196. // Since all proxy sessions share a common PortAllocatorSession,
  197. // all Candidates will have name associated with the common PAS.
  198. // Change Candidate name with the PortAllocatorSessionProxy name.
  199. std::vector<Candidate> our_candidates;
  200. for (size_t i = 0; i < candidates.size(); ++i) {
  201. Candidate new_local_candidate = candidates[i];
  202. new_local_candidate.set_component(component_);
  203. our_candidates.push_back(new_local_candidate);
  204. }
  205. SignalCandidatesReady(this, our_candidates);
  206. }
  207. void PortAllocatorSessionProxy::OnCandidatesAllocationDone(
  208. PortAllocatorSession* session) {
  209. ASSERT(session == impl_);
  210. SignalCandidatesAllocationDone(this);
  211. }
  212. } // namespace cricket