| 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 // MSVC++ requires this to be set before any other includes to get M_PI. | 5 // MSVC++ requires this to be set before any other includes to get M_PI. |
| 6 #define _USE_MATH_DEFINES | 6 #define _USE_MATH_DEFINES |
| 7 | 7 |
| 8 #include "remoting/test/fake_socket_factory.h" | 8 #include "remoting/test/fake_socket_factory.h" |
| 9 | 9 |
| 10 #include <math.h> | 10 #include <math.h> |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 const rtc::SocketAddress& from, | 169 const rtc::SocketAddress& from, |
| 170 const rtc::SocketAddress& to, | 170 const rtc::SocketAddress& to, |
| 171 const scoped_refptr<net::IOBuffer>& data, | 171 const scoped_refptr<net::IOBuffer>& data, |
| 172 int data_size) | 172 int data_size) |
| 173 : from(from), to(to), data(data), data_size(data_size) { | 173 : from(from), to(to), data(data), data_size(data_size) { |
| 174 } | 174 } |
| 175 | 175 |
| 176 FakePacketSocketFactory::PendingPacket::PendingPacket( | 176 FakePacketSocketFactory::PendingPacket::PendingPacket( |
| 177 const PendingPacket& other) = default; | 177 const PendingPacket& other) = default; |
| 178 | 178 |
| 179 FakePacketSocketFactory::PendingPacket::~PendingPacket() { | 179 FakePacketSocketFactory::PendingPacket::~PendingPacket() {} |
| 180 } | |
| 181 | 180 |
| 182 FakePacketSocketFactory::FakePacketSocketFactory( | 181 FakePacketSocketFactory::FakePacketSocketFactory( |
| 183 FakeNetworkDispatcher* dispatcher) | 182 FakeNetworkDispatcher* dispatcher) |
| 184 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | 183 : task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 185 dispatcher_(dispatcher), | 184 dispatcher_(dispatcher), |
| 186 address_(dispatcher_->AllocateAddress()), | 185 address_(dispatcher_->AllocateAddress()), |
| 187 out_of_order_rate_(0.0), | 186 out_of_order_rate_(0.0), |
| 188 next_port_(kPortRangeStart), | 187 next_port_(kPortRangeStart), |
| 189 weak_factory_(this) { | 188 weak_factory_(this) { |
| 190 dispatcher_->AddNode(this); | 189 dispatcher_->AddNode(this); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 } | 301 } |
| 303 | 302 |
| 304 if (latency_average_ > base::TimeDelta()) { | 303 if (latency_average_ > base::TimeDelta()) { |
| 305 delay += base::TimeDelta::FromMillisecondsD( | 304 delay += base::TimeDelta::FromMillisecondsD( |
| 306 GetNormalRandom(latency_average_.InMillisecondsF(), | 305 GetNormalRandom(latency_average_.InMillisecondsF(), |
| 307 latency_stddev_.InMillisecondsF())); | 306 latency_stddev_.InMillisecondsF())); |
| 308 } | 307 } |
| 309 if (delay < base::TimeDelta()) | 308 if (delay < base::TimeDelta()) |
| 310 delay = base::TimeDelta(); | 309 delay = base::TimeDelta(); |
| 311 | 310 |
| 311 total_delay_ += delay; |
| 312 ++total_packets_received_; |
| 313 |
| 312 // Put the packet to the |pending_packets_| and post a task for | 314 // Put the packet to the |pending_packets_| and post a task for |
| 313 // DoReceivePackets(). Note that the DoReceivePackets() task posted here may | 315 // DoReceivePackets(). Note that the DoReceivePackets() task posted here may |
| 314 // deliver a different packet, not the one added to the queue here. This | 316 // deliver a different packet, not the one added to the queue here. This |
| 315 // would happen if another task gets posted with a shorted delay or when | 317 // would happen if another task gets posted with a shorted delay or when |
| 316 // |out_of_order_rate_| is greater than 0. It's implemented this way to | 318 // |out_of_order_rate_| is greater than 0. It's implemented this way to |
| 317 // decouple latency variability from out-of-order delivery. | 319 // decouple latency variability from out-of-order delivery. |
| 318 PendingPacket packet(from, to, data, data_size); | 320 PendingPacket packet(from, to, data, data_size); |
| 319 pending_packets_.push_back(packet); | 321 pending_packets_.push_back(packet); |
| 320 task_runner_->PostDelayedTask( | 322 task_runner_->PostDelayedTask( |
| 321 FROM_HERE, | 323 FROM_HERE, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 340 | 342 |
| 341 UdpSocketsMap::iterator iter = udp_sockets_.find(packet.to.port()); | 343 UdpSocketsMap::iterator iter = udp_sockets_.find(packet.to.port()); |
| 342 if (iter == udp_sockets_.end()) { | 344 if (iter == udp_sockets_.end()) { |
| 343 // Invalid port number. | 345 // Invalid port number. |
| 344 return; | 346 return; |
| 345 } | 347 } |
| 346 | 348 |
| 347 iter->second.Run(packet.from, packet.to, packet.data, packet.data_size); | 349 iter->second.Run(packet.from, packet.to, packet.data, packet.data_size); |
| 348 } | 350 } |
| 349 | 351 |
| 352 void FakePacketSocketFactory::ResetStats() { |
| 353 total_packets_received_ = 0; |
| 354 total_delay_ = base::TimeDelta(); |
| 355 } |
| 356 |
| 357 base::TimeDelta FakePacketSocketFactory::GetAverageDelay() { |
| 358 return total_packets_received_ > 0 ? (total_delay_ / total_packets_received_) |
| 359 : base::TimeDelta(); |
| 360 } |
| 361 |
| 350 } // namespace remoting | 362 } // namespace remoting |
| OLD | NEW |