Index: webrtc/p2p/base/faketransportcontroller.h |
diff --git a/webrtc/p2p/base/fakesession.h b/webrtc/p2p/base/faketransportcontroller.h |
similarity index 53% |
rename from webrtc/p2p/base/fakesession.h |
rename to webrtc/p2p/base/faketransportcontroller.h |
index bd3c08902508fafc1cf414db3fecbb2d141e48bf..e356e814b754a1b2b6f88f987b7bd33cb46e943a 100644 |
--- a/webrtc/p2p/base/fakesession.h |
+++ b/webrtc/p2p/base/faketransportcontroller.h |
@@ -8,30 +8,31 @@ |
* be found in the AUTHORS file in the root of the source tree. |
*/ |
-#ifndef WEBRTC_P2P_BASE_FAKESESSION_H_ |
-#define WEBRTC_P2P_BASE_FAKESESSION_H_ |
+#ifndef WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
+#define WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
#include <map> |
#include <string> |
#include <vector> |
-#include "webrtc/p2p/base/session.h" |
#include "webrtc/p2p/base/transport.h" |
#include "webrtc/p2p/base/transportchannel.h" |
+#include "webrtc/p2p/base/transportcontroller.h" |
#include "webrtc/p2p/base/transportchannelimpl.h" |
+#include "webrtc/base/bind.h" |
#include "webrtc/base/buffer.h" |
#include "webrtc/base/fakesslidentity.h" |
#include "webrtc/base/messagequeue.h" |
#include "webrtc/base/sigslot.h" |
#include "webrtc/base/sslfingerprint.h" |
+#include "webrtc/base/thread.h" |
namespace cricket { |
class FakeTransport; |
struct PacketMessageData : public rtc::MessageData { |
- PacketMessageData(const char* data, size_t len) : packet(data, len) { |
- } |
+ PacketMessageData(const char* data, size_t len) : packet(data, len) {} |
rtc::Buffer packet; |
}; |
@@ -43,24 +44,12 @@ class FakeTransportChannel : public TransportChannelImpl, |
public rtc::MessageHandler { |
public: |
explicit FakeTransportChannel(Transport* transport, |
- const std::string& content_name, |
+ const std::string& name, |
int component) |
- : TransportChannelImpl(content_name, component), |
+ : TransportChannelImpl(name, component), |
transport_(transport), |
- dest_(nullptr), |
- state_(STATE_INIT), |
- async_(false), |
- do_dtls_(false), |
- role_(ICEROLE_UNKNOWN), |
- tiebreaker_(0), |
- remote_ice_mode_(ICEMODE_FULL), |
- dtls_fingerprint_("", nullptr, 0), |
- ssl_role_(rtc::SSL_CLIENT), |
- connection_count_(0) { |
- } |
- ~FakeTransportChannel() { |
- Reset(); |
- } |
+ dtls_fingerprint_("", nullptr, 0) {} |
+ ~FakeTransportChannel() { Reset(); } |
uint64 IceTiebreaker() const { return tiebreaker_; } |
IceMode remote_ice_mode() const { return remote_ice_mode_; } |
@@ -72,24 +61,23 @@ class FakeTransportChannel : public TransportChannelImpl, |
return dtls_fingerprint_; |
} |
- void SetAsync(bool async) { |
- async_ = async; |
- } |
+ // If async, will send packets by "Post"-ing to message queue instead of |
+ // synchronously "Send"-ing. |
+ void SetAsync(bool async) { async_ = async; } |
- Transport* GetTransport() override { |
- return transport_; |
- } |
+ Transport* GetTransport() override { return transport_; } |
TransportChannelState GetState() const override { |
if (connection_count_ == 0) { |
- return TransportChannelState::STATE_FAILED; |
+ return had_connection_ ? TransportChannelState::STATE_FAILED |
+ : TransportChannelState::STATE_INIT; |
} |
if (connection_count_ == 1) { |
return TransportChannelState::STATE_COMPLETED; |
} |
- return TransportChannelState::STATE_FAILED; |
+ return TransportChannelState::STATE_CONNECTING; |
} |
void SetIceRole(IceRole role) override { role_ = role; } |
@@ -109,7 +97,8 @@ class FakeTransportChannel : public TransportChannelImpl, |
} |
void SetRemoteIceMode(IceMode mode) override { remote_ice_mode_ = mode; } |
- bool SetRemoteFingerprint(const std::string& alg, const uint8* digest, |
+ bool SetRemoteFingerprint(const std::string& alg, |
+ const uint8* digest, |
size_t digest_len) override { |
dtls_fingerprint_ = rtc::SSLFingerprint(alg, digest, digest_len); |
return true; |
@@ -128,27 +117,37 @@ class FakeTransportChannel : public TransportChannelImpl, |
state_ = STATE_CONNECTING; |
} |
} |
- virtual void Reset() { |
+ |
+ void MaybeStartGathering() override { |
+ if (gathering_state_ == kIceGatheringNew) { |
+ gathering_state_ = kIceGatheringGathering; |
+ SignalGatheringState(this); |
+ } |
+ } |
+ |
+ IceGatheringState gathering_state() const override { |
+ return gathering_state_; |
+ } |
+ |
+ void Reset() { |
if (state_ != STATE_INIT) { |
state_ = STATE_INIT; |
if (dest_) { |
dest_->state_ = STATE_INIT; |
- dest_->dest_ = NULL; |
- dest_ = NULL; |
+ dest_->dest_ = nullptr; |
+ dest_ = nullptr; |
} |
} |
} |
- void SetWritable(bool writable) { |
- set_writable(writable); |
- } |
+ void SetWritable(bool writable) { set_writable(writable); } |
void SetDestination(FakeTransportChannel* dest) { |
if (state_ == STATE_CONNECTING && dest) { |
// This simulates the delivery of candidates. |
dest_ = dest; |
dest_->dest_ = this; |
- if (certificate_ && dest_->certificate_) { |
+ if (local_cert_ && dest_->local_cert_) { |
do_dtls_ = true; |
dest_->do_dtls_ = true; |
NegotiateSrtpCiphers(); |
@@ -159,7 +158,7 @@ class FakeTransportChannel : public TransportChannelImpl, |
dest_->set_writable(true); |
} else if (state_ == STATE_CONNECTED && !dest) { |
// Simulates loss of connectivity, by asymmetrically forgetting dest_. |
- dest_ = NULL; |
+ dest_ = nullptr; |
state_ = STATE_CONNECTING; |
set_writable(false); |
} |
@@ -168,18 +167,31 @@ class FakeTransportChannel : public TransportChannelImpl, |
void SetConnectionCount(size_t connection_count) { |
size_t old_connection_count = connection_count_; |
connection_count_ = connection_count; |
+ if (connection_count) |
+ had_connection_ = true; |
if (connection_count_ < old_connection_count) |
SignalConnectionRemoved(this); |
} |
- void SetReceiving(bool receiving) { |
- set_receiving(receiving); |
+ void SetCandidatesGatheringComplete() { |
+ if (gathering_state_ != kIceGatheringComplete) { |
+ gathering_state_ = kIceGatheringComplete; |
+ SignalGatheringState(this); |
+ } |
} |
- void SetReceivingTimeout(int timeout) override {} |
+ void SetReceiving(bool receiving) { set_receiving(receiving); } |
+ |
+ void SetReceivingTimeout(int timeout) override { |
+ receiving_timeout_ = timeout; |
+ } |
- int SendPacket(const char* data, size_t len, |
- const rtc::PacketOptions& options, int flags) override { |
+ int receiving_timeout() const { return receiving_timeout_; } |
+ |
+ int SendPacket(const char* data, |
+ size_t len, |
+ const rtc::PacketOptions& options, |
+ int flags) override { |
if (state_ != STATE_CONNECTED) { |
return -1; |
} |
@@ -196,32 +208,25 @@ class FakeTransportChannel : public TransportChannelImpl, |
} |
return static_cast<int>(len); |
} |
- int SetOption(rtc::Socket::Option opt, int value) override { |
- return true; |
- } |
- bool GetOption(rtc::Socket::Option opt, int* value) override { |
- return true; |
- } |
- int GetError() override { |
- return 0; |
- } |
+ int SetOption(rtc::Socket::Option opt, int value) override { return true; } |
+ bool GetOption(rtc::Socket::Option opt, int* value) override { return true; } |
+ int GetError() override { return 0; } |
- void OnSignalingReady() override { |
- } |
- void OnCandidate(const Candidate& candidate) override { |
+ void AddRemoteCandidate(const Candidate& candidate) override { |
+ remote_candidates_.push_back(candidate); |
} |
+ const Candidates& remote_candidates() const { return remote_candidates_; } |
void OnMessage(rtc::Message* msg) override { |
- PacketMessageData* data = static_cast<PacketMessageData*>( |
- msg->pdata); |
+ PacketMessageData* data = static_cast<PacketMessageData*>(msg->pdata); |
dest_->SignalReadPacket(dest_, data->packet.data<char>(), |
data->packet.size(), rtc::CreatePacketTime(0), 0); |
delete data; |
} |
bool SetLocalCertificate( |
- const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { |
- certificate_ = certificate; |
+ const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
+ local_cert_ = certificate; |
return true; |
} |
@@ -229,9 +234,7 @@ class FakeTransportChannel : public TransportChannelImpl, |
remote_cert_ = cert; |
} |
- bool IsDtlsActive() const override { |
- return do_dtls_; |
- } |
+ bool IsDtlsActive() const override { return do_dtls_; } |
bool SetSrtpCiphers(const std::vector<std::string>& ciphers) override { |
srtp_ciphers_ = ciphers; |
@@ -246,13 +249,10 @@ class FakeTransportChannel : public TransportChannelImpl, |
return false; |
} |
- bool GetSslCipher(std::string* cipher) override { |
- return false; |
- } |
+ bool GetSslCipher(std::string* cipher) override { return false; } |
- rtc::scoped_refptr<rtc::RTCCertificate> |
- GetLocalCertificate() const override { |
- return certificate_; |
+ rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const { |
+ return local_cert_; |
} |
bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override { |
@@ -277,12 +277,12 @@ class FakeTransportChannel : public TransportChannelImpl, |
return false; |
} |
- virtual void NegotiateSrtpCiphers() { |
+ void NegotiateSrtpCiphers() { |
for (std::vector<std::string>::const_iterator it1 = srtp_ciphers_.begin(); |
- it1 != srtp_ciphers_.end(); ++it1) { |
+ it1 != srtp_ciphers_.end(); ++it1) { |
for (std::vector<std::string>::const_iterator it2 = |
- dest_->srtp_ciphers_.begin(); |
- it2 != dest_->srtp_ciphers_.end(); ++it2) { |
+ dest_->srtp_ciphers_.begin(); |
+ it2 != dest_->srtp_ciphers_.end(); ++it2) { |
if (*it1 == *it2) { |
chosen_srtp_cipher_ = *it1; |
dest_->chosen_srtp_cipher_ = *it2; |
@@ -299,27 +299,39 @@ class FakeTransportChannel : public TransportChannelImpl, |
return true; |
} |
+ void set_ssl_max_protocol_version(rtc::SSLProtocolVersion version) { |
+ ssl_max_version_ = version; |
+ } |
+ rtc::SSLProtocolVersion ssl_max_protocol_version() const { |
+ return ssl_max_version_; |
+ } |
+ |
private: |
enum State { STATE_INIT, STATE_CONNECTING, STATE_CONNECTED }; |
Transport* transport_; |
- FakeTransportChannel* dest_; |
- State state_; |
- bool async_; |
- rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
- rtc::FakeSSLCertificate* remote_cert_; |
- bool do_dtls_; |
+ FakeTransportChannel* dest_ = nullptr; |
+ State state_ = STATE_INIT; |
+ bool async_ = false; |
+ Candidates remote_candidates_; |
+ rtc::scoped_refptr<rtc::RTCCertificate> local_cert_; |
+ rtc::FakeSSLCertificate* remote_cert_ = nullptr; |
+ bool do_dtls_ = false; |
std::vector<std::string> srtp_ciphers_; |
std::string chosen_srtp_cipher_; |
- IceRole role_; |
- uint64 tiebreaker_; |
+ int receiving_timeout_ = -1; |
+ IceRole role_ = ICEROLE_UNKNOWN; |
+ uint64 tiebreaker_ = 0; |
std::string ice_ufrag_; |
std::string ice_pwd_; |
std::string remote_ice_ufrag_; |
std::string remote_ice_pwd_; |
- IceMode remote_ice_mode_; |
+ IceMode remote_ice_mode_ = ICEMODE_FULL; |
+ rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_10; |
rtc::SSLFingerprint dtls_fingerprint_; |
- rtc::SSLRole ssl_role_; |
- size_t connection_count_; |
+ rtc::SSLRole ssl_role_ = rtc::SSL_CLIENT; |
+ size_t connection_count_ = 0; |
+ IceGatheringState gathering_state_ = kIceGatheringNew; |
+ bool had_connection_ = false; |
}; |
// Fake transport class, which can be passed to anything that needs a Transport. |
@@ -328,42 +340,65 @@ class FakeTransportChannel : public TransportChannelImpl, |
class FakeTransport : public Transport { |
public: |
typedef std::map<int, FakeTransportChannel*> ChannelMap; |
- FakeTransport(rtc::Thread* signaling_thread, |
- rtc::Thread* worker_thread, |
- const std::string& content_name, |
- PortAllocator* alllocator = nullptr) |
- : Transport(signaling_thread, worker_thread, |
- content_name, nullptr), |
- dest_(nullptr), |
- async_(false) { |
- } |
- ~FakeTransport() { |
- DestroyAllChannels(); |
- } |
+ |
+ explicit FakeTransport(const std::string& name) : Transport(name, nullptr) {} |
+ |
+ // Note that we only have a constructor with the allocator parameter so it can |
+ // be wrapped by a DtlsTransport. |
+ FakeTransport(const std::string& name, PortAllocator* allocator) |
+ : Transport(name, nullptr) {} |
+ |
+ ~FakeTransport() { DestroyAllChannels(); } |
const ChannelMap& channels() const { return channels_; } |
+ // If async, will send packets by "Post"-ing to message queue instead of |
+ // synchronously "Send"-ing. |
void SetAsync(bool async) { async_ = async; } |
void SetDestination(FakeTransport* dest) { |
dest_ = dest; |
- for (ChannelMap::iterator it = channels_.begin(); it != channels_.end(); |
- ++it) { |
- it->second->SetLocalCertificate(certificate_); |
- SetChannelDestination(it->first, it->second); |
+ for (const auto& kv : channels_) { |
+ kv.second->SetLocalCertificate(certificate_); |
+ SetChannelDestination(kv.first, kv.second); |
} |
} |
void SetWritable(bool writable) { |
- for (ChannelMap::iterator it = channels_.begin(); it != channels_.end(); |
- ++it) { |
- it->second->SetWritable(writable); |
+ for (const auto& kv : channels_) { |
+ kv.second->SetWritable(writable); |
} |
} |
- void set_certificate( |
- const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
+ void SetLocalCertificate( |
+ const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { |
certificate_ = certificate; |
} |
+ bool GetLocalCertificate( |
+ rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override { |
+ if (!certificate_) |
+ return false; |
+ |
+ *certificate = certificate_; |
+ return true; |
+ } |
+ |
+ bool GetSslRole(rtc::SSLRole* role) const override { |
+ if (channels_.empty()) { |
+ return false; |
+ } |
+ return channels_.begin()->second->GetSslRole(role); |
+ } |
+ |
+ bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version) override { |
+ ssl_max_version_ = version; |
+ for (const auto& kv : channels_) { |
+ kv.second->set_ssl_max_protocol_version(ssl_max_version_); |
+ } |
+ return true; |
+ } |
+ rtc::SSLProtocolVersion ssl_max_protocol_version() const { |
+ return ssl_max_version_; |
+ } |
using Transport::local_description; |
using Transport::remote_description; |
@@ -371,129 +406,125 @@ class FakeTransport : public Transport { |
protected: |
TransportChannelImpl* CreateTransportChannel(int component) override { |
if (channels_.find(component) != channels_.end()) { |
- return NULL; |
+ return nullptr; |
} |
FakeTransportChannel* channel = |
- new FakeTransportChannel(this, content_name(), component); |
+ new FakeTransportChannel(this, name(), component); |
+ channel->set_ssl_max_protocol_version(ssl_max_version_); |
channel->SetAsync(async_); |
SetChannelDestination(component, channel); |
channels_[component] = channel; |
return channel; |
} |
+ |
void DestroyTransportChannel(TransportChannelImpl* channel) override { |
channels_.erase(channel->component()); |
delete channel; |
} |
- void SetCertificate_w( |
- const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { |
- certificate_ = certificate; |
- } |
- bool GetCertificate_w( |
- rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override { |
- if (!certificate_) |
- return false; |
- |
- *certificate = certificate_; |
- return true; |
- } |
private: |
FakeTransportChannel* GetFakeChannel(int component) { |
- ChannelMap::iterator it = channels_.find(component); |
- return (it != channels_.end()) ? it->second : NULL; |
+ auto it = channels_.find(component); |
+ return (it != channels_.end()) ? it->second : nullptr; |
} |
- void SetChannelDestination(int component, |
- FakeTransportChannel* channel) { |
- FakeTransportChannel* dest_channel = NULL; |
+ |
+ void SetChannelDestination(int component, FakeTransportChannel* channel) { |
+ FakeTransportChannel* dest_channel = nullptr; |
if (dest_) { |
dest_channel = dest_->GetFakeChannel(component); |
- if (dest_channel) |
+ if (dest_channel) { |
dest_channel->SetLocalCertificate(dest_->certificate_); |
+ } |
} |
channel->SetDestination(dest_channel); |
} |
// Note, this is distinct from the Channel map owned by Transport. |
// This map just tracks the FakeTransportChannels created by this class. |
+ // It's mainly needed so that we can access a FakeTransportChannel directly, |
+ // even if wrapped by a DtlsTransportChannelWrapper. |
ChannelMap channels_; |
- FakeTransport* dest_; |
- bool async_; |
+ FakeTransport* dest_ = nullptr; |
+ bool async_ = false; |
rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
+ rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_10; |
}; |
-// Fake session class, which can be passed into a BaseChannel object for |
-// test purposes. Can be connected to other FakeSessions via Connect(). |
-class FakeSession : public BaseSession { |
+// Fake TransportController class, which can be passed into a BaseChannel object |
+// for test purposes. Can be connected to other FakeTransportControllers via |
+// Connect(). |
+// |
+// This fake is unusual in that for the most part, it's implemented with the |
+// real TransportController code, but with fake TransportChannels underneath. |
+class FakeTransportController : public TransportController { |
public: |
- explicit FakeSession() |
- : BaseSession(rtc::Thread::Current(), |
- rtc::Thread::Current(), |
- NULL, "", "", true), |
- fail_create_channel_(false) { |
- } |
- explicit FakeSession(bool initiator) |
- : BaseSession(rtc::Thread::Current(), |
- rtc::Thread::Current(), |
- NULL, "", "", initiator), |
- fail_create_channel_(false) { |
- } |
- FakeSession(rtc::Thread* worker_thread, bool initiator) |
- : BaseSession(rtc::Thread::Current(), |
- worker_thread, |
- NULL, "", "", initiator), |
- fail_create_channel_(false) { |
- } |
- |
- FakeTransport* GetTransport(const std::string& content_name) { |
+ FakeTransportController() |
+ : TransportController(rtc::Thread::Current(), |
+ rtc::Thread::Current(), |
+ nullptr), |
+ fail_create_channel_(false) {} |
+ |
+ explicit FakeTransportController(IceRole role) |
+ : TransportController(rtc::Thread::Current(), |
+ rtc::Thread::Current(), |
+ nullptr), |
+ fail_create_channel_(false) { |
+ SetIceRole(role); |
+ } |
+ |
+ explicit FakeTransportController(rtc::Thread* worker_thread) |
+ : TransportController(rtc::Thread::Current(), worker_thread, nullptr), |
+ fail_create_channel_(false) {} |
+ |
+ FakeTransportController(rtc::Thread* worker_thread, IceRole role) |
+ : TransportController(rtc::Thread::Current(), worker_thread, nullptr), |
+ fail_create_channel_(false) { |
+ SetIceRole(role); |
+ } |
+ |
+ FakeTransport* GetTransport_w(const std::string& transport_name) { |
return static_cast<FakeTransport*>( |
- BaseSession::GetTransport(content_name)); |
+ TransportController::GetTransport_w(transport_name)); |
} |
- void Connect(FakeSession* dest) { |
- // Simulate the exchange of candidates. |
- CompleteNegotiation(); |
- dest->CompleteNegotiation(); |
- for (TransportMap::const_iterator it = transport_proxies().begin(); |
- it != transport_proxies().end(); ++it) { |
- static_cast<FakeTransport*>(it->second->impl())->SetDestination( |
- dest->GetTransport(it->first)); |
- } |
+ void Connect(FakeTransportController* dest) { |
+ worker_thread()->Invoke<void>( |
+ rtc::Bind(&FakeTransportController::Connect_w, this, dest)); |
} |
- TransportChannel* CreateChannel(const std::string& content_name, |
- int component) override { |
+ TransportChannel* CreateTransportChannel_w(const std::string& transport_name, |
+ int component) override { |
if (fail_create_channel_) { |
- return NULL; |
+ return nullptr; |
} |
- return BaseSession::CreateChannel(content_name, component); |
+ return TransportController::CreateTransportChannel_w(transport_name, |
+ component); |
} |
void set_fail_channel_creation(bool fail_channel_creation) { |
fail_create_channel_ = fail_channel_creation; |
} |
- // TODO: Hoist this into Session when we re-work the Session code. |
- void set_ssl_rtccertificate( |
- const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
- for (TransportMap::const_iterator it = transport_proxies().begin(); |
- it != transport_proxies().end(); ++it) { |
- // We know that we have a FakeTransport* |
- |
- static_cast<FakeTransport*>(it->second->impl())->set_certificate |
- (certificate); |
- } |
+ protected: |
+ Transport* CreateTransport_w(const std::string& transport_name) override { |
+ return new FakeTransport(transport_name); |
} |
- protected: |
- Transport* CreateTransport(const std::string& content_name) override { |
- return new FakeTransport(signaling_thread(), worker_thread(), content_name); |
+ void Connect_w(FakeTransportController* dest) { |
+ // Simulate the exchange of candidates. |
+ ConnectChannels_w(); |
+ dest->ConnectChannels_w(); |
+ for (auto& kv : transports()) { |
+ FakeTransport* transport = static_cast<FakeTransport*>(kv.second); |
+ transport->SetDestination(dest->GetTransport_w(kv.first)); |
+ } |
} |
- void CompleteNegotiation() { |
- for (TransportMap::const_iterator it = transport_proxies().begin(); |
- it != transport_proxies().end(); ++it) { |
- it->second->CompleteNegotiation(); |
- it->second->ConnectChannels(); |
+ void ConnectChannels_w() { |
+ for (auto& kv : transports()) { |
+ FakeTransport* transport = static_cast<FakeTransport*>(kv.second); |
+ transport->ConnectChannels(); |
+ transport->MaybeStartGathering(); |
} |
} |
@@ -503,4 +534,4 @@ class FakeSession : public BaseSession { |
} // namespace cricket |
-#endif // WEBRTC_P2P_BASE_FAKESESSION_H_ |
+#endif // WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |