fake_socket_factory.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #ifndef REMOTING_TEST_FAKE_SOCKET_FACTORY_H_
  5. #define REMOTING_TEST_FAKE_SOCKET_FACTORY_H_
  6. #include <stdint.h>
  7. #include <list>
  8. #include <memory>
  9. #include "base/callback_forward.h"
  10. #include "base/compiler_specific.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/time/time.h"
  13. #include "remoting/test/fake_network_dispatcher.h"
  14. #include "third_party/webrtc/api/packet_socket_factory.h"
  15. namespace remoting {
  16. class FakeNetworkDispatcher;
  17. class LeakyBucket;
  18. class FakePacketSocketFactory : public rtc::PacketSocketFactory,
  19. public FakeNetworkDispatcher::Node {
  20. public:
  21. // |dispatcher| must outlive the factory.
  22. explicit FakePacketSocketFactory(FakeNetworkDispatcher* dispatcher);
  23. FakePacketSocketFactory(const FakePacketSocketFactory&) = delete;
  24. FakePacketSocketFactory& operator=(const FakePacketSocketFactory&) = delete;
  25. ~FakePacketSocketFactory() override;
  26. void OnSocketDestroyed(int port);
  27. // |bandwidth| - simulated link bandwidth in bytes/second. 0 indicates that
  28. // bandwidth is unlimited.
  29. // |max_buffer| - size of buffers in bytes. Ignored when |bandwidth| is 0.
  30. void SetBandwidth(int bandwidth, int max_buffer);
  31. // Specifies parameters for simulated network latency. Latency is generated
  32. // with normal distribution around |average| with the given |stddev|. Random
  33. // latency calculated based on these parameters is added to the buffering
  34. // delay (which is calculated based on the parameters passed to
  35. // SetBandwidth()). I.e. total latency for each packet is calculated using the
  36. // following formula
  37. //
  38. // l = NormalRand(average, stddev) + bytes_buffered / bandwidth .
  39. //
  40. // Where bytes_buffered is the current level in the leaky bucket used to
  41. // control bandwidth.
  42. void SetLatency(base::TimeDelta average, base::TimeDelta stddev);
  43. void set_out_of_order_rate(double out_of_order_rate) {
  44. out_of_order_rate_ = out_of_order_rate;
  45. }
  46. void ResetStats();
  47. base::TimeDelta average_buffer_delay() {
  48. return total_packets_received_ > 0
  49. ? (total_buffer_delay_ / total_packets_received_)
  50. : base::TimeDelta();
  51. }
  52. base::TimeDelta max_buffer_delay() { return max_buffer_delay_; }
  53. double drop_rate() {
  54. return static_cast<double>(total_packets_dropped_) /
  55. (total_packets_received_ + total_packets_dropped_);
  56. }
  57. // rtc::PacketSocketFactory interface.
  58. rtc::AsyncPacketSocket* CreateUdpSocket(
  59. const rtc::SocketAddress& local_address,
  60. uint16_t min_port,
  61. uint16_t max_port) override;
  62. rtc::AsyncListenSocket* CreateServerTcpSocket(
  63. const rtc::SocketAddress& local_address,
  64. uint16_t min_port,
  65. uint16_t max_port,
  66. int opts) override;
  67. rtc::AsyncPacketSocket* CreateClientTcpSocket(
  68. const rtc::SocketAddress& local_address,
  69. const rtc::SocketAddress& remote_address,
  70. const rtc::ProxyInfo& proxy_info,
  71. const std::string& user_agent,
  72. const rtc::PacketSocketTcpOptions& opts) override;
  73. rtc::AsyncResolverInterface* CreateAsyncResolver() override;
  74. // FakeNetworkDispatcher::Node interface.
  75. const scoped_refptr<base::SingleThreadTaskRunner>& GetThread() const override;
  76. const rtc::IPAddress& GetAddress() const override;
  77. void ReceivePacket(const rtc::SocketAddress& from,
  78. const rtc::SocketAddress& to,
  79. const scoped_refptr<net::IOBuffer>& data,
  80. int data_size) override;
  81. private:
  82. struct PendingPacket {
  83. PendingPacket();
  84. PendingPacket(
  85. const rtc::SocketAddress& from,
  86. const rtc::SocketAddress& to,
  87. const scoped_refptr<net::IOBuffer>& data,
  88. int data_size);
  89. PendingPacket(const PendingPacket& other);
  90. ~PendingPacket();
  91. rtc::SocketAddress from;
  92. rtc::SocketAddress to;
  93. scoped_refptr<net::IOBuffer> data;
  94. int data_size;
  95. };
  96. using ReceiveCallback =
  97. base::RepeatingCallback<void(const rtc::SocketAddress& from,
  98. const rtc::SocketAddress& to,
  99. const scoped_refptr<net::IOBuffer>& data,
  100. int data_size)>;
  101. typedef std::map<uint16_t, ReceiveCallback> UdpSocketsMap;
  102. void DoReceivePacket();
  103. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  104. scoped_refptr<FakeNetworkDispatcher> dispatcher_;
  105. rtc::IPAddress address_;
  106. std::unique_ptr<LeakyBucket> leaky_bucket_;
  107. base::TimeDelta latency_average_;
  108. base::TimeDelta latency_stddev_;
  109. double out_of_order_rate_;
  110. UdpSocketsMap udp_sockets_;
  111. uint16_t next_port_;
  112. std::list<PendingPacket> pending_packets_;
  113. int total_packets_received_ = 0;
  114. int total_packets_dropped_ = 0;
  115. base::TimeDelta total_buffer_delay_;
  116. base::TimeDelta max_buffer_delay_;
  117. base::WeakPtrFactory<FakePacketSocketFactory> weak_factory_{this};
  118. };
  119. } // namespace remoting
  120. #endif // REMOTING_TEST_FAKE_SOCKET_FACTORY_H_