Chromium Code Reviews| Index: remoting/test/fake_socket_factory.h |
| diff --git a/remoting/test/fake_socket_factory.h b/remoting/test/fake_socket_factory.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6e44e2c59b18f34c61b46552264d849bf6320aaa |
| --- /dev/null |
| +++ b/remoting/test/fake_socket_factory.h |
| @@ -0,0 +1,123 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef REMOTING_TEST_FAKE_SOCKET_FACTORY_H_ |
| +#define REMOTING_TEST_FAKE_SOCKET_FACTORY_H_ |
| + |
| +#include <list> |
| + |
| +#include "base/callback_forward.h" |
| +#include "base/compiler_specific.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "remoting/test/fake_network_dispatcher.h" |
| +#include "third_party/libjingle/source/talk/p2p/base/packetsocketfactory.h" |
| + |
| +namespace remoting { |
| + |
| +class FakeNetworkDispatcher; |
| +class LeakyBucket; |
| + |
| +class FakePacketSocketFactory : public talk_base::PacketSocketFactory, |
| + public FakeNetworkDispatcher::Node { |
| + public: |
| + // |dispatcher| must outlive the factory. |
| + explicit FakePacketSocketFactory(FakeNetworkDispatcher* dispatcher); |
| + virtual ~FakePacketSocketFactory(); |
| + |
| + void OnSocketDestroyed(int port); |
| + |
| + // |bandwidth| - simulated link bandwidth in bytes/second. 0 indicates that |
| + // bundwidth is unlimited. |
|
rmsousa
2014/08/12 20:42:11
typo: bandwidth
Sergey Ulanov
2014/08/16 00:03:34
Done.
|
| + // |max_buffer| - size of buffers in bytes. Ignored when |bandwidth| is 0. |
| + void SetBandwidth(int bandwidth, int max_buffer); |
| + |
| + // Specifies parameters for simulated network latency. Latency is generated |
| + // with normal distribution around |average| with the given |variance|. Random |
| + // latency calculated based on these parameters is added to the buffering |
| + // delay (which is calculated based on the parameters passed to |
| + // SetBandwidth()). I.e. total latency for each packet is calculated using the |
| + // following formula |
| + // |
| + // l = NormalRand(average, variance) + bytes_buffered / bandwidth . |
| + // |
| + // Where bytes_buffered is the current level in the leaky bucket used to |
| + // control bandwidth. |
| + void SetLatency(base::TimeDelta average, base::TimeDelta variance); |
| + |
| + void set_out_of_order_rate(double out_of_order_rate) { |
| + out_of_order_rate_ = out_of_order_rate; |
| + } |
| + |
| + // talk_base::PacketSocketFactory interface. |
| + virtual talk_base::AsyncPacketSocket* CreateUdpSocket( |
| + const talk_base::SocketAddress& local_address, |
| + int min_port, int max_port) OVERRIDE; |
| + virtual talk_base::AsyncPacketSocket* CreateServerTcpSocket( |
| + const talk_base::SocketAddress& local_address, |
| + int min_port, int max_port, |
| + int opts) OVERRIDE; |
| + virtual talk_base::AsyncPacketSocket* CreateClientTcpSocket( |
| + const talk_base::SocketAddress& local_address, |
| + const talk_base::SocketAddress& remote_address, |
| + const talk_base::ProxyInfo& proxy_info, |
| + const std::string& user_agent, |
| + int opts) OVERRIDE; |
| + virtual talk_base::AsyncResolverInterface* CreateAsyncResolver() OVERRIDE; |
| + |
| + // FakeNetworkDispatcher::Node interface. |
| + virtual const scoped_refptr<base::SingleThreadTaskRunner>& GetThread() |
| + const OVERRIDE; |
| + virtual const talk_base::IPAddress& GetAddress() const OVERRIDE; |
| + virtual void ReceivePacket(const talk_base::SocketAddress& from, |
| + const talk_base::SocketAddress& to, |
| + const scoped_refptr<net::IOBuffer>& data, |
| + int data_size) OVERRIDE; |
| + |
| + private: |
| + struct PendingPacket { |
| + PendingPacket(); |
| + PendingPacket( |
| + const talk_base::SocketAddress& from, |
| + const talk_base::SocketAddress& to, |
| + const scoped_refptr<net::IOBuffer>& data, |
| + int data_size); |
| + ~PendingPacket(); |
| + |
| + talk_base::SocketAddress from; |
| + talk_base::SocketAddress to; |
| + scoped_refptr<net::IOBuffer> data; |
| + int data_size; |
| + }; |
| + |
| + typedef base::Callback<void(const talk_base::SocketAddress& from, |
| + const talk_base::SocketAddress& to, |
| + const scoped_refptr<net::IOBuffer>& data, |
| + int data_size)> ReceiveCallback; |
| + typedef std::map<uint16_t, ReceiveCallback> UdpSocketsMap; |
| + |
| + void DoReceivePacket(); |
| + |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| + scoped_refptr<FakeNetworkDispatcher> dispatcher_; |
| + |
| + talk_base::IPAddress address_; |
| + |
| + scoped_ptr<LeakyBucket> leaky_bucket_; |
| + base::TimeDelta latency_average_; |
| + 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.
|
| + double out_of_order_rate_; |
| + |
| + UdpSocketsMap udp_sockets_; |
| + uint16_t next_port_; |
| + |
| + std::list<PendingPacket> pending_packets_; |
| + |
| + base::WeakPtrFactory<FakePacketSocketFactory> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FakePacketSocketFactory); |
| +}; |
| + |
| +} // namespace remoting |
| + |
| +#endif // REMOTING_TEST_FAKE_SOCKET_FACTORY_H_ |