OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2009 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 #ifndef WEBRTC_P2P_BASE_FAKESESSION_H_ | 11 #ifndef WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
12 #define WEBRTC_P2P_BASE_FAKESESSION_H_ | 12 #define WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
13 | 13 |
14 #include <map> | 14 #include <map> |
15 #include <string> | 15 #include <string> |
16 #include <vector> | 16 #include <vector> |
17 | 17 |
18 #include "webrtc/p2p/base/session.h" | |
19 #include "webrtc/p2p/base/transport.h" | 18 #include "webrtc/p2p/base/transport.h" |
20 #include "webrtc/p2p/base/transportchannel.h" | 19 #include "webrtc/p2p/base/transportchannel.h" |
| 20 #include "webrtc/p2p/base/transportcontroller.h" |
21 #include "webrtc/p2p/base/transportchannelimpl.h" | 21 #include "webrtc/p2p/base/transportchannelimpl.h" |
| 22 #include "webrtc/base/bind.h" |
22 #include "webrtc/base/buffer.h" | 23 #include "webrtc/base/buffer.h" |
23 #include "webrtc/base/fakesslidentity.h" | 24 #include "webrtc/base/fakesslidentity.h" |
24 #include "webrtc/base/messagequeue.h" | 25 #include "webrtc/base/messagequeue.h" |
25 #include "webrtc/base/sigslot.h" | 26 #include "webrtc/base/sigslot.h" |
26 #include "webrtc/base/sslfingerprint.h" | 27 #include "webrtc/base/sslfingerprint.h" |
| 28 #include "webrtc/base/thread.h" |
27 | 29 |
28 namespace cricket { | 30 namespace cricket { |
29 | 31 |
30 class FakeTransport; | 32 class FakeTransport; |
31 | 33 |
32 struct PacketMessageData : public rtc::MessageData { | 34 struct PacketMessageData : public rtc::MessageData { |
33 PacketMessageData(const char* data, size_t len) : packet(data, len) { | 35 PacketMessageData(const char* data, size_t len) : packet(data, len) {} |
34 } | |
35 rtc::Buffer packet; | 36 rtc::Buffer packet; |
36 }; | 37 }; |
37 | 38 |
38 // Fake transport channel class, which can be passed to anything that needs a | 39 // Fake transport channel class, which can be passed to anything that needs a |
39 // transport channel. Can be informed of another FakeTransportChannel via | 40 // transport channel. Can be informed of another FakeTransportChannel via |
40 // SetDestination. | 41 // SetDestination. |
41 class FakeTransportChannel : public TransportChannelImpl, | 42 class FakeTransportChannel : public TransportChannelImpl, |
42 public rtc::MessageHandler { | 43 public rtc::MessageHandler { |
43 public: | 44 public: |
44 explicit FakeTransportChannel(Transport* transport, | 45 explicit FakeTransportChannel(Transport* transport, |
45 const std::string& content_name, | 46 const std::string& content_name, |
46 int component) | 47 int component) |
47 : TransportChannelImpl(content_name, component), | 48 : TransportChannelImpl(content_name, component), |
48 transport_(transport), | 49 transport_(transport), |
49 dest_(NULL), | 50 dest_(nullptr), |
50 state_(STATE_INIT), | 51 state_(STATE_INIT), |
51 async_(false), | 52 async_(false), |
52 identity_(NULL), | 53 identity_(nullptr), |
53 do_dtls_(false), | 54 do_dtls_(false), |
54 role_(ICEROLE_UNKNOWN), | 55 role_(ICEROLE_UNKNOWN), |
55 tiebreaker_(0), | 56 tiebreaker_(0), |
56 ice_proto_(ICEPROTO_HYBRID), | 57 ice_proto_(ICEPROTO_HYBRID), |
57 remote_ice_mode_(ICEMODE_FULL), | 58 remote_ice_mode_(ICEMODE_FULL), |
58 dtls_fingerprint_("", NULL, 0), | 59 dtls_fingerprint_("", nullptr, 0), |
59 ssl_role_(rtc::SSL_CLIENT), | 60 ssl_role_(rtc::SSL_CLIENT), |
60 connection_count_(0) { | 61 connection_count_(0), |
61 } | 62 gathering_state_(kGatheringNew) {} |
62 ~FakeTransportChannel() { | 63 ~FakeTransportChannel() { Reset(); } |
63 Reset(); | |
64 } | |
65 | 64 |
66 uint64 IceTiebreaker() const { return tiebreaker_; } | 65 uint64 IceTiebreaker() const { return tiebreaker_; } |
67 TransportProtocol protocol() const { return ice_proto_; } | 66 TransportProtocol protocol() const { return ice_proto_; } |
68 IceMode remote_ice_mode() const { return remote_ice_mode_; } | 67 IceMode remote_ice_mode() const { return remote_ice_mode_; } |
69 const std::string& ice_ufrag() const { return ice_ufrag_; } | 68 const std::string& ice_ufrag() const { return ice_ufrag_; } |
70 const std::string& ice_pwd() const { return ice_pwd_; } | 69 const std::string& ice_pwd() const { return ice_pwd_; } |
71 const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; } | 70 const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; } |
72 const std::string& remote_ice_pwd() const { return remote_ice_pwd_; } | 71 const std::string& remote_ice_pwd() const { return remote_ice_pwd_; } |
73 const rtc::SSLFingerprint& dtls_fingerprint() const { | 72 const rtc::SSLFingerprint& dtls_fingerprint() const { |
74 return dtls_fingerprint_; | 73 return dtls_fingerprint_; |
75 } | 74 } |
76 | 75 |
77 void SetAsync(bool async) { | 76 void SetAsync(bool async) { async_ = async; } |
78 async_ = async; | |
79 } | |
80 | 77 |
81 virtual Transport* GetTransport() { | 78 virtual Transport* GetTransport() { return transport_; } |
82 return transport_; | |
83 } | |
84 | 79 |
85 virtual TransportChannelState GetState() const { | 80 virtual TransportChannelState GetState() const { |
86 if (connection_count_ == 0) { | 81 if (connection_count_ == 0) { |
87 return TransportChannelState::STATE_FAILED; | 82 return TransportChannelState::STATE_FAILED; |
88 } | 83 } |
89 | 84 |
90 if (connection_count_ == 1) { | 85 if (connection_count_ == 1) { |
91 return TransportChannelState::STATE_COMPLETED; | 86 return TransportChannelState::STATE_COMPLETED; |
92 } | 87 } |
93 | 88 |
(...skipping 13 matching lines...) Expand all Loading... |
107 ice_ufrag_ = ice_ufrag; | 102 ice_ufrag_ = ice_ufrag; |
108 ice_pwd_ = ice_pwd; | 103 ice_pwd_ = ice_pwd; |
109 } | 104 } |
110 virtual void SetRemoteIceCredentials(const std::string& ice_ufrag, | 105 virtual void SetRemoteIceCredentials(const std::string& ice_ufrag, |
111 const std::string& ice_pwd) { | 106 const std::string& ice_pwd) { |
112 remote_ice_ufrag_ = ice_ufrag; | 107 remote_ice_ufrag_ = ice_ufrag; |
113 remote_ice_pwd_ = ice_pwd; | 108 remote_ice_pwd_ = ice_pwd; |
114 } | 109 } |
115 | 110 |
116 virtual void SetRemoteIceMode(IceMode mode) { remote_ice_mode_ = mode; } | 111 virtual void SetRemoteIceMode(IceMode mode) { remote_ice_mode_ = mode; } |
117 virtual bool SetRemoteFingerprint(const std::string& alg, const uint8* digest, | 112 virtual bool SetRemoteFingerprint(const std::string& alg, |
| 113 const uint8* digest, |
118 size_t digest_len) { | 114 size_t digest_len) { |
119 dtls_fingerprint_ = rtc::SSLFingerprint(alg, digest, digest_len); | 115 dtls_fingerprint_ = rtc::SSLFingerprint(alg, digest, digest_len); |
120 return true; | 116 return true; |
121 } | 117 } |
122 virtual bool SetSslRole(rtc::SSLRole role) { | 118 virtual bool SetSslRole(rtc::SSLRole role) { |
123 ssl_role_ = role; | 119 ssl_role_ = role; |
124 return true; | 120 return true; |
125 } | 121 } |
126 virtual bool GetSslRole(rtc::SSLRole* role) const { | 122 virtual bool GetSslRole(rtc::SSLRole* role) const { |
127 *role = ssl_role_; | 123 *role = ssl_role_; |
128 return true; | 124 return true; |
129 } | 125 } |
130 | 126 |
131 virtual void Connect() { | 127 virtual void Connect() { |
132 if (state_ == STATE_INIT) { | 128 if (state_ == STATE_INIT) { |
133 state_ = STATE_CONNECTING; | 129 state_ = STATE_CONNECTING; |
134 } | 130 } |
135 } | 131 } |
| 132 |
| 133 virtual GatheringState gathering_state() const { return gathering_state_; } |
| 134 |
136 virtual void Reset() { | 135 virtual void Reset() { |
137 if (state_ != STATE_INIT) { | 136 if (state_ != STATE_INIT) { |
138 state_ = STATE_INIT; | 137 state_ = STATE_INIT; |
139 if (dest_) { | 138 if (dest_) { |
140 dest_->state_ = STATE_INIT; | 139 dest_->state_ = STATE_INIT; |
141 dest_->dest_ = NULL; | 140 dest_->dest_ = nullptr; |
142 dest_ = NULL; | 141 dest_ = nullptr; |
143 } | 142 } |
144 } | 143 } |
145 } | 144 } |
146 | 145 |
147 void SetWritable(bool writable) { | 146 void SetWritable(bool writable) { set_writable(writable); } |
148 set_writable(writable); | |
149 } | |
150 | 147 |
151 void SetDestination(FakeTransportChannel* dest) { | 148 void SetDestination(FakeTransportChannel* dest) { |
152 if (state_ == STATE_CONNECTING && dest) { | 149 if (state_ == STATE_CONNECTING && dest) { |
153 // This simulates the delivery of candidates. | 150 // This simulates the delivery of candidates. |
154 dest_ = dest; | 151 dest_ = dest; |
155 dest_->dest_ = this; | 152 dest_->dest_ = this; |
156 if (identity_ && dest_->identity_) { | 153 if (identity_ && dest_->identity_) { |
157 do_dtls_ = true; | 154 do_dtls_ = true; |
158 dest_->do_dtls_ = true; | 155 dest_->do_dtls_ = true; |
159 NegotiateSrtpCiphers(); | 156 NegotiateSrtpCiphers(); |
160 } | 157 } |
161 state_ = STATE_CONNECTED; | 158 state_ = STATE_CONNECTED; |
162 dest_->state_ = STATE_CONNECTED; | 159 dest_->state_ = STATE_CONNECTED; |
163 set_writable(true); | 160 set_writable(true); |
164 dest_->set_writable(true); | 161 dest_->set_writable(true); |
165 } else if (state_ == STATE_CONNECTED && !dest) { | 162 } else if (state_ == STATE_CONNECTED && !dest) { |
166 // Simulates loss of connectivity, by asymmetrically forgetting dest_. | 163 // Simulates loss of connectivity, by asymmetrically forgetting dest_. |
167 dest_ = NULL; | 164 dest_ = nullptr; |
168 state_ = STATE_CONNECTING; | 165 state_ = STATE_CONNECTING; |
169 set_writable(false); | 166 set_writable(false); |
170 } | 167 } |
171 } | 168 } |
172 | 169 |
173 void SetConnectionCount(size_t connection_count) { | 170 void SetConnectionCount(size_t connection_count) { |
174 size_t old_connection_count = connection_count_; | 171 size_t old_connection_count = connection_count_; |
175 connection_count_ = connection_count; | 172 connection_count_ = connection_count; |
176 if (connection_count_ < old_connection_count) | 173 if (connection_count_ < old_connection_count) |
177 SignalConnectionRemoved(this); | 174 SignalConnectionRemoved(this); |
178 } | 175 } |
179 | 176 |
180 void SetReceiving(bool receiving) { | 177 void SetCandidatesAllocationDone() { |
181 set_receiving(receiving); | 178 if (gathering_state_ != kGatheringComplete) { |
| 179 gathering_state_ = kGatheringComplete; |
| 180 SignalGatheringState(this); |
| 181 } |
182 } | 182 } |
183 | 183 |
| 184 void SetReceiving(bool receiving) { set_receiving(receiving); } |
| 185 |
184 void SetReceivingTimeout(int timeout) override {} | 186 void SetReceivingTimeout(int timeout) override {} |
185 | 187 |
186 virtual int SendPacket(const char* data, size_t len, | 188 virtual int SendPacket(const char* data, |
187 const rtc::PacketOptions& options, int flags) { | 189 size_t len, |
| 190 const rtc::PacketOptions& options, |
| 191 int flags) { |
188 if (state_ != STATE_CONNECTED) { | 192 if (state_ != STATE_CONNECTED) { |
189 return -1; | 193 return -1; |
190 } | 194 } |
191 | 195 |
192 if (flags != PF_SRTP_BYPASS && flags != 0) { | 196 if (flags != PF_SRTP_BYPASS && flags != 0) { |
193 return -1; | 197 return -1; |
194 } | 198 } |
195 | 199 |
196 PacketMessageData* packet = new PacketMessageData(data, len); | 200 PacketMessageData* packet = new PacketMessageData(data, len); |
197 if (async_) { | 201 if (async_) { |
198 rtc::Thread::Current()->Post(this, 0, packet); | 202 rtc::Thread::Current()->Post(this, 0, packet); |
199 } else { | 203 } else { |
200 rtc::Thread::Current()->Send(this, 0, packet); | 204 rtc::Thread::Current()->Send(this, 0, packet); |
201 } | 205 } |
202 return static_cast<int>(len); | 206 return static_cast<int>(len); |
203 } | 207 } |
204 virtual int SetOption(rtc::Socket::Option opt, int value) { | 208 virtual int SetOption(rtc::Socket::Option opt, int value) { return true; } |
205 return true; | 209 virtual bool GetOption(rtc::Socket::Option opt, int* value) { return true; } |
206 } | 210 virtual int GetError() { return 0; } |
207 virtual bool GetOption(rtc::Socket::Option opt, int* value) { | |
208 return true; | |
209 } | |
210 virtual int GetError() { | |
211 return 0; | |
212 } | |
213 | 211 |
214 virtual void OnSignalingReady() { | 212 virtual void OnCandidate(const Candidate& candidate) {} |
215 } | |
216 virtual void OnCandidate(const Candidate& candidate) { | |
217 } | |
218 | 213 |
219 virtual void OnMessage(rtc::Message* msg) { | 214 virtual void OnMessage(rtc::Message* msg) { |
220 PacketMessageData* data = static_cast<PacketMessageData*>( | 215 PacketMessageData* data = static_cast<PacketMessageData*>(msg->pdata); |
221 msg->pdata); | |
222 dest_->SignalReadPacket(dest_, data->packet.data<char>(), | 216 dest_->SignalReadPacket(dest_, data->packet.data<char>(), |
223 data->packet.size(), rtc::CreatePacketTime(0), 0); | 217 data->packet.size(), rtc::CreatePacketTime(0), 0); |
224 delete data; | 218 delete data; |
225 } | 219 } |
226 | 220 |
227 bool SetLocalIdentity(rtc::SSLIdentity* identity) { | 221 bool SetLocalIdentity(rtc::SSLIdentity* identity) { |
228 identity_ = identity; | 222 identity_ = identity; |
229 return true; | 223 return true; |
230 } | 224 } |
231 | 225 |
232 | |
233 void SetRemoteCertificate(rtc::FakeSSLCertificate* cert) { | 226 void SetRemoteCertificate(rtc::FakeSSLCertificate* cert) { |
234 remote_cert_ = cert; | 227 remote_cert_ = cert; |
235 } | 228 } |
236 | 229 |
237 virtual bool IsDtlsActive() const { | 230 virtual bool IsDtlsActive() const { return do_dtls_; } |
238 return do_dtls_; | |
239 } | |
240 | 231 |
241 virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers) { | 232 virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers) { |
242 srtp_ciphers_ = ciphers; | 233 srtp_ciphers_ = ciphers; |
243 return true; | 234 return true; |
244 } | 235 } |
245 | 236 |
246 virtual bool GetSrtpCipher(std::string* cipher) { | 237 virtual bool GetSrtpCipher(std::string* cipher) { |
247 if (!chosen_srtp_cipher_.empty()) { | 238 if (!chosen_srtp_cipher_.empty()) { |
248 *cipher = chosen_srtp_cipher_; | 239 *cipher = chosen_srtp_cipher_; |
249 return true; | 240 return true; |
250 } | 241 } |
251 return false; | 242 return false; |
252 } | 243 } |
253 | 244 |
254 virtual bool GetSslCipher(std::string* cipher) { | 245 virtual bool GetSslCipher(std::string* cipher) { return false; } |
255 return false; | |
256 } | |
257 | 246 |
258 virtual bool GetLocalIdentity(rtc::SSLIdentity** identity) const { | 247 virtual bool GetLocalIdentity(rtc::SSLIdentity** identity) const { |
259 if (!identity_) | 248 if (!identity_) |
260 return false; | 249 return false; |
261 | 250 |
262 *identity = identity_->GetReference(); | 251 *identity = identity_->GetReference(); |
263 return true; | 252 return true; |
264 } | 253 } |
265 | 254 |
266 virtual bool GetRemoteCertificate(rtc::SSLCertificate** cert) const { | 255 virtual bool GetRemoteCertificate(rtc::SSLCertificate** cert) const { |
(...skipping 13 matching lines...) Expand all Loading... |
280 if (!chosen_srtp_cipher_.empty()) { | 269 if (!chosen_srtp_cipher_.empty()) { |
281 memset(result, 0xff, result_len); | 270 memset(result, 0xff, result_len); |
282 return true; | 271 return true; |
283 } | 272 } |
284 | 273 |
285 return false; | 274 return false; |
286 } | 275 } |
287 | 276 |
288 virtual void NegotiateSrtpCiphers() { | 277 virtual void NegotiateSrtpCiphers() { |
289 for (std::vector<std::string>::const_iterator it1 = srtp_ciphers_.begin(); | 278 for (std::vector<std::string>::const_iterator it1 = srtp_ciphers_.begin(); |
290 it1 != srtp_ciphers_.end(); ++it1) { | 279 it1 != srtp_ciphers_.end(); ++it1) { |
291 for (std::vector<std::string>::const_iterator it2 = | 280 for (std::vector<std::string>::const_iterator it2 = |
292 dest_->srtp_ciphers_.begin(); | 281 dest_->srtp_ciphers_.begin(); |
293 it2 != dest_->srtp_ciphers_.end(); ++it2) { | 282 it2 != dest_->srtp_ciphers_.end(); ++it2) { |
294 if (*it1 == *it2) { | 283 if (*it1 == *it2) { |
295 chosen_srtp_cipher_ = *it1; | 284 chosen_srtp_cipher_ = *it1; |
296 dest_->chosen_srtp_cipher_ = *it2; | 285 dest_->chosen_srtp_cipher_ = *it2; |
297 return; | 286 return; |
298 } | 287 } |
299 } | 288 } |
300 } | 289 } |
301 } | 290 } |
302 | 291 |
303 bool GetStats(ConnectionInfos* infos) override { | 292 bool GetStats(ConnectionInfos* infos) override { |
(...skipping 18 matching lines...) Expand all Loading... |
322 uint64 tiebreaker_; | 311 uint64 tiebreaker_; |
323 IceProtocolType ice_proto_; | 312 IceProtocolType ice_proto_; |
324 std::string ice_ufrag_; | 313 std::string ice_ufrag_; |
325 std::string ice_pwd_; | 314 std::string ice_pwd_; |
326 std::string remote_ice_ufrag_; | 315 std::string remote_ice_ufrag_; |
327 std::string remote_ice_pwd_; | 316 std::string remote_ice_pwd_; |
328 IceMode remote_ice_mode_; | 317 IceMode remote_ice_mode_; |
329 rtc::SSLFingerprint dtls_fingerprint_; | 318 rtc::SSLFingerprint dtls_fingerprint_; |
330 rtc::SSLRole ssl_role_; | 319 rtc::SSLRole ssl_role_; |
331 size_t connection_count_; | 320 size_t connection_count_; |
| 321 GatheringState gathering_state_; |
332 }; | 322 }; |
333 | 323 |
334 // Fake transport class, which can be passed to anything that needs a Transport. | 324 // Fake transport class, which can be passed to anything that needs a Transport. |
335 // Can be informed of another FakeTransport via SetDestination (low-tech way | 325 // Can be informed of another FakeTransport via SetDestination (low-tech way |
336 // of doing candidates) | 326 // of doing candidates) |
337 class FakeTransport : public Transport { | 327 class FakeTransport : public Transport { |
338 public: | 328 public: |
339 typedef std::map<int, FakeTransportChannel*> ChannelMap; | 329 typedef std::map<int, FakeTransportChannel*> ChannelMap; |
340 FakeTransport(rtc::Thread* signaling_thread, | 330 FakeTransport(const std::string& content_name, |
341 rtc::Thread* worker_thread, | 331 PortAllocator* alllocator = nullptr) |
342 const std::string& content_name, | 332 : Transport(content_name, "test_type", nullptr), |
343 PortAllocator* alllocator = NULL) | 333 dest_(nullptr), |
344 : Transport(signaling_thread, worker_thread, | 334 async_(false), |
345 content_name, "test_type", NULL), | 335 identity_(nullptr) {} |
346 dest_(NULL), | 336 |
347 async_(false), | 337 ~FakeTransport() { DestroyAllChannels(); } |
348 identity_(NULL) { | |
349 } | |
350 ~FakeTransport() { | |
351 DestroyAllChannels(); | |
352 } | |
353 | 338 |
354 const ChannelMap& channels() const { return channels_; } | 339 const ChannelMap& channels() const { return channels_; } |
355 | 340 |
356 void SetAsync(bool async) { async_ = async; } | 341 void SetAsync(bool async) { async_ = async; } |
357 void SetDestination(FakeTransport* dest) { | 342 void SetDestination(FakeTransport* dest) { |
358 dest_ = dest; | 343 dest_ = dest; |
359 for (ChannelMap::iterator it = channels_.begin(); it != channels_.end(); | 344 for (const auto& kv : channels_) { |
360 ++it) { | 345 kv.second->SetLocalIdentity(identity_); |
361 it->second->SetLocalIdentity(identity_); | 346 SetChannelDestination(kv.first, kv.second); |
362 SetChannelDestination(it->first, it->second); | |
363 } | 347 } |
364 } | 348 } |
365 | 349 |
366 void SetWritable(bool writable) { | 350 void SetWritable(bool writable) { |
367 for (ChannelMap::iterator it = channels_.begin(); it != channels_.end(); | 351 for (const auto& kv : channels_) { |
368 ++it) { | 352 kv.second->SetWritable(writable); |
369 it->second->SetWritable(writable); | |
370 } | 353 } |
371 } | 354 } |
372 | 355 |
373 void set_identity(rtc::SSLIdentity* identity) { | 356 virtual void SetIdentity(rtc::SSLIdentity* identity) { identity_ = identity; } |
374 identity_ = identity; | 357 virtual bool GetIdentity(rtc::SSLIdentity** identity) { |
| 358 if (!identity_) |
| 359 return false; |
| 360 |
| 361 *identity = identity_->GetReference(); |
| 362 return true; |
375 } | 363 } |
376 | 364 |
377 using Transport::local_description; | 365 using Transport::local_description; |
378 using Transport::remote_description; | 366 using Transport::remote_description; |
379 | 367 |
380 protected: | 368 protected: |
381 virtual TransportChannelImpl* CreateTransportChannel(int component) { | 369 virtual TransportChannelImpl* CreateTransportChannel(int component) { |
382 if (channels_.find(component) != channels_.end()) { | 370 if (channels_.find(component) != channels_.end()) { |
383 return NULL; | 371 return nullptr; |
384 } | 372 } |
385 FakeTransportChannel* channel = | 373 FakeTransportChannel* channel = |
386 new FakeTransportChannel(this, content_name(), component); | 374 new FakeTransportChannel(this, content_name(), component); |
387 channel->SetAsync(async_); | 375 channel->SetAsync(async_); |
388 SetChannelDestination(component, channel); | 376 SetChannelDestination(component, channel); |
389 channels_[component] = channel; | 377 channels_[component] = channel; |
390 return channel; | 378 return channel; |
391 } | 379 } |
| 380 |
392 virtual void DestroyTransportChannel(TransportChannelImpl* channel) { | 381 virtual void DestroyTransportChannel(TransportChannelImpl* channel) { |
393 channels_.erase(channel->component()); | 382 channels_.erase(channel->component()); |
394 delete channel; | 383 delete channel; |
395 } | 384 } |
396 virtual void SetIdentity_w(rtc::SSLIdentity* identity) { | |
397 identity_ = identity; | |
398 } | |
399 virtual bool GetIdentity_w(rtc::SSLIdentity** identity) { | |
400 if (!identity_) | |
401 return false; | |
402 | |
403 *identity = identity_->GetReference(); | |
404 return true; | |
405 } | |
406 | 385 |
407 private: | 386 private: |
408 FakeTransportChannel* GetFakeChannel(int component) { | 387 FakeTransportChannel* GetFakeChannel(int component) { |
409 ChannelMap::iterator it = channels_.find(component); | 388 auto it = channels_.find(component); |
410 return (it != channels_.end()) ? it->second : NULL; | 389 return (it != channels_.end()) ? it->second : nullptr; |
411 } | 390 } |
412 void SetChannelDestination(int component, | 391 |
413 FakeTransportChannel* channel) { | 392 void SetChannelDestination(int component, FakeTransportChannel* channel) { |
414 FakeTransportChannel* dest_channel = NULL; | 393 FakeTransportChannel* dest_channel = nullptr; |
415 if (dest_) { | 394 if (dest_) { |
416 dest_channel = dest_->GetFakeChannel(component); | 395 dest_channel = dest_->GetFakeChannel(component); |
417 if (dest_channel) { | 396 if (dest_channel) { |
418 dest_channel->SetLocalIdentity(dest_->identity_); | 397 dest_channel->SetLocalIdentity(dest_->identity_); |
419 } | 398 } |
420 } | 399 } |
421 channel->SetDestination(dest_channel); | 400 channel->SetDestination(dest_channel); |
422 } | 401 } |
423 | 402 |
424 // Note, this is distinct from the Channel map owned by Transport. | 403 // Note, this is distinct from the Channel map owned by Transport. |
425 // This map just tracks the FakeTransportChannels created by this class. | 404 // This map just tracks the FakeTransportChannels created by this class. |
| 405 // It's mainly needed so that we can access a FakeTransportChannel directly, |
| 406 // even if wrapped by a DtlsTransportChannelWrapper. |
426 ChannelMap channels_; | 407 ChannelMap channels_; |
427 FakeTransport* dest_; | 408 FakeTransport* dest_; |
428 bool async_; | 409 bool async_; |
429 rtc::SSLIdentity* identity_; | 410 rtc::SSLIdentity* identity_; |
430 }; | 411 }; |
431 | 412 |
432 // Fake session class, which can be passed into a BaseChannel object for | 413 // Fake TransportController class, which can be passed into a BaseChannel object |
433 // test purposes. Can be connected to other FakeSessions via Connect(). | 414 // for test purposes. Can be connected to other FakeTransportControllers via |
434 class FakeSession : public BaseSession { | 415 // Connect(). |
| 416 class FakeTransportController : public TransportController { |
435 public: | 417 public: |
436 explicit FakeSession() | 418 FakeTransportController() |
437 : BaseSession(rtc::Thread::Current(), | 419 : TransportController(rtc::Thread::Current(), |
438 rtc::Thread::Current(), | 420 rtc::Thread::Current(), |
439 NULL, "", "", true), | 421 nullptr), |
440 fail_create_channel_(false) { | 422 fail_create_channel_(false) {} |
441 } | 423 |
442 explicit FakeSession(bool initiator) | 424 explicit FakeTransportController(bool initiator) |
443 : BaseSession(rtc::Thread::Current(), | 425 : TransportController(rtc::Thread::Current(), |
444 rtc::Thread::Current(), | 426 rtc::Thread::Current(), |
445 NULL, "", "", initiator), | 427 nullptr), |
446 fail_create_channel_(false) { | 428 fail_create_channel_(false) { |
447 } | 429 SetIceRole(initiator ? ICEROLE_CONTROLLING : ICEROLE_CONTROLLED); |
448 FakeSession(rtc::Thread* worker_thread, bool initiator) | |
449 : BaseSession(rtc::Thread::Current(), | |
450 worker_thread, | |
451 NULL, "", "", initiator), | |
452 fail_create_channel_(false) { | |
453 } | 430 } |
454 | 431 |
455 FakeTransport* GetTransport(const std::string& content_name) { | 432 FakeTransportController(rtc::Thread* worker_thread, bool initiator) |
456 return static_cast<FakeTransport*>( | 433 : TransportController(rtc::Thread::Current(), worker_thread, nullptr), |
457 BaseSession::GetTransport(content_name)); | 434 fail_create_channel_(false) { |
| 435 SetIceRole(initiator ? ICEROLE_CONTROLLING : ICEROLE_CONTROLLED); |
458 } | 436 } |
459 | 437 |
460 void Connect(FakeSession* dest) { | 438 FakeTransport* GetTransport_w(const std::string& transport_name) { |
461 // Simulate the exchange of candidates. | 439 return static_cast<FakeTransport*>( |
462 CompleteNegotiation(); | 440 TransportController::GetTransport_w(transport_name)); |
463 dest->CompleteNegotiation(); | |
464 for (TransportMap::const_iterator it = transport_proxies().begin(); | |
465 it != transport_proxies().end(); ++it) { | |
466 static_cast<FakeTransport*>(it->second->impl())->SetDestination( | |
467 dest->GetTransport(it->first)); | |
468 } | |
469 } | 441 } |
470 | 442 |
471 virtual TransportChannel* CreateChannel( | 443 void Connect(FakeTransportController* dest) { |
472 const std::string& content_name, | 444 worker_thread()->Invoke<void>( |
| 445 rtc::Bind(&FakeTransportController::Connect_w, this, dest)); |
| 446 } |
| 447 |
| 448 virtual TransportChannel* CreateTransportChannel_w( |
| 449 const std::string& transport_name, |
473 int component) { | 450 int component) { |
474 if (fail_create_channel_) { | 451 if (fail_create_channel_) { |
475 return NULL; | 452 return nullptr; |
476 } | 453 } |
477 return BaseSession::CreateChannel(content_name, component); | 454 return TransportController::CreateTransportChannel_w(transport_name, |
| 455 component); |
478 } | 456 } |
479 | 457 |
480 void set_fail_channel_creation(bool fail_channel_creation) { | 458 void set_fail_channel_creation(bool fail_channel_creation) { |
481 fail_create_channel_ = fail_channel_creation; | 459 fail_create_channel_ = fail_channel_creation; |
482 } | 460 } |
483 | 461 |
484 // TODO: Hoist this into Session when we re-work the Session code. | 462 protected: |
485 void set_ssl_identity(rtc::SSLIdentity* identity) { | 463 virtual Transport* CreateTransport_w(const std::string& transport_name) { |
486 for (TransportMap::const_iterator it = transport_proxies().begin(); | 464 return new FakeTransport(transport_name); |
487 it != transport_proxies().end(); ++it) { | 465 } |
488 // We know that we have a FakeTransport* | |
489 | 466 |
490 static_cast<FakeTransport*>(it->second->impl())->set_identity | 467 void Connect_w(FakeTransportController* dest) { |
491 (identity); | 468 // Simulate the exchange of candidates. |
| 469 ConnectChannels_w(); |
| 470 dest->ConnectChannels_w(); |
| 471 for (auto& kv : transports()) { |
| 472 FakeTransport* transport = static_cast<FakeTransport*>(kv.second); |
| 473 transport->SetDestination(dest->GetTransport_w(kv.first)); |
492 } | 474 } |
493 } | 475 } |
494 | 476 |
495 protected: | 477 void ConnectChannels_w() { |
496 virtual Transport* CreateTransport(const std::string& content_name) { | 478 for (auto& kv : transports()) { |
497 return new FakeTransport(signaling_thread(), worker_thread(), content_name); | 479 FakeTransport* transport = static_cast<FakeTransport*>(kv.second); |
498 } | 480 transport->ConnectChannels(); |
499 | |
500 void CompleteNegotiation() { | |
501 for (TransportMap::const_iterator it = transport_proxies().begin(); | |
502 it != transport_proxies().end(); ++it) { | |
503 it->second->CompleteNegotiation(); | |
504 it->second->ConnectChannels(); | |
505 } | 481 } |
506 } | 482 } |
507 | 483 |
508 private: | 484 private: |
509 bool fail_create_channel_; | 485 bool fail_create_channel_; |
510 }; | 486 }; |
511 | 487 |
512 } // namespace cricket | 488 } // namespace cricket |
513 | 489 |
514 #endif // WEBRTC_P2P_BASE_FAKESESSION_H_ | 490 #endif // WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
OLD | NEW |