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 |
(...skipping 12 matching lines...) Expand all Loading... | |
23 namespace rtc { | 23 namespace rtc { |
24 | 24 |
25 class Packet; | 25 class Packet; |
26 class VirtualSocket; | 26 class VirtualSocket; |
27 class SocketAddressPair; | 27 class SocketAddressPair; |
28 | 28 |
29 // Simulates a network in the same manner as a loopback interface. The | 29 // Simulates a network in the same manner as a loopback interface. The |
30 // interface can create as many addresses as you want. All of the sockets | 30 // interface can create as many addresses as you want. All of the sockets |
31 // created by this network will be able to communicate with one another, unless | 31 // created by this network will be able to communicate with one another, unless |
32 // they are bound to addresses from incompatible families. | 32 // they are bound to addresses from incompatible families. |
33 class VirtualSocketServer : public SocketServer, public sigslot::has_slots<> { | 33 class VirtualSocketServer : public SocketServer, public sigslot::has_slots<> { |
skvlad
2016/08/26 22:39:04
Not related to the code review, more like a genera
Taylor Brandstetter
2016/08/26 23:01:03
The current convention seems to be to put test cod
| |
34 public: | 34 public: |
35 // TODO: Add "owned" parameter. | 35 // TODO: Add "owned" parameter. |
36 // If "owned" is set, the supplied socketserver will be deleted later. | 36 // If "owned" is set, the supplied socketserver will be deleted later. |
37 explicit VirtualSocketServer(SocketServer* ss); | 37 explicit VirtualSocketServer(SocketServer* ss); |
38 ~VirtualSocketServer() override; | 38 ~VirtualSocketServer() override; |
39 | 39 |
40 SocketServer* socketserver() { return server_; } | 40 SocketServer* socketserver() { return server_; } |
41 | 41 |
42 // The default route indicates which local address to use when a socket is | 42 // The default route indicates which local address to use when a socket is |
43 // bound to the 'any' address, e.g. 0.0.0.0. | 43 // bound to the 'any' address, e.g. 0.0.0.0. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 void UpdateDelayDistribution(); | 83 void UpdateDelayDistribution(); |
84 | 84 |
85 // Controls the (uniform) probability that any sent packet is dropped. This | 85 // Controls the (uniform) probability that any sent packet is dropped. This |
86 // is separate from calculations to drop based on queue size. | 86 // is separate from calculations to drop based on queue size. |
87 double drop_probability() { return drop_prob_; } | 87 double drop_probability() { return drop_prob_; } |
88 void set_drop_probability(double drop_prob) { | 88 void set_drop_probability(double drop_prob) { |
89 assert((0 <= drop_prob) && (drop_prob <= 1)); | 89 assert((0 <= drop_prob) && (drop_prob <= 1)); |
90 drop_prob_ = drop_prob; | 90 drop_prob_ = drop_prob; |
91 } | 91 } |
92 | 92 |
93 // If |blocked| is true, subsequent attempts to send will result in -1 being | |
94 // returned, with the socket error set to EWOULDBLOCK. | |
95 // | |
96 // If this method is later called with |blocked| set to false, any sockets | |
97 // that previously failed to send with EWOULDBLOCK will emit SignalWriteEvent. | |
98 // | |
99 // This can be used to simulate the send buffer on a network interface being | |
100 // full, and test functionality related to EWOULDBLOCK/SignalWriteEvent. | |
101 void SetSendingBlocked(bool blocked); | |
102 | |
93 // SocketFactory: | 103 // SocketFactory: |
94 Socket* CreateSocket(int type) override; | 104 Socket* CreateSocket(int type) override; |
95 Socket* CreateSocket(int family, int type) override; | 105 Socket* CreateSocket(int family, int type) override; |
96 | 106 |
97 AsyncSocket* CreateAsyncSocket(int type) override; | 107 AsyncSocket* CreateAsyncSocket(int type) override; |
98 AsyncSocket* CreateAsyncSocket(int family, int type) override; | 108 AsyncSocket* CreateAsyncSocket(int family, int type) override; |
99 | 109 |
100 // SocketServer: | 110 // SocketServer: |
101 void SetMessageQueue(MessageQueue* queue) override; | 111 void SetMessageQueue(MessageQueue* queue) override; |
102 bool Wait(int cms, bool process_io) override; | 112 bool Wait(int cms, bool process_io) override; |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 // This applies even if the IPv4 address is 0.0.0.0. | 226 // This applies even if the IPv4 address is 0.0.0.0. |
217 // The socket arguments are optional; the sockets are checked to see if they | 227 // The socket arguments are optional; the sockets are checked to see if they |
218 // were explicitly bound to IPv6-any ('::'), and if so communication is | 228 // were explicitly bound to IPv6-any ('::'), and if so communication is |
219 // permitted. | 229 // permitted. |
220 // NB: This scheme doesn't permit non-dualstack IPv6 sockets. | 230 // NB: This scheme doesn't permit non-dualstack IPv6 sockets. |
221 static bool CanInteractWith(VirtualSocket* local, VirtualSocket* remote); | 231 static bool CanInteractWith(VirtualSocket* local, VirtualSocket* remote); |
222 | 232 |
223 private: | 233 private: |
224 friend class VirtualSocket; | 234 friend class VirtualSocket; |
225 | 235 |
236 // Sending was previously blocked, but now isn't. | |
237 sigslot::signal0<> SignalReadyToSend; | |
238 | |
226 typedef std::map<SocketAddress, VirtualSocket*> AddressMap; | 239 typedef std::map<SocketAddress, VirtualSocket*> AddressMap; |
227 typedef std::map<SocketAddressPair, VirtualSocket*> ConnectionMap; | 240 typedef std::map<SocketAddressPair, VirtualSocket*> ConnectionMap; |
228 | 241 |
229 SocketServer* server_; | 242 SocketServer* server_; |
230 bool server_owned_; | 243 bool server_owned_; |
231 MessageQueue* msg_queue_; | 244 MessageQueue* msg_queue_; |
232 bool stop_on_idle_; | 245 bool stop_on_idle_; |
233 int64_t network_delay_; | 246 int64_t network_delay_; |
234 in_addr next_ipv4_; | 247 in_addr next_ipv4_; |
235 in6_addr next_ipv6_; | 248 in6_addr next_ipv6_; |
236 uint16_t next_port_; | 249 uint16_t next_port_; |
237 AddressMap* bindings_; | 250 AddressMap* bindings_; |
238 ConnectionMap* connections_; | 251 ConnectionMap* connections_; |
239 | 252 |
240 IPAddress default_route_v4_; | 253 IPAddress default_route_v4_; |
241 IPAddress default_route_v6_; | 254 IPAddress default_route_v6_; |
242 | 255 |
243 uint32_t bandwidth_; | 256 uint32_t bandwidth_; |
244 uint32_t network_capacity_; | 257 uint32_t network_capacity_; |
245 uint32_t send_buffer_capacity_; | 258 uint32_t send_buffer_capacity_; |
246 uint32_t recv_buffer_capacity_; | 259 uint32_t recv_buffer_capacity_; |
247 uint32_t delay_mean_; | 260 uint32_t delay_mean_; |
248 uint32_t delay_stddev_; | 261 uint32_t delay_stddev_; |
249 uint32_t delay_samples_; | 262 uint32_t delay_samples_; |
250 Function* delay_dist_; | 263 Function* delay_dist_; |
251 CriticalSection delay_crit_; | 264 CriticalSection delay_crit_; |
252 | 265 |
253 double drop_prob_; | 266 double drop_prob_; |
267 bool sending_blocked_ = false; | |
254 RTC_DISALLOW_COPY_AND_ASSIGN(VirtualSocketServer); | 268 RTC_DISALLOW_COPY_AND_ASSIGN(VirtualSocketServer); |
255 }; | 269 }; |
256 | 270 |
257 // Implements the socket interface using the virtual network. Packets are | 271 // Implements the socket interface using the virtual network. Packets are |
258 // passed as messages using the message queue of the socket server. | 272 // passed as messages using the message queue of the socket server. |
259 class VirtualSocket : public AsyncSocket, public MessageHandler { | 273 class VirtualSocket : public AsyncSocket, |
274 public MessageHandler, | |
275 public sigslot::has_slots<> { | |
260 public: | 276 public: |
261 VirtualSocket(VirtualSocketServer* server, int family, int type, bool async); | 277 VirtualSocket(VirtualSocketServer* server, int family, int type, bool async); |
262 ~VirtualSocket() override; | 278 ~VirtualSocket() override; |
263 | 279 |
264 SocketAddress GetLocalAddress() const override; | 280 SocketAddress GetLocalAddress() const override; |
265 SocketAddress GetRemoteAddress() const override; | 281 SocketAddress GetRemoteAddress() const override; |
266 | 282 |
267 // Used by TurnPortTest to mimic a case where proxy returns local host address | 283 // Used by TurnPortTest to mimic a case where proxy returns local host address |
268 // instead of the original one TurnPort was bound against. Please see WebRTC | 284 // instead of the original one TurnPort was bound against. Please see WebRTC |
269 // issue 3927 for more detail. | 285 // issue 3927 for more detail. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
309 typedef std::map<Option, int> OptionsMap; | 325 typedef std::map<Option, int> OptionsMap; |
310 | 326 |
311 int InitiateConnect(const SocketAddress& addr, bool use_delay); | 327 int InitiateConnect(const SocketAddress& addr, bool use_delay); |
312 void CompleteConnect(const SocketAddress& addr, bool notify); | 328 void CompleteConnect(const SocketAddress& addr, bool notify); |
313 int SendUdp(const void* pv, size_t cb, const SocketAddress& addr); | 329 int SendUdp(const void* pv, size_t cb, const SocketAddress& addr); |
314 int SendTcp(const void* pv, size_t cb); | 330 int SendTcp(const void* pv, size_t cb); |
315 | 331 |
316 // Used by server sockets to set the local address without binding. | 332 // Used by server sockets to set the local address without binding. |
317 void SetLocalAddress(const SocketAddress& addr); | 333 void SetLocalAddress(const SocketAddress& addr); |
318 | 334 |
335 void OnSocketServerReadyToSend(); | |
336 | |
319 VirtualSocketServer* server_; | 337 VirtualSocketServer* server_; |
320 int type_; | 338 int type_; |
321 bool async_; | 339 bool async_; |
322 ConnState state_; | 340 ConnState state_; |
323 int error_; | 341 int error_; |
324 SocketAddress local_addr_; | 342 SocketAddress local_addr_; |
325 SocketAddress alternative_local_addr_; | 343 SocketAddress alternative_local_addr_; |
326 SocketAddress remote_addr_; | 344 SocketAddress remote_addr_; |
327 | 345 |
328 // Pending sockets which can be Accepted | 346 // Pending sockets which can be Accepted |
329 ListenQueue* listen_queue_; | 347 ListenQueue* listen_queue_; |
330 | 348 |
331 // Data which tcp has buffered for sending | 349 // Data which tcp has buffered for sending |
332 SendBuffer send_buffer_; | 350 SendBuffer send_buffer_; |
333 bool write_enabled_; | 351 // Set to false if the last attempt to send resulted in EWOULDBLOCK. |
352 // Set back to true when the socket can send again. | |
353 bool ready_to_send_ = true; | |
334 | 354 |
335 // Critical section to protect the recv_buffer and queue_ | 355 // Critical section to protect the recv_buffer and queue_ |
336 CriticalSection crit_; | 356 CriticalSection crit_; |
337 | 357 |
338 // Network model that enforces bandwidth and capacity constraints | 358 // Network model that enforces bandwidth and capacity constraints |
339 NetworkQueue network_; | 359 NetworkQueue network_; |
340 size_t network_size_; | 360 size_t network_size_; |
341 | 361 |
342 // Data which has been received from the network | 362 // Data which has been received from the network |
343 RecvBuffer recv_buffer_; | 363 RecvBuffer recv_buffer_; |
(...skipping 11 matching lines...) Expand all Loading... | |
355 | 375 |
356 // Store the options that are set | 376 // Store the options that are set |
357 OptionsMap options_map_; | 377 OptionsMap options_map_; |
358 | 378 |
359 friend class VirtualSocketServer; | 379 friend class VirtualSocketServer; |
360 }; | 380 }; |
361 | 381 |
362 } // namespace rtc | 382 } // namespace rtc |
363 | 383 |
364 #endif // WEBRTC_BASE_VIRTUALSOCKETSERVER_H_ | 384 #endif // WEBRTC_BASE_VIRTUALSOCKETSERVER_H_ |
OLD | NEW |