Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef REMOTING_TEST_FAKE_SOCKET_FACTORY_H_ | |
| 6 #define REMOTING_TEST_FAKE_SOCKET_FACTORY_H_ | |
| 7 | |
| 8 #include <list> | |
| 9 | |
| 10 #include "base/callback_forward.h" | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "remoting/test/fake_network_dispatcher.h" | |
| 14 #include "third_party/libjingle/source/talk/p2p/base/packetsocketfactory.h" | |
| 15 | |
| 16 namespace remoting { | |
| 17 | |
| 18 class FakeNetworkDispatcher; | |
| 19 class LeakyBucket; | |
| 20 | |
| 21 class FakePacketSocketFactory : public talk_base::PacketSocketFactory, | |
| 22 public FakeNetworkDispatcher::Node { | |
| 23 public: | |
| 24 // |dispatcher| must outlive the factory. | |
| 25 explicit FakePacketSocketFactory(FakeNetworkDispatcher* dispatcher); | |
| 26 virtual ~FakePacketSocketFactory(); | |
| 27 | |
| 28 void OnSocketDestroyed(int port); | |
| 29 | |
| 30 // |bandwidth| - simulated link bandwidth in bytes/second. 0 indicates that | |
| 31 // bundwidth is unlimited. | |
|
rmsousa
2014/08/12 20:42:11
typo: bandwidth
Sergey Ulanov
2014/08/16 00:03:34
Done.
| |
| 32 // |max_buffer| - size of buffers in bytes. Ignored when |bandwidth| is 0. | |
| 33 void SetBandwidth(int bandwidth, int max_buffer); | |
| 34 | |
| 35 // Specifies parameters for simulated network latency. Latency is generated | |
| 36 // with normal distribution around |average| with the given |variance|. Random | |
| 37 // latency calculated based on these parameters is added to the buffering | |
| 38 // delay (which is calculated based on the parameters passed to | |
| 39 // SetBandwidth()). I.e. total latency for each packet is calculated using the | |
| 40 // following formula | |
| 41 // | |
| 42 // l = NormalRand(average, variance) + bytes_buffered / bandwidth . | |
| 43 // | |
| 44 // Where bytes_buffered is the current level in the leaky bucket used to | |
| 45 // control bandwidth. | |
| 46 void SetLatency(base::TimeDelta average, base::TimeDelta variance); | |
| 47 | |
| 48 void set_out_of_order_rate(double out_of_order_rate) { | |
| 49 out_of_order_rate_ = out_of_order_rate; | |
| 50 } | |
| 51 | |
| 52 // talk_base::PacketSocketFactory interface. | |
| 53 virtual talk_base::AsyncPacketSocket* CreateUdpSocket( | |
| 54 const talk_base::SocketAddress& local_address, | |
| 55 int min_port, int max_port) OVERRIDE; | |
| 56 virtual talk_base::AsyncPacketSocket* CreateServerTcpSocket( | |
| 57 const talk_base::SocketAddress& local_address, | |
| 58 int min_port, int max_port, | |
| 59 int opts) OVERRIDE; | |
| 60 virtual talk_base::AsyncPacketSocket* CreateClientTcpSocket( | |
| 61 const talk_base::SocketAddress& local_address, | |
| 62 const talk_base::SocketAddress& remote_address, | |
| 63 const talk_base::ProxyInfo& proxy_info, | |
| 64 const std::string& user_agent, | |
| 65 int opts) OVERRIDE; | |
| 66 virtual talk_base::AsyncResolverInterface* CreateAsyncResolver() OVERRIDE; | |
| 67 | |
| 68 // FakeNetworkDispatcher::Node interface. | |
| 69 virtual const scoped_refptr<base::SingleThreadTaskRunner>& GetThread() | |
| 70 const OVERRIDE; | |
| 71 virtual const talk_base::IPAddress& GetAddress() const OVERRIDE; | |
| 72 virtual void ReceivePacket(const talk_base::SocketAddress& from, | |
| 73 const talk_base::SocketAddress& to, | |
| 74 const scoped_refptr<net::IOBuffer>& data, | |
| 75 int data_size) OVERRIDE; | |
| 76 | |
| 77 private: | |
| 78 struct PendingPacket { | |
| 79 PendingPacket(); | |
| 80 PendingPacket( | |
| 81 const talk_base::SocketAddress& from, | |
| 82 const talk_base::SocketAddress& to, | |
| 83 const scoped_refptr<net::IOBuffer>& data, | |
| 84 int data_size); | |
| 85 ~PendingPacket(); | |
| 86 | |
| 87 talk_base::SocketAddress from; | |
| 88 talk_base::SocketAddress to; | |
| 89 scoped_refptr<net::IOBuffer> data; | |
| 90 int data_size; | |
| 91 }; | |
| 92 | |
| 93 typedef base::Callback<void(const talk_base::SocketAddress& from, | |
| 94 const talk_base::SocketAddress& to, | |
| 95 const scoped_refptr<net::IOBuffer>& data, | |
| 96 int data_size)> ReceiveCallback; | |
| 97 typedef std::map<uint16_t, ReceiveCallback> UdpSocketsMap; | |
| 98 | |
| 99 void DoReceivePacket(); | |
| 100 | |
| 101 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 102 scoped_refptr<FakeNetworkDispatcher> dispatcher_; | |
| 103 | |
| 104 talk_base::IPAddress address_; | |
| 105 | |
| 106 scoped_ptr<LeakyBucket> leaky_bucket_; | |
| 107 base::TimeDelta latency_average_; | |
| 108 base::TimeDelta latency_variance_; | |
|
rmsousa
2014/08/12 20:42:11
fix variance->stddev here as well.
Sergey Ulanov
2014/08/16 00:03:34
Done.
| |
| 109 double out_of_order_rate_; | |
| 110 | |
| 111 UdpSocketsMap udp_sockets_; | |
| 112 uint16_t next_port_; | |
| 113 | |
| 114 std::list<PendingPacket> pending_packets_; | |
| 115 | |
| 116 base::WeakPtrFactory<FakePacketSocketFactory> weak_factory_; | |
| 117 | |
| 118 DISALLOW_COPY_AND_ASSIGN(FakePacketSocketFactory); | |
| 119 }; | |
| 120 | |
| 121 } // namespace remoting | |
| 122 | |
| 123 #endif // REMOTING_TEST_FAKE_SOCKET_FACTORY_H_ | |
| OLD | NEW |