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/base/rawtransportchannel.h" | 11 #include "webrtc/p2p/base/rawtransportchannel.h" |
12 | 12 |
13 #include <string> | 13 #include <string> |
14 #include <vector> | 14 #include <vector> |
15 #include "webrtc/p2p/base/constants.h" | 15 #include "webrtc/p2p/base/constants.h" |
16 #include "webrtc/p2p/base/portallocator.h" | 16 #include "webrtc/p2p/base/portallocator.h" |
17 #include "webrtc/p2p/base/portinterface.h" | 17 #include "webrtc/p2p/base/portinterface.h" |
18 #include "webrtc/p2p/base/rawtransport.h" | 18 #include "webrtc/p2p/base/rawtransport.h" |
19 #include "webrtc/p2p/base/relayport.h" | 19 #include "webrtc/p2p/base/relayport.h" |
20 #include "webrtc/p2p/base/stunport.h" | 20 #include "webrtc/p2p/base/stunport.h" |
21 #include "webrtc/base/common.h" | 21 #include "webrtc/base/common.h" |
22 | 22 |
23 #if defined(FEATURE_ENABLE_PSTN) | 23 #if defined(FEATURE_ENABLE_PSTN) |
24 | 24 |
25 namespace { | |
26 | |
27 const uint32 MSG_DESTROY_RTC_UNUSED_PORTS = 1; | |
28 | |
29 } // namespace | |
30 | |
31 namespace cricket { | 25 namespace cricket { |
32 | 26 |
33 RawTransportChannel::RawTransportChannel(const std::string& content_name, | 27 RawTransportChannel::RawTransportChannel(const std::string& content_name, |
34 int component, | 28 int component, |
35 RawTransport* transport, | 29 RawTransport* transport, |
36 rtc::Thread *worker_thread, | 30 PortAllocator* allocator) |
37 PortAllocator *allocator) | 31 : TransportChannelImpl(content_name, component), |
38 : TransportChannelImpl(content_name, component), | 32 raw_transport_(transport), |
39 raw_transport_(transport), | 33 allocator_(allocator), |
40 allocator_(allocator), | 34 allocator_session_(NULL), |
41 allocator_session_(NULL), | 35 stun_port_(NULL), |
42 stun_port_(NULL), | 36 relay_port_(NULL), |
43 relay_port_(NULL), | 37 port_(NULL), |
44 port_(NULL), | 38 use_relay_(false) { |
45 use_relay_(false) { | |
46 if (worker_thread == NULL) | |
47 worker_thread_ = raw_transport_->worker_thread(); | |
48 else | |
49 worker_thread_ = worker_thread; | |
50 } | 39 } |
51 | 40 |
52 RawTransportChannel::~RawTransportChannel() { | 41 RawTransportChannel::~RawTransportChannel() { |
53 delete allocator_session_; | 42 delete allocator_session_; |
54 } | 43 } |
55 | 44 |
56 int RawTransportChannel::SendPacket(const char *data, size_t size, | 45 int RawTransportChannel::SendPacket(const char *data, size_t size, |
57 const rtc::PacketOptions& options, | 46 const rtc::PacketOptions& options, |
58 int flags) { | 47 int flags) { |
59 if (port_ == NULL) | 48 if (port_ == NULL) |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 if (port->Type() == STUN_PORT_TYPE) { | 140 if (port->Type() == STUN_PORT_TYPE) { |
152 stun_port_ = static_cast<StunPort*>(port); | 141 stun_port_ = static_cast<StunPort*>(port); |
153 } else if (port->Type() == RELAY_PORT_TYPE) { | 142 } else if (port->Type() == RELAY_PORT_TYPE) { |
154 relay_port_ = static_cast<RelayPort*>(port); | 143 relay_port_ = static_cast<RelayPort*>(port); |
155 } else { | 144 } else { |
156 ASSERT(false); | 145 ASSERT(false); |
157 } | 146 } |
158 } | 147 } |
159 | 148 |
160 void RawTransportChannel::OnCandidatesReady( | 149 void RawTransportChannel::OnCandidatesReady( |
161 PortAllocatorSession *session, const std::vector<Candidate>& candidates) { | 150 PortAllocatorSession* session, |
| 151 const std::vector<Candidate>& candidates) { |
162 ASSERT(session == allocator_session_); | 152 ASSERT(session == allocator_session_); |
163 ASSERT(candidates.size() >= 1); | 153 ASSERT(candidates.size() >= 1); |
164 | 154 |
165 // The most recent candidate is the one we haven't seen yet. | 155 // The most recent candidate is the one we haven't seen yet. |
166 Candidate c = candidates[candidates.size() - 1]; | 156 Candidate c = candidates[candidates.size() - 1]; |
167 | 157 |
168 if (c.type() == STUN_PORT_TYPE) { | 158 if (c.type() == STUN_PORT_TYPE) { |
169 ASSERT(stun_port_ != NULL); | 159 ASSERT(stun_port_ != NULL); |
170 | 160 |
171 #if defined(FEATURE_ENABLE_STUN_CLASSIFICATION) | 161 #if defined(FEATURE_ENABLE_STUN_CLASSIFICATION) |
(...skipping 28 matching lines...) Expand all Loading... |
200 ASSERT(false); | 190 ASSERT(false); |
201 } | 191 } |
202 } | 192 } |
203 | 193 |
204 void RawTransportChannel::SetPort(PortInterface* port) { | 194 void RawTransportChannel::SetPort(PortInterface* port) { |
205 ASSERT(port_ == NULL); | 195 ASSERT(port_ == NULL); |
206 port_ = port; | 196 port_ = port; |
207 | 197 |
208 // We don't need any ports other than the one we picked. | 198 // We don't need any ports other than the one we picked. |
209 allocator_session_->StopGettingPorts(); | 199 allocator_session_->StopGettingPorts(); |
210 worker_thread_->Post( | 200 DestroyUnusedPorts(); |
211 this, MSG_DESTROY_RTC_UNUSED_PORTS, NULL); | |
212 | 201 |
213 // Send a message to the other client containing our address. | 202 // Send a message to the other client containing our address. |
214 | 203 |
215 ASSERT(port_->Candidates().size() >= 1); | 204 ASSERT(port_->Candidates().size() >= 1); |
216 ASSERT(port_->Candidates()[0].protocol() == "udp"); | 205 ASSERT(port_->Candidates()[0].protocol() == "udp"); |
217 SignalCandidateReady(this, port_->Candidates()[0]); | 206 SignalCandidateGathered(this, port_->Candidates()[0]); |
218 | 207 |
219 // Read all packets from this port. | 208 // Read all packets from this port. |
220 port_->EnablePortPackets(); | 209 port_->EnablePortPackets(); |
221 port_->SignalReadPacket.connect(this, &RawTransportChannel::OnReadPacket); | 210 port_->SignalReadPacket.connect(this, &RawTransportChannel::OnReadPacket); |
222 | 211 |
223 // We can write once we have a port and a remote address. | 212 // We can write once we have a port and a remote address. |
224 if (!remote_address_.IsAny()) | 213 if (!remote_address_.IsAny()) |
225 SetWritable(); | 214 SetWritable(); |
226 } | 215 } |
227 | 216 |
228 void RawTransportChannel::SetWritable() { | 217 void RawTransportChannel::SetWritable() { |
229 ASSERT(port_ != NULL); | 218 ASSERT(port_ != NULL); |
230 ASSERT(!remote_address_.IsAny()); | 219 ASSERT(!remote_address_.IsAny()); |
231 | 220 |
232 set_writable(true); | 221 set_writable(true); |
233 | 222 |
234 Candidate remote_candidate; | 223 Candidate remote_candidate; |
235 remote_candidate.set_address(remote_address_); | 224 remote_candidate.set_address(remote_address_); |
236 SignalRouteChange(this, remote_candidate); | 225 SignalRouteChange(this, remote_candidate); |
237 } | 226 } |
238 | 227 |
239 void RawTransportChannel::OnReadPacket( | 228 void RawTransportChannel::OnReadPacket( |
240 PortInterface* port, const char* data, size_t size, | 229 PortInterface* port, const char* data, size_t size, |
241 const rtc::SocketAddress& addr) { | 230 const rtc::SocketAddress& addr) { |
242 ASSERT(port_ == port); | 231 ASSERT(port_ == port); |
243 SignalReadPacket(this, data, size, rtc::CreatePacketTime(0), 0); | 232 SignalReadPacket(this, data, size, rtc::CreatePacketTime(0), 0); |
244 } | 233 } |
245 | 234 |
246 void RawTransportChannel::OnMessage(rtc::Message* msg) { | 235 void RawTransportChannel::DestroyUnusedPorts() { |
247 ASSERT(msg->message_id == MSG_DESTROY_RTC_UNUSED_PORTS); | |
248 ASSERT(port_ != NULL); | 236 ASSERT(port_ != NULL); |
249 if (port_ != stun_port_) { | 237 if (port_ != stun_port_) { |
250 stun_port_->Destroy(); | 238 stun_port_->Destroy(); |
251 stun_port_ = NULL; | 239 stun_port_ = NULL; |
252 } | 240 } |
253 if (port_ != relay_port_ && relay_port_ != NULL) { | 241 if (port_ != relay_port_ && relay_port_ != NULL) { |
254 relay_port_->Destroy(); | 242 relay_port_->Destroy(); |
255 relay_port_ = NULL; | 243 relay_port_ = NULL; |
256 } | 244 } |
257 } | 245 } |
258 | 246 |
259 } // namespace cricket | 247 } // namespace cricket |
260 #endif // defined(FEATURE_ENABLE_PSTN) | 248 #endif // defined(FEATURE_ENABLE_PSTN) |
OLD | NEW |