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