OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2010 The WebRTC Project Authors. All rights reserved. |
| 3 * |
| 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 |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #ifndef WEBRTC_P2P_CLIENT_FAKEPORTALLOCATOR_H_ |
| 12 #define WEBRTC_P2P_CLIENT_FAKEPORTALLOCATOR_H_ |
| 13 |
| 14 #include <memory> |
| 15 #include <string> |
| 16 |
| 17 #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
| 18 #include "webrtc/p2p/base/portallocator.h" |
| 19 #include "webrtc/p2p/base/udpport.h" |
| 20 #include "webrtc/base/scoped_ptr.h" |
| 21 |
| 22 namespace rtc { |
| 23 class SocketFactory; |
| 24 class Thread; |
| 25 } |
| 26 |
| 27 namespace cricket { |
| 28 |
| 29 class TestUDPPort : public UDPPort { |
| 30 public: |
| 31 static TestUDPPort* Create(rtc::Thread* thread, |
| 32 rtc::PacketSocketFactory* factory, |
| 33 rtc::Network* network, |
| 34 const rtc::IPAddress& ip, |
| 35 uint16_t min_port, |
| 36 uint16_t max_port, |
| 37 const std::string& username, |
| 38 const std::string& password, |
| 39 const std::string& origin, |
| 40 bool emit_localhost_for_anyaddress) { |
| 41 TestUDPPort* port = new TestUDPPort(thread, factory, network, ip, min_port, |
| 42 max_port, username, password, origin, |
| 43 emit_localhost_for_anyaddress); |
| 44 if (!port->Init()) { |
| 45 delete port; |
| 46 port = nullptr; |
| 47 } |
| 48 return port; |
| 49 } |
| 50 void SendBindingResponse(StunMessage* request, |
| 51 const rtc::SocketAddress& addr) override { |
| 52 UDPPort::SendBindingResponse(request, addr); |
| 53 sent_binding_response_ = true; |
| 54 } |
| 55 bool sent_binding_response() { return sent_binding_response_; } |
| 56 void set_sent_binding_response(bool response) { |
| 57 sent_binding_response_ = response; |
| 58 } |
| 59 |
| 60 protected: |
| 61 TestUDPPort(rtc::Thread* thread, |
| 62 rtc::PacketSocketFactory* factory, |
| 63 rtc::Network* network, |
| 64 const rtc::IPAddress& ip, |
| 65 uint16_t min_port, |
| 66 uint16_t max_port, |
| 67 const std::string& username, |
| 68 const std::string& password, |
| 69 const std::string& origin, |
| 70 bool emit_localhost_for_anyaddress) |
| 71 : UDPPort(thread, |
| 72 factory, |
| 73 network, |
| 74 ip, |
| 75 min_port, |
| 76 max_port, |
| 77 username, |
| 78 password, |
| 79 origin, |
| 80 emit_localhost_for_anyaddress) {} |
| 81 |
| 82 bool sent_binding_response_ = false; |
| 83 }; |
| 84 |
| 85 class FakePortAllocatorSession : public PortAllocatorSession { |
| 86 public: |
| 87 FakePortAllocatorSession(rtc::Thread* worker_thread, |
| 88 rtc::PacketSocketFactory* factory, |
| 89 const std::string& content_name, |
| 90 int component, |
| 91 const std::string& ice_ufrag, |
| 92 const std::string& ice_pwd) |
| 93 : PortAllocatorSession(content_name, component, ice_ufrag, ice_pwd, |
| 94 cricket::kDefaultPortAllocatorFlags), |
| 95 worker_thread_(worker_thread), |
| 96 factory_(factory), |
| 97 network_("network", "unittest", |
| 98 rtc::IPAddress(INADDR_LOOPBACK), 8), |
| 99 port_(), running_(false), |
| 100 port_config_count_(0) { |
| 101 network_.AddIP(rtc::IPAddress(INADDR_LOOPBACK)); |
| 102 } |
| 103 |
| 104 virtual void StartGettingPorts() { |
| 105 if (!port_) { |
| 106 port_.reset(TestUDPPort::Create(worker_thread_, factory_, &network_, |
| 107 network_.GetBestIP(), 0, 0, username(), |
| 108 password(), std::string(), false)); |
| 109 AddPort(port_.get()); |
| 110 } |
| 111 ++port_config_count_; |
| 112 running_ = true; |
| 113 } |
| 114 |
| 115 virtual void StopGettingPorts() { running_ = false; } |
| 116 virtual bool IsGettingPorts() { return running_; } |
| 117 virtual void ClearGettingPorts() {} |
| 118 |
| 119 int port_config_count() { return port_config_count_; } |
| 120 |
| 121 void AddPort(cricket::Port* port) { |
| 122 port->set_component(component_); |
| 123 port->set_generation(generation()); |
| 124 port->SignalPortComplete.connect( |
| 125 this, &FakePortAllocatorSession::OnPortComplete); |
| 126 port->PrepareAddress(); |
| 127 SignalPortReady(this, port); |
| 128 } |
| 129 void OnPortComplete(cricket::Port* port) { |
| 130 SignalCandidatesReady(this, port->Candidates()); |
| 131 SignalCandidatesAllocationDone(this); |
| 132 } |
| 133 |
| 134 private: |
| 135 rtc::Thread* worker_thread_; |
| 136 rtc::PacketSocketFactory* factory_; |
| 137 rtc::Network network_; |
| 138 std::unique_ptr<cricket::Port> port_; |
| 139 bool running_; |
| 140 int port_config_count_; |
| 141 }; |
| 142 |
| 143 class FakePortAllocator : public cricket::PortAllocator { |
| 144 public: |
| 145 FakePortAllocator(rtc::Thread* worker_thread, |
| 146 rtc::PacketSocketFactory* factory) |
| 147 : worker_thread_(worker_thread), factory_(factory) { |
| 148 if (factory_ == NULL) { |
| 149 owned_factory_.reset(new rtc::BasicPacketSocketFactory( |
| 150 worker_thread_)); |
| 151 factory_ = owned_factory_.get(); |
| 152 } |
| 153 } |
| 154 |
| 155 void SetIceServers( |
| 156 const ServerAddresses& stun_servers, |
| 157 const std::vector<RelayServerConfig>& turn_servers) override { |
| 158 stun_servers_ = stun_servers; |
| 159 turn_servers_ = turn_servers; |
| 160 } |
| 161 |
| 162 void SetNetworkIgnoreMask(int network_ignore_mask) override {} |
| 163 |
| 164 const ServerAddresses& stun_servers() const { return stun_servers_; } |
| 165 |
| 166 const std::vector<RelayServerConfig>& turn_servers() const { |
| 167 return turn_servers_; |
| 168 } |
| 169 |
| 170 virtual cricket::PortAllocatorSession* CreateSessionInternal( |
| 171 const std::string& content_name, |
| 172 int component, |
| 173 const std::string& ice_ufrag, |
| 174 const std::string& ice_pwd) override { |
| 175 return new FakePortAllocatorSession( |
| 176 worker_thread_, factory_, content_name, component, ice_ufrag, ice_pwd); |
| 177 } |
| 178 |
| 179 private: |
| 180 rtc::Thread* worker_thread_; |
| 181 rtc::PacketSocketFactory* factory_; |
| 182 std::unique_ptr<rtc::BasicPacketSocketFactory> owned_factory_; |
| 183 ServerAddresses stun_servers_; |
| 184 std::vector<RelayServerConfig> turn_servers_; |
| 185 }; |
| 186 |
| 187 } // namespace cricket |
| 188 |
| 189 #endif // WEBRTC_P2P_CLIENT_FAKEPORTALLOCATOR_H_ |
OLD | NEW |