| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef REMOTING_TEST_FAKE_SOCKET_FACTORY_H_ | 5 #ifndef REMOTING_TEST_FAKE_SOCKET_FACTORY_H_ |
| 6 #define REMOTING_TEST_FAKE_SOCKET_FACTORY_H_ | 6 #define REMOTING_TEST_FAKE_SOCKET_FACTORY_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 | 9 |
| 10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "remoting/test/fake_network_dispatcher.h" | 13 #include "remoting/test/fake_network_dispatcher.h" |
| 14 #include "third_party/libjingle/source/talk/p2p/base/packetsocketfactory.h" | 14 #include "third_party/libjingle/source/talk/p2p/base/packetsocketfactory.h" |
| 15 | 15 |
| 16 namespace remoting { | 16 namespace remoting { |
| 17 | 17 |
| 18 class FakeNetworkDispatcher; | 18 class FakeNetworkDispatcher; |
| 19 class LeakyBucket; | 19 class LeakyBucket; |
| 20 | 20 |
| 21 class FakePacketSocketFactory : public rtc::PacketSocketFactory, | 21 class FakePacketSocketFactory : public rtc::PacketSocketFactory, |
| 22 public FakeNetworkDispatcher::Node { | 22 public FakeNetworkDispatcher::Node { |
| 23 public: | 23 public: |
| 24 // |dispatcher| must outlive the factory. | 24 // |dispatcher| must outlive the factory. |
| 25 explicit FakePacketSocketFactory(FakeNetworkDispatcher* dispatcher); | 25 explicit FakePacketSocketFactory(FakeNetworkDispatcher* dispatcher); |
| 26 virtual ~FakePacketSocketFactory(); | 26 ~FakePacketSocketFactory() override; |
| 27 | 27 |
| 28 void OnSocketDestroyed(int port); | 28 void OnSocketDestroyed(int port); |
| 29 | 29 |
| 30 // |bandwidth| - simulated link bandwidth in bytes/second. 0 indicates that | 30 // |bandwidth| - simulated link bandwidth in bytes/second. 0 indicates that |
| 31 // bandwidth is unlimited. | 31 // bandwidth is unlimited. |
| 32 // |max_buffer| - size of buffers in bytes. Ignored when |bandwidth| is 0. | 32 // |max_buffer| - size of buffers in bytes. Ignored when |bandwidth| is 0. |
| 33 void SetBandwidth(int bandwidth, int max_buffer); | 33 void SetBandwidth(int bandwidth, int max_buffer); |
| 34 | 34 |
| 35 // Specifies parameters for simulated network latency. Latency is generated | 35 // Specifies parameters for simulated network latency. Latency is generated |
| 36 // with normal distribution around |average| with the given |stddev|. Random | 36 // with normal distribution around |average| with the given |stddev|. Random |
| 37 // latency calculated based on these parameters is added to the buffering | 37 // latency calculated based on these parameters is added to the buffering |
| 38 // delay (which is calculated based on the parameters passed to | 38 // delay (which is calculated based on the parameters passed to |
| 39 // SetBandwidth()). I.e. total latency for each packet is calculated using the | 39 // SetBandwidth()). I.e. total latency for each packet is calculated using the |
| 40 // following formula | 40 // following formula |
| 41 // | 41 // |
| 42 // l = NormalRand(average, stddev) + bytes_buffered / bandwidth . | 42 // l = NormalRand(average, stddev) + bytes_buffered / bandwidth . |
| 43 // | 43 // |
| 44 // Where bytes_buffered is the current level in the leaky bucket used to | 44 // Where bytes_buffered is the current level in the leaky bucket used to |
| 45 // control bandwidth. | 45 // control bandwidth. |
| 46 void SetLatency(base::TimeDelta average, base::TimeDelta stddev); | 46 void SetLatency(base::TimeDelta average, base::TimeDelta stddev); |
| 47 | 47 |
| 48 void set_out_of_order_rate(double out_of_order_rate) { | 48 void set_out_of_order_rate(double out_of_order_rate) { |
| 49 out_of_order_rate_ = out_of_order_rate; | 49 out_of_order_rate_ = out_of_order_rate; |
| 50 } | 50 } |
| 51 | 51 |
| 52 // rtc::PacketSocketFactory interface. | 52 // rtc::PacketSocketFactory interface. |
| 53 virtual rtc::AsyncPacketSocket* CreateUdpSocket( | 53 rtc::AsyncPacketSocket* CreateUdpSocket( |
| 54 const rtc::SocketAddress& local_address, | 54 const rtc::SocketAddress& local_address, |
| 55 int min_port, int max_port) override; | 55 int min_port, |
| 56 virtual rtc::AsyncPacketSocket* CreateServerTcpSocket( | 56 int max_port) override; |
| 57 rtc::AsyncPacketSocket* CreateServerTcpSocket( |
| 57 const rtc::SocketAddress& local_address, | 58 const rtc::SocketAddress& local_address, |
| 58 int min_port, int max_port, | 59 int min_port, |
| 60 int max_port, |
| 59 int opts) override; | 61 int opts) override; |
| 60 virtual rtc::AsyncPacketSocket* CreateClientTcpSocket( | 62 rtc::AsyncPacketSocket* CreateClientTcpSocket( |
| 61 const rtc::SocketAddress& local_address, | 63 const rtc::SocketAddress& local_address, |
| 62 const rtc::SocketAddress& remote_address, | 64 const rtc::SocketAddress& remote_address, |
| 63 const rtc::ProxyInfo& proxy_info, | 65 const rtc::ProxyInfo& proxy_info, |
| 64 const std::string& user_agent, | 66 const std::string& user_agent, |
| 65 int opts) override; | 67 int opts) override; |
| 66 virtual rtc::AsyncResolverInterface* CreateAsyncResolver() override; | 68 rtc::AsyncResolverInterface* CreateAsyncResolver() override; |
| 67 | 69 |
| 68 // FakeNetworkDispatcher::Node interface. | 70 // FakeNetworkDispatcher::Node interface. |
| 69 virtual const scoped_refptr<base::SingleThreadTaskRunner>& GetThread() | 71 const scoped_refptr<base::SingleThreadTaskRunner>& GetThread() const override; |
| 70 const override; | 72 const rtc::IPAddress& GetAddress() const override; |
| 71 virtual const rtc::IPAddress& GetAddress() const override; | 73 void ReceivePacket(const rtc::SocketAddress& from, |
| 72 virtual void ReceivePacket(const rtc::SocketAddress& from, | 74 const rtc::SocketAddress& to, |
| 73 const rtc::SocketAddress& to, | 75 const scoped_refptr<net::IOBuffer>& data, |
| 74 const scoped_refptr<net::IOBuffer>& data, | 76 int data_size) override; |
| 75 int data_size) override; | |
| 76 | 77 |
| 77 private: | 78 private: |
| 78 struct PendingPacket { | 79 struct PendingPacket { |
| 79 PendingPacket(); | 80 PendingPacket(); |
| 80 PendingPacket( | 81 PendingPacket( |
| 81 const rtc::SocketAddress& from, | 82 const rtc::SocketAddress& from, |
| 82 const rtc::SocketAddress& to, | 83 const rtc::SocketAddress& to, |
| 83 const scoped_refptr<net::IOBuffer>& data, | 84 const scoped_refptr<net::IOBuffer>& data, |
| 84 int data_size); | 85 int data_size); |
| 85 ~PendingPacket(); | 86 ~PendingPacket(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 114 std::list<PendingPacket> pending_packets_; | 115 std::list<PendingPacket> pending_packets_; |
| 115 | 116 |
| 116 base::WeakPtrFactory<FakePacketSocketFactory> weak_factory_; | 117 base::WeakPtrFactory<FakePacketSocketFactory> weak_factory_; |
| 117 | 118 |
| 118 DISALLOW_COPY_AND_ASSIGN(FakePacketSocketFactory); | 119 DISALLOW_COPY_AND_ASSIGN(FakePacketSocketFactory); |
| 119 }; | 120 }; |
| 120 | 121 |
| 121 } // namespace remoting | 122 } // namespace remoting |
| 122 | 123 |
| 123 #endif // REMOTING_TEST_FAKE_SOCKET_FACTORY_H_ | 124 #endif // REMOTING_TEST_FAKE_SOCKET_FACTORY_H_ |
| OLD | NEW |