OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 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_PORTALLOCATOR_H_ | 11 #ifndef WEBRTC_P2P_BASE_PORTALLOCATOR_H_ |
12 #define WEBRTC_P2P_BASE_PORTALLOCATOR_H_ | 12 #define WEBRTC_P2P_BASE_PORTALLOCATOR_H_ |
13 | 13 |
14 #include <deque> | |
15 #include <memory> | |
14 #include <string> | 16 #include <string> |
15 #include <vector> | 17 #include <vector> |
16 | 18 |
17 #include "webrtc/p2p/base/port.h" | 19 #include "webrtc/p2p/base/port.h" |
18 #include "webrtc/p2p/base/portinterface.h" | 20 #include "webrtc/p2p/base/portinterface.h" |
19 #include "webrtc/base/helpers.h" | 21 #include "webrtc/base/helpers.h" |
20 #include "webrtc/base/proxyinfo.h" | 22 #include "webrtc/base/proxyinfo.h" |
21 #include "webrtc/base/sigslot.h" | 23 #include "webrtc/base/sigslot.h" |
24 #include "webrtc/base/thread.h" | |
22 | 25 |
23 namespace cricket { | 26 namespace cricket { |
24 | 27 |
25 // PortAllocator is responsible for allocating Port types for a given | 28 // PortAllocator is responsible for allocating Port types for a given |
26 // P2PSocket. It also handles port freeing. | 29 // P2PSocket. It also handles port freeing. |
27 // | 30 // |
28 // Clients can override this class to control port allocation, including | 31 // Clients can override this class to control port allocation, including |
29 // what kinds of ports are allocated. | 32 // what kinds of ports are allocated. |
30 | 33 |
31 enum { | 34 enum { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 CF_RELAY = 0x4, | 78 CF_RELAY = 0x4, |
76 CF_ALL = 0x7, | 79 CF_ALL = 0x7, |
77 }; | 80 }; |
78 | 81 |
79 // TODO(deadbeef): Rename to TurnCredentials (and username to ufrag). | 82 // TODO(deadbeef): Rename to TurnCredentials (and username to ufrag). |
80 struct RelayCredentials { | 83 struct RelayCredentials { |
81 RelayCredentials() {} | 84 RelayCredentials() {} |
82 RelayCredentials(const std::string& username, const std::string& password) | 85 RelayCredentials(const std::string& username, const std::string& password) |
83 : username(username), password(password) {} | 86 : username(username), password(password) {} |
84 | 87 |
88 bool operator==(const RelayCredentials& o) const { | |
89 return username == o.username && password == o.password; | |
90 } | |
91 bool operator!=(const RelayCredentials& o) const { return !(*this == o); } | |
92 | |
85 std::string username; | 93 std::string username; |
86 std::string password; | 94 std::string password; |
87 }; | 95 }; |
88 | 96 |
89 typedef std::vector<ProtocolAddress> PortList; | 97 typedef std::vector<ProtocolAddress> PortList; |
90 // TODO(deadbeef): Rename to TurnServerConfig. | 98 // TODO(deadbeef): Rename to TurnServerConfig. |
91 struct RelayServerConfig { | 99 struct RelayServerConfig { |
92 RelayServerConfig(RelayType type) : type(type), priority(0) {} | 100 RelayServerConfig(RelayType type) : type(type), priority(0) {} |
93 | 101 |
94 RelayServerConfig(const std::string& address, | 102 RelayServerConfig(const std::string& address, |
95 int port, | 103 int port, |
96 const std::string& username, | 104 const std::string& username, |
97 const std::string& password, | 105 const std::string& password, |
98 ProtocolType proto, | 106 ProtocolType proto, |
99 bool secure) | 107 bool secure) |
100 : type(RELAY_TURN), credentials(username, password) { | 108 : type(RELAY_TURN), credentials(username, password) { |
101 ports.push_back( | 109 ports.push_back( |
102 ProtocolAddress(rtc::SocketAddress(address, port), proto, secure)); | 110 ProtocolAddress(rtc::SocketAddress(address, port), proto, secure)); |
103 } | 111 } |
104 | 112 |
113 bool operator==(const RelayServerConfig& o) const { | |
114 return type == o.type && ports == o.ports && credentials == o.credentials && | |
115 priority == o.priority; | |
116 } | |
117 bool operator!=(const RelayServerConfig& o) const { return !(*this == o); } | |
118 | |
105 RelayType type; | 119 RelayType type; |
106 PortList ports; | 120 PortList ports; |
107 RelayCredentials credentials; | 121 RelayCredentials credentials; |
108 int priority; | 122 int priority; |
109 }; | 123 }; |
110 | 124 |
111 class PortAllocatorSession : public sigslot::has_slots<> { | 125 class PortAllocatorSession : public sigslot::has_slots<> { |
112 public: | 126 public: |
113 // Content name passed in mostly for logging and debugging. | 127 // Content name passed in mostly for logging and debugging. |
114 PortAllocatorSession(const std::string& content_name, | 128 PortAllocatorSession(const std::string& content_name, |
115 int component, | 129 int component, |
116 const std::string& ice_ufrag, | 130 const std::string& ice_ufrag, |
117 const std::string& ice_pwd, | 131 const std::string& ice_pwd, |
118 uint32_t flags); | 132 uint32_t flags); |
119 | 133 |
120 // Subclasses should clean up any ports created. | 134 // Subclasses should clean up any ports created. |
121 virtual ~PortAllocatorSession() {} | 135 virtual ~PortAllocatorSession() {} |
122 | 136 |
123 uint32_t flags() const { return flags_; } | 137 uint32_t flags() const { return flags_; } |
124 void set_flags(uint32_t flags) { flags_ = flags; } | 138 void set_flags(uint32_t flags) { flags_ = flags; } |
125 std::string content_name() const { return content_name_; } | 139 std::string content_name() const { return content_name_; } |
126 int component() const { return component_; } | 140 int component() const { return component_; } |
141 const std::string& ice_ufrag() const { return ice_ufrag_; } | |
142 const std::string& ice_pwd() const { return ice_pwd_; } | |
127 | 143 |
128 // Starts gathering STUN and Relay configurations. | 144 // Starts gathering STUN and Relay configurations. |
129 virtual void StartGettingPorts() = 0; | 145 virtual void StartGettingPorts() = 0; |
130 virtual void StopGettingPorts() = 0; | 146 virtual void StopGettingPorts() = 0; |
131 // Only stop the existing gathering process but may start new ones if needed. | 147 // Only stop the existing gathering process but may start new ones if needed. |
132 virtual void ClearGettingPorts() = 0; | 148 virtual void ClearGettingPorts() = 0; |
133 // Whether the process of getting ports has been stopped. | 149 // Whether the process of getting ports has been stopped. |
134 virtual bool IsGettingPorts() = 0; | 150 virtual bool IsGettingPorts() = 0; |
135 | 151 |
152 // Another way of getting the information provided by the signals below. | |
153 // | |
154 // Ports and candidates are not guaranteed to be in the same order as the | |
155 // signals were emitted in. | |
156 virtual std::vector<PortInterface*> ReadyPorts() const = 0; | |
157 virtual std::vector<Candidate> ReadyCandidates() const = 0; | |
158 virtual bool CandidatesAllocationDone() const = 0; | |
159 | |
136 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady; | 160 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady; |
137 sigslot::signal2<PortAllocatorSession*, | 161 sigslot::signal2<PortAllocatorSession*, |
138 const std::vector<Candidate>&> SignalCandidatesReady; | 162 const std::vector<Candidate>&> SignalCandidatesReady; |
139 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone; | 163 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone; |
140 | 164 |
141 virtual uint32_t generation() { return generation_; } | 165 virtual uint32_t generation() { return generation_; } |
142 virtual void set_generation(uint32_t generation) { generation_ = generation; } | 166 virtual void set_generation(uint32_t generation) { generation_ = generation; } |
143 sigslot::signal1<PortAllocatorSession*> SignalDestroyed; | 167 sigslot::signal1<PortAllocatorSession*> SignalDestroyed; |
144 | 168 |
145 const std::string& ice_ufrag() const { return ice_ufrag_; } | 169 protected: |
146 const std::string& ice_pwd() const { return ice_pwd_; } | 170 // This method is called when a pooled session (which doesn't have these |
171 // properties initially) is returned by PortAllocator::GetPooledSession, | |
172 // and the content name, component, and ICE ufrag/pwd are updated. | |
173 // | |
174 // A subclass may need to override this method to perform additional actions, | |
175 // such as applying the updated information to ports and candidates. | |
176 virtual void UpdateTransportInformationInternal() {} | |
pthatcher1
2016/05/05 21:51:25
And here, SetIceParametersInternal might be a bett
Taylor Brandstetter
2016/05/06 03:53:35
I don't like saying "if a derived class overrides
| |
147 | 177 |
148 protected: | |
149 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and | 178 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and |
150 // ice_pwd. | 179 // ice_pwd. |
151 const std::string& username() const { return ice_ufrag_; } | 180 const std::string& username() const { return ice_ufrag_; } |
152 const std::string& password() const { return ice_pwd_; } | 181 const std::string& password() const { return ice_pwd_; } |
153 | 182 |
183 private: | |
184 void SetTransportInformation(const std::string& content_name, | |
185 int component, | |
186 const std::string& ice_ufrag, | |
187 const std::string& ice_pwd) { | |
188 content_name_ = content_name; | |
189 component_ = component; | |
190 ice_ufrag_ = ice_ufrag; | |
191 ice_pwd_ = ice_pwd; | |
192 UpdateTransportInformationInternal(); | |
193 } | |
194 | |
195 uint32_t flags_; | |
196 uint32_t generation_; | |
154 std::string content_name_; | 197 std::string content_name_; |
155 int component_; | 198 int component_; |
156 | |
157 private: | |
158 uint32_t flags_; | |
159 uint32_t generation_; | |
160 std::string ice_ufrag_; | 199 std::string ice_ufrag_; |
161 std::string ice_pwd_; | 200 std::string ice_pwd_; |
201 | |
202 friend class PortAllocator; | |
pthatcher1
2016/05/05 21:51:25
Why do we need this?
Taylor Brandstetter
2016/05/06 03:53:34
SetTransportInformation is private. Only PortAlloc
pthatcher1
2016/05/07 00:21:44
Can you put a comment explaining this?
And... is
Taylor Brandstetter
2016/05/09 22:07:10
This method can be either an implementation detail
| |
162 }; | 203 }; |
163 | 204 |
205 // Note that all methods of this class that deal with sessions (such as | |
206 // CreateSession, SetConfiguration, GetPooledSession, and the destructor) should | |
207 // be called on the same thread. Other methods don't have this restriction, | |
208 // but they aren't guaranteed to be synchronized. | |
pthatcher1
2016/05/05 21:51:25
As mentioned in another place, I think we should e
Taylor Brandstetter
2016/05/06 03:53:35
I was just documenting the behavior we previously
| |
164 class PortAllocator : public sigslot::has_slots<> { | 209 class PortAllocator : public sigslot::has_slots<> { |
165 public: | 210 public: |
166 PortAllocator() : | 211 PortAllocator() : |
167 flags_(kDefaultPortAllocatorFlags), | 212 flags_(kDefaultPortAllocatorFlags), |
168 min_port_(0), | 213 min_port_(0), |
169 max_port_(0), | 214 max_port_(0), |
170 step_delay_(kDefaultStepDelay), | 215 step_delay_(kDefaultStepDelay), |
171 allow_tcp_listen_(true), | 216 allow_tcp_listen_(true), |
172 candidate_filter_(CF_ALL) { | 217 candidate_filter_(CF_ALL) { |
173 // This will allow us to have old behavior on non webrtc clients. | 218 // This will allow us to have old behavior on non webrtc clients. |
174 } | 219 } |
175 virtual ~PortAllocator() {} | 220 virtual ~PortAllocator() {} |
176 | 221 |
177 // Set STUN and TURN servers to be used in future sessions. | 222 // Set STUN and TURN servers to be used in future sessions, and set |
178 virtual void SetIceServers( | 223 // candidate pool size, as described in JSEP. |
179 const ServerAddresses& stun_servers, | 224 // |
180 const std::vector<RelayServerConfig>& turn_servers) = 0; | 225 // If the servers are changing and the candidate pool size is nonzero, |
226 // existing pooled sessions will be destroyed and new ones created. | |
227 // | |
228 // If the servers are not changing but the candidate pool size is, | |
229 // pooled sessions will be either created or destroyed as necessary. | |
230 void SetConfiguration(const ServerAddresses& stun_servers, | |
231 const std::vector<RelayServerConfig>& turn_servers, | |
232 int candidate_pool_size); | |
233 | |
234 const ServerAddresses& stun_servers() const { return stun_servers_; } | |
235 | |
236 const std::vector<RelayServerConfig>& turn_servers() const { | |
237 return turn_servers_; | |
238 } | |
239 | |
240 int candidate_pool_size() const { return candidate_pool_size_; } | |
181 | 241 |
182 // Sets the network types to ignore. | 242 // Sets the network types to ignore. |
183 // Values are defined by the AdapterType enum. | 243 // Values are defined by the AdapterType enum. |
184 // For instance, calling this with | 244 // For instance, calling this with |
185 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and | 245 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and |
186 // loopback interfaces. | 246 // loopback interfaces. |
187 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0; | 247 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0; |
188 | 248 |
189 PortAllocatorSession* CreateSession( | 249 PortAllocatorSession* CreateSession( |
190 const std::string& sid, | 250 const std::string& sid, |
191 const std::string& content_name, | 251 const std::string& content_name, |
192 int component, | 252 int component, |
193 const std::string& ice_ufrag, | 253 const std::string& ice_ufrag, |
194 const std::string& ice_pwd); | 254 const std::string& ice_pwd); |
195 | 255 |
256 // Get an available pooled session and set the transport information on it. | |
257 // | |
258 // Caller takes ownership of the returned session. | |
259 // | |
260 // If no pooled sessions are available, returns null. | |
261 PortAllocatorSession* GetPooledSession(const std::string& content_name, | |
262 int component, | |
263 const std::string& ice_ufrag, | |
264 const std::string& ice_pwd); | |
pthatcher1
2016/05/05 21:51:25
If it takes ownership, should it return a unique_p
Taylor Brandstetter
2016/05/06 03:53:34
I was just matching CreateSession (which also retu
pthatcher1
2016/05/07 00:21:44
I can see the merit in "PopPooledSession + SetIceP
| |
265 | |
266 // Returns the next session that would be returned by GetPooledSession. | |
267 const PortAllocatorSession* PeekPooledSession() const; | |
pthatcher1
2016/05/05 21:51:25
It seems like this one should be called GetPooledS
Taylor Brandstetter
2016/05/06 03:53:34
"Get/Peek", "Pop/Peek", "Take/Get", I don't care m
pthatcher1
2016/05/07 00:21:44
I think I'd prefer Take/Get
Taylor Brandstetter
2016/05/09 22:07:10
I don't like it much but I'll compromise... Done.
| |
268 | |
196 uint32_t flags() const { return flags_; } | 269 uint32_t flags() const { return flags_; } |
197 void set_flags(uint32_t flags) { flags_ = flags; } | 270 void set_flags(uint32_t flags) { flags_ = flags; } |
198 | 271 |
199 const std::string& user_agent() const { return agent_; } | 272 const std::string& user_agent() const { return agent_; } |
200 const rtc::ProxyInfo& proxy() const { return proxy_; } | 273 const rtc::ProxyInfo& proxy() const { return proxy_; } |
201 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) { | 274 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) { |
202 agent_ = agent; | 275 agent_ = agent; |
203 proxy_ = proxy; | 276 proxy_ = proxy; |
204 } | 277 } |
205 | 278 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 | 317 |
245 uint32_t flags_; | 318 uint32_t flags_; |
246 std::string agent_; | 319 std::string agent_; |
247 rtc::ProxyInfo proxy_; | 320 rtc::ProxyInfo proxy_; |
248 int min_port_; | 321 int min_port_; |
249 int max_port_; | 322 int max_port_; |
250 uint32_t step_delay_; | 323 uint32_t step_delay_; |
251 bool allow_tcp_listen_; | 324 bool allow_tcp_listen_; |
252 uint32_t candidate_filter_; | 325 uint32_t candidate_filter_; |
253 std::string origin_; | 326 std::string origin_; |
327 | |
328 private: | |
329 ServerAddresses stun_servers_; | |
330 std::vector<RelayServerConfig> turn_servers_; | |
331 // The last size passed into SetConfiguration. | |
332 int candidate_pool_size_ = 0; | |
333 // This variable represents the total number of pooled sessions | |
334 // both owned by this class and taken by GetPooledSession. | |
335 int allocated_pooled_sessions_ = 0; | |
pthatcher1
2016/05/05 21:51:25
Can we call these two numbers something similar, l
Taylor Brandstetter
2016/05/06 03:53:34
It represents the number of currently pooled sessi
pthatcher1
2016/05/07 00:21:44
OK, I get it now. Yes, your comment is good alrea
| |
336 std::deque<std::unique_ptr<PortAllocatorSession>> pooled_sessions_; | |
254 }; | 337 }; |
255 | 338 |
256 } // namespace cricket | 339 } // namespace cricket |
257 | 340 |
258 #endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_ | 341 #endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_ |
OLD | NEW |