| Index: webrtc/p2p/base/faketransportcontroller.h
|
| diff --git a/webrtc/p2p/base/fakesession.h b/webrtc/p2p/base/faketransportcontroller.h
|
| similarity index 66%
|
| rename from webrtc/p2p/base/fakesession.h
|
| rename to webrtc/p2p/base/faketransportcontroller.h
|
| index 67b770ae5c1236b04e2869e01aaa27015f9769ec..8066baa0e495759f531a41b8b920be79ad4c29f4 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;
|
| };
|
|
|
| @@ -46,22 +47,20 @@ class FakeTransportChannel : public TransportChannelImpl,
|
| int component)
|
| : TransportChannelImpl(content_name, component),
|
| transport_(transport),
|
| - dest_(NULL),
|
| + dest_(nullptr),
|
| state_(STATE_INIT),
|
| async_(false),
|
| - identity_(NULL),
|
| + identity_(nullptr),
|
| do_dtls_(false),
|
| role_(ICEROLE_UNKNOWN),
|
| tiebreaker_(0),
|
| ice_proto_(ICEPROTO_HYBRID),
|
| remote_ice_mode_(ICEMODE_FULL),
|
| - dtls_fingerprint_("", NULL, 0),
|
| + dtls_fingerprint_("", nullptr, 0),
|
| ssl_role_(rtc::SSL_CLIENT),
|
| - connection_count_(0) {
|
| - }
|
| - ~FakeTransportChannel() {
|
| - Reset();
|
| - }
|
| + connection_count_(0),
|
| + gathering_state_(kGatheringNew) {}
|
| + ~FakeTransportChannel() { Reset(); }
|
|
|
| uint64 IceTiebreaker() const { return tiebreaker_; }
|
| TransportProtocol protocol() const { return ice_proto_; }
|
| @@ -74,13 +73,9 @@ class FakeTransportChannel : public TransportChannelImpl,
|
| return dtls_fingerprint_;
|
| }
|
|
|
| - void SetAsync(bool async) {
|
| - async_ = async;
|
| - }
|
| + void SetAsync(bool async) { async_ = async; }
|
|
|
| - virtual Transport* GetTransport() {
|
| - return transport_;
|
| - }
|
| + virtual Transport* GetTransport() { return transport_; }
|
|
|
| virtual TransportChannelState GetState() const {
|
| if (connection_count_ == 0) {
|
| @@ -114,7 +109,8 @@ class FakeTransportChannel : public TransportChannelImpl,
|
| }
|
|
|
| virtual void SetRemoteIceMode(IceMode mode) { remote_ice_mode_ = mode; }
|
| - virtual bool SetRemoteFingerprint(const std::string& alg, const uint8* digest,
|
| + virtual bool SetRemoteFingerprint(const std::string& alg,
|
| + const uint8* digest,
|
| size_t digest_len) {
|
| dtls_fingerprint_ = rtc::SSLFingerprint(alg, digest, digest_len);
|
| return true;
|
| @@ -133,20 +129,21 @@ class FakeTransportChannel : public TransportChannelImpl,
|
| state_ = STATE_CONNECTING;
|
| }
|
| }
|
| +
|
| + virtual GatheringState gathering_state() const { return gathering_state_; }
|
| +
|
| virtual 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) {
|
| @@ -164,7 +161,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);
|
| }
|
| @@ -177,14 +174,21 @@ class FakeTransportChannel : public TransportChannelImpl,
|
| SignalConnectionRemoved(this);
|
| }
|
|
|
| - void SetReceiving(bool receiving) {
|
| - set_receiving(receiving);
|
| + void SetCandidatesAllocationDone() {
|
| + if (gathering_state_ != kGatheringComplete) {
|
| + gathering_state_ = kGatheringComplete;
|
| + SignalGatheringState(this);
|
| + }
|
| }
|
|
|
| + void SetReceiving(bool receiving) { set_receiving(receiving); }
|
| +
|
| void SetReceivingTimeout(int timeout) override {}
|
|
|
| - virtual int SendPacket(const char* data, size_t len,
|
| - const rtc::PacketOptions& options, int flags) {
|
| + virtual int SendPacket(const char* data,
|
| + size_t len,
|
| + const rtc::PacketOptions& options,
|
| + int flags) {
|
| if (state_ != STATE_CONNECTED) {
|
| return -1;
|
| }
|
| @@ -201,24 +205,14 @@ class FakeTransportChannel : public TransportChannelImpl,
|
| }
|
| return static_cast<int>(len);
|
| }
|
| - virtual int SetOption(rtc::Socket::Option opt, int value) {
|
| - return true;
|
| - }
|
| - virtual bool GetOption(rtc::Socket::Option opt, int* value) {
|
| - return true;
|
| - }
|
| - virtual int GetError() {
|
| - return 0;
|
| - }
|
| + virtual int SetOption(rtc::Socket::Option opt, int value) { return true; }
|
| + virtual bool GetOption(rtc::Socket::Option opt, int* value) { return true; }
|
| + virtual int GetError() { return 0; }
|
|
|
| - virtual void OnSignalingReady() {
|
| - }
|
| - virtual void OnCandidate(const Candidate& candidate) {
|
| - }
|
| + virtual void OnCandidate(const Candidate& candidate) {}
|
|
|
| virtual void OnMessage(rtc::Message* msg) {
|
| - 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;
|
| @@ -229,14 +223,11 @@ class FakeTransportChannel : public TransportChannelImpl,
|
| return true;
|
| }
|
|
|
| -
|
| void SetRemoteCertificate(rtc::FakeSSLCertificate* cert) {
|
| remote_cert_ = cert;
|
| }
|
|
|
| - virtual bool IsDtlsActive() const {
|
| - return do_dtls_;
|
| - }
|
| + virtual bool IsDtlsActive() const { return do_dtls_; }
|
|
|
| virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers) {
|
| srtp_ciphers_ = ciphers;
|
| @@ -251,9 +242,7 @@ class FakeTransportChannel : public TransportChannelImpl,
|
| return false;
|
| }
|
|
|
| - virtual bool GetSslCipher(std::string* cipher) {
|
| - return false;
|
| - }
|
| + virtual bool GetSslCipher(std::string* cipher) { return false; }
|
|
|
| virtual bool GetLocalIdentity(rtc::SSLIdentity** identity) const {
|
| if (!identity_)
|
| @@ -287,10 +276,10 @@ class FakeTransportChannel : public TransportChannelImpl,
|
|
|
| virtual 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;
|
| @@ -329,6 +318,7 @@ class FakeTransportChannel : public TransportChannelImpl,
|
| rtc::SSLFingerprint dtls_fingerprint_;
|
| rtc::SSLRole ssl_role_;
|
| size_t connection_count_;
|
| + GatheringState gathering_state_;
|
| };
|
|
|
| // Fake transport class, which can be passed to anything that needs a Transport.
|
| @@ -337,41 +327,39 @@ 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 = NULL)
|
| - : Transport(signaling_thread, worker_thread,
|
| - content_name, "test_type", NULL),
|
| - dest_(NULL),
|
| - async_(false),
|
| - identity_(NULL) {
|
| - }
|
| - ~FakeTransport() {
|
| - DestroyAllChannels();
|
| - }
|
| + FakeTransport(const std::string& content_name,
|
| + PortAllocator* alllocator = nullptr)
|
| + : Transport(content_name, "test_type", nullptr),
|
| + dest_(nullptr),
|
| + async_(false),
|
| + identity_(nullptr) {}
|
| +
|
| + ~FakeTransport() { DestroyAllChannels(); }
|
|
|
| const ChannelMap& channels() const { return channels_; }
|
|
|
| void SetAsync(bool async) { async_ = async; }
|
| void SetDestination(FakeTransport* dest) {
|
| dest_ = dest;
|
| - for (ChannelMap::iterator it = channels_.begin(); it != channels_.end();
|
| - ++it) {
|
| - it->second->SetLocalIdentity(identity_);
|
| - SetChannelDestination(it->first, it->second);
|
| + for (const auto& kv : channels_) {
|
| + kv.second->SetLocalIdentity(identity_);
|
| + 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_identity(rtc::SSLIdentity* identity) {
|
| - identity_ = identity;
|
| + virtual void SetIdentity(rtc::SSLIdentity* identity) { identity_ = identity; }
|
| + virtual bool GetIdentity(rtc::SSLIdentity** identity) {
|
| + if (!identity_)
|
| + return false;
|
| +
|
| + *identity = identity_->GetReference();
|
| + return true;
|
| }
|
|
|
| using Transport::local_description;
|
| @@ -380,7 +368,7 @@ class FakeTransport : public Transport {
|
| protected:
|
| virtual TransportChannelImpl* CreateTransportChannel(int component) {
|
| if (channels_.find(component) != channels_.end()) {
|
| - return NULL;
|
| + return nullptr;
|
| }
|
| FakeTransportChannel* channel =
|
| new FakeTransportChannel(this, content_name(), component);
|
| @@ -389,29 +377,20 @@ class FakeTransport : public Transport {
|
| channels_[component] = channel;
|
| return channel;
|
| }
|
| +
|
| virtual void DestroyTransportChannel(TransportChannelImpl* channel) {
|
| channels_.erase(channel->component());
|
| delete channel;
|
| }
|
| - virtual void SetIdentity_w(rtc::SSLIdentity* identity) {
|
| - identity_ = identity;
|
| - }
|
| - virtual bool GetIdentity_w(rtc::SSLIdentity** identity) {
|
| - if (!identity_)
|
| - return false;
|
| -
|
| - *identity = identity_->GetReference();
|
| - 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) {
|
| @@ -423,85 +402,82 @@ class FakeTransport : public Transport {
|
|
|
| // 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_;
|
| rtc::SSLIdentity* identity_;
|
| };
|
|
|
| -// 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().
|
| +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(bool initiator)
|
| + : TransportController(rtc::Thread::Current(),
|
| + rtc::Thread::Current(),
|
| + nullptr),
|
| + fail_create_channel_(false) {
|
| + SetIceRole(initiator ? ICEROLE_CONTROLLING : ICEROLE_CONTROLLED);
|
| + }
|
| +
|
| + FakeTransportController(rtc::Thread* worker_thread, bool initiator)
|
| + : TransportController(rtc::Thread::Current(), worker_thread, nullptr),
|
| + fail_create_channel_(false) {
|
| + SetIceRole(initiator ? ICEROLE_CONTROLLING : ICEROLE_CONTROLLED);
|
| + }
|
| +
|
| + 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));
|
| }
|
|
|
| - virtual TransportChannel* CreateChannel(
|
| - const std::string& content_name,
|
| + virtual TransportChannel* CreateTransportChannel_w(
|
| + const std::string& transport_name,
|
| int component) {
|
| 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_identity(rtc::SSLIdentity* identity) {
|
| - 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_identity
|
| - (identity);
|
| - }
|
| + protected:
|
| + virtual Transport* CreateTransport_w(const std::string& transport_name) {
|
| + return new FakeTransport(transport_name);
|
| }
|
|
|
| - protected:
|
| - virtual Transport* CreateTransport(const std::string& content_name) {
|
| - 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();
|
| }
|
| }
|
|
|
| @@ -511,4 +487,4 @@ class FakeSession : public BaseSession {
|
|
|
| } // namespace cricket
|
|
|
| -#endif // WEBRTC_P2P_BASE_FAKESESSION_H_
|
| +#endif // WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_
|
|
|