OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 NET_TOOLS_QUIC_TEST_TOOLS_PACKET_DROPPING_TEST_WRITER_H_ | 5 #ifndef NET_TOOLS_QUIC_TEST_TOOLS_PACKET_DROPPING_TEST_WRITER_H_ |
6 #define NET_TOOLS_QUIC_TEST_TOOLS_PACKET_DROPPING_TEST_WRITER_H_ | 6 #define NET_TOOLS_QUIC_TEST_TOOLS_PACKET_DROPPING_TEST_WRITER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "net/quic/quic_alarm.h" | 14 #include "net/quic/quic_alarm.h" |
15 #include "net/quic/quic_blocked_writer_interface.h" | 15 #include "net/quic/quic_blocked_writer_interface.h" |
16 #include "net/tools/quic/quic_epoll_clock.h" | 16 #include "net/tools/quic/quic_epoll_clock.h" |
17 #include "net/tools/quic/quic_packet_writer_wrapper.h" | 17 #include "net/tools/quic/quic_packet_writer_wrapper.h" |
18 #include "net/tools/quic/test_tools/quic_test_client.h" | 18 #include "net/tools/quic/test_tools/quic_test_client.h" |
19 #include "net/tools/quic/test_tools/quic_test_utils.h" | 19 #include "net/tools/quic/test_tools/quic_test_utils.h" |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 namespace tools { | 22 namespace tools { |
23 namespace test { | 23 namespace test { |
24 | 24 |
25 // Simulates a connection that drops packets a configured percentage of the time | 25 // Simulates a connection that drops packets a configured percentage of the time |
26 // and has a blocked socket a configured percentage of the time. Also provides | 26 // and has a blocked socket a configured percentage of the time. Also provides |
27 // the options to delay packets and reorder packets if delay is enabled. | 27 // the options to delay packets and reorder packets if delay is enabled. |
28 class PacketDroppingTestWriter : public QuicPacketWriterWrapper { | 28 class PacketDroppingTestWriter : public QuicPacketWriterWrapper { |
29 public: | 29 public: |
| 30 class Delegate { |
| 31 public: |
| 32 virtual ~Delegate() {} |
| 33 virtual void OnCanWrite() = 0; |
| 34 }; |
| 35 |
30 PacketDroppingTestWriter(); | 36 PacketDroppingTestWriter(); |
31 | 37 |
32 virtual ~PacketDroppingTestWriter(); | 38 virtual ~PacketDroppingTestWriter(); |
33 | 39 |
34 void SetConnectionHelper(QuicEpollConnectionHelper* helper); | 40 // Must be called before blocking, reordering or delaying (loss is OK). May be |
| 41 // called after connecting if the helper is not available before. |
| 42 // |on_can_write| will be triggered when fake-unblocking; ownership will be |
| 43 // assumed. |
| 44 void Initialize(QuicEpollConnectionHelper* helper, Delegate* on_can_write); |
35 | 45 |
36 // QuicPacketWriter methods: | 46 // QuicPacketWriter methods: |
37 virtual WriteResult WritePacket( | 47 virtual WriteResult WritePacket( |
38 const char* buffer, | 48 const char* buffer, |
39 size_t buf_len, | 49 size_t buf_len, |
40 const IPAddressNumber& self_address, | 50 const IPAddressNumber& self_address, |
41 const IPEndPoint& peer_address, | 51 const IPEndPoint& peer_address, |
42 QuicBlockedWriterInterface* blocked_writer) OVERRIDE; | 52 QuicBlockedWriterInterface* blocked_writer) OVERRIDE; |
43 | 53 |
44 virtual bool IsWriteBlocked() const OVERRIDE; | 54 virtual bool IsWriteBlocked() const OVERRIDE; |
45 | 55 |
46 virtual void SetWritable() OVERRIDE; | 56 virtual void SetWritable() OVERRIDE; |
47 | 57 |
48 // Writes out any packet which should have been sent by now | 58 // Writes out any packet which should have been sent by now |
49 // to the contained writer and returns the time | 59 // to the contained writer and returns the time |
50 // for the next delayed packet to be written. | 60 // for the next delayed packet to be written. |
51 QuicTime ReleaseOldPackets(); | 61 QuicTime ReleaseOldPackets(); |
52 | 62 |
53 QuicBlockedWriterInterface* blocked_writer() { return blocked_writer_; } | 63 void OnCanWrite(); |
54 | 64 |
55 // The percent of time a packet is simulated as being lost. | 65 // The percent of time a packet is simulated as being lost. |
56 void set_fake_packet_loss_percentage(int32 fake_packet_loss_percentage) { | 66 void set_fake_packet_loss_percentage(int32 fake_packet_loss_percentage) { |
57 base::AutoLock locked(config_mutex_); | 67 base::AutoLock locked(config_mutex_); |
58 fake_packet_loss_percentage_ = fake_packet_loss_percentage; | 68 fake_packet_loss_percentage_ = fake_packet_loss_percentage; |
59 } | 69 } |
60 | 70 |
61 // The percent of time WritePacket will block and set WriteResult's status | 71 // The percent of time WritePacket will block and set WriteResult's status |
62 // to WRITE_STATUS_BLOCKED. | 72 // to WRITE_STATUS_BLOCKED. |
63 void set_fake_blocked_socket_percentage( | 73 void set_fake_blocked_socket_percentage( |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 const IPAddressNumber self_address; | 128 const IPAddressNumber self_address; |
119 const IPEndPoint peer_address; | 129 const IPEndPoint peer_address; |
120 QuicTime send_time; | 130 QuicTime send_time; |
121 }; | 131 }; |
122 | 132 |
123 typedef std::list<DelayedWrite> DelayedPacketList; | 133 typedef std::list<DelayedWrite> DelayedPacketList; |
124 | 134 |
125 const QuicClock* clock_; | 135 const QuicClock* clock_; |
126 scoped_ptr<QuicAlarm> write_unblocked_alarm_; | 136 scoped_ptr<QuicAlarm> write_unblocked_alarm_; |
127 scoped_ptr<QuicAlarm> delay_alarm_; | 137 scoped_ptr<QuicAlarm> delay_alarm_; |
128 QuicBlockedWriterInterface* blocked_writer_; | 138 scoped_ptr<Delegate> on_can_write_; |
129 SimpleRandom simple_random_; | 139 SimpleRandom simple_random_; |
130 // Stored packets delayed by fake packet delay or bandwidth restrictions. | 140 // Stored packets delayed by fake packet delay or bandwidth restrictions. |
131 DelayedPacketList delayed_packets_; | 141 DelayedPacketList delayed_packets_; |
132 QuicByteCount cur_buffer_size_; | 142 QuicByteCount cur_buffer_size_; |
133 | 143 |
134 base::Lock config_mutex_; | 144 base::Lock config_mutex_; |
135 int32 fake_packet_loss_percentage_; | 145 int32 fake_packet_loss_percentage_; |
136 int32 fake_blocked_socket_percentage_; | 146 int32 fake_blocked_socket_percentage_; |
137 int32 fake_packet_reorder_percentage_; | 147 int32 fake_packet_reorder_percentage_; |
138 QuicTime::Delta fake_packet_delay_; | 148 QuicTime::Delta fake_packet_delay_; |
139 QuicBandwidth fake_bandwidth_; | 149 QuicBandwidth fake_bandwidth_; |
140 QuicByteCount buffer_size_; | 150 QuicByteCount buffer_size_; |
141 | 151 |
142 DISALLOW_COPY_AND_ASSIGN(PacketDroppingTestWriter); | 152 DISALLOW_COPY_AND_ASSIGN(PacketDroppingTestWriter); |
143 }; | 153 }; |
144 | 154 |
145 } // namespace test | 155 } // namespace test |
146 } // namespace tools | 156 } // namespace tools |
147 } // namespace net | 157 } // namespace net |
148 | 158 |
149 #endif // NET_TOOLS_QUIC_TEST_TOOLS_PACKET_DROPPING_TEST_WRITER_H_ | 159 #endif // NET_TOOLS_QUIC_TEST_TOOLS_PACKET_DROPPING_TEST_WRITER_H_ |
OLD | NEW |