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 #include <errno.h> | 5 #include <errno.h> |
6 #include <fcntl.h> | 6 #include <fcntl.h> |
7 #include <linux/if_tun.h> | 7 #include <linux/if_tun.h> |
8 #include <linux/types.h> | 8 #include <linux/types.h> |
9 #include <math.h> | 9 #include <math.h> |
10 #include <net/if.h> | 10 #include <net/if.h> |
11 #include <netinet/in.h> | 11 #include <netinet/in.h> |
12 #include <stddef.h> | 12 #include <stddef.h> |
13 #include <stdint.h> | 13 #include <stdint.h> |
14 #include <stdio.h> | 14 #include <stdio.h> |
15 #include <stdlib.h> | 15 #include <stdlib.h> |
16 #include <sys/ioctl.h> | 16 #include <sys/ioctl.h> |
17 #include <sys/stat.h> | 17 #include <sys/stat.h> |
18 #include <sys/types.h> | 18 #include <sys/types.h> |
19 #include <unistd.h> | 19 #include <unistd.h> |
20 | |
21 #include <deque> | 20 #include <deque> |
22 #include <map> | 21 #include <map> |
| 22 #include <utility> |
23 | 23 |
24 #include "base/at_exit.h" | 24 #include "base/at_exit.h" |
25 #include "base/bind.h" | 25 #include "base/bind.h" |
26 #include "base/command_line.h" | 26 #include "base/command_line.h" |
27 #include "base/logging.h" | 27 #include "base/logging.h" |
28 #include "base/rand_util.h" | 28 #include "base/rand_util.h" |
29 #include "base/single_thread_task_runner.h" | 29 #include "base/single_thread_task_runner.h" |
30 #include "base/synchronization/waitable_event.h" | 30 #include "base/synchronization/waitable_event.h" |
31 #include "base/thread_task_runner_handle.h" | 31 #include "base/thread_task_runner_handle.h" |
32 #include "base/threading/thread.h" | 32 #include "base/threading/thread.h" |
(...skipping 30 matching lines...) Expand all Loading... |
63 } | 63 } |
64 break; | 64 break; |
65 } | 65 } |
66 } | 66 } |
67 private: | 67 private: |
68 int fd_; | 68 int fd_; |
69 }; | 69 }; |
70 | 70 |
71 class QueueManager : public base::MessageLoopForIO::Watcher { | 71 class QueueManager : public base::MessageLoopForIO::Watcher { |
72 public: | 72 public: |
73 QueueManager(int input_fd, | 73 QueueManager(int input_fd, int output_fd, scoped_ptr<PacketPipe> pipe) |
74 int output_fd, | 74 : input_fd_(input_fd), packet_pipe_(std::move(pipe)) { |
75 scoped_ptr<PacketPipe> pipe) : | |
76 input_fd_(input_fd), | |
77 packet_pipe_(pipe.Pass()) { | |
78 | |
79 CHECK(base::MessageLoopForIO::current()->WatchFileDescriptor( | 75 CHECK(base::MessageLoopForIO::current()->WatchFileDescriptor( |
80 input_fd_, true, base::MessageLoopForIO::WATCH_READ, | 76 input_fd_, true, base::MessageLoopForIO::WATCH_READ, |
81 &read_socket_watcher_, this)); | 77 &read_socket_watcher_, this)); |
82 | 78 |
83 scoped_ptr<PacketPipe> tmp(new SendToFDPipe(output_fd)); | 79 scoped_ptr<PacketPipe> tmp(new SendToFDPipe(output_fd)); |
84 if (packet_pipe_) { | 80 if (packet_pipe_) { |
85 packet_pipe_->AppendToPipe(tmp.Pass()); | 81 packet_pipe_->AppendToPipe(std::move(tmp)); |
86 } else { | 82 } else { |
87 packet_pipe_ = tmp.Pass(); | 83 packet_pipe_ = std::move(tmp); |
88 } | 84 } |
89 packet_pipe_->InitOnIOThread(base::ThreadTaskRunnerHandle::Get(), | 85 packet_pipe_->InitOnIOThread(base::ThreadTaskRunnerHandle::Get(), |
90 &tick_clock_); | 86 &tick_clock_); |
91 } | 87 } |
92 | 88 |
93 ~QueueManager() final {} | 89 ~QueueManager() final {} |
94 | 90 |
95 // MessageLoopForIO::Watcher methods | 91 // MessageLoopForIO::Watcher methods |
96 void OnFileCanReadWithoutBlocking(int fd) final { | 92 void OnFileCanReadWithoutBlocking(int fd) final { |
97 scoped_ptr<Packet> packet(new Packet(kMaxPacketSize)); | 93 scoped_ptr<Packet> packet(new Packet(kMaxPacketSize)); |
98 int nread = read(input_fd_, | 94 int nread = read(input_fd_, |
99 reinterpret_cast<char*>(&packet->front()), | 95 reinterpret_cast<char*>(&packet->front()), |
100 kMaxPacketSize); | 96 kMaxPacketSize); |
101 if (nread < 0) { | 97 if (nread < 0) { |
102 if (errno == EINTR) return; | 98 if (errno == EINTR) return; |
103 perror("read"); | 99 perror("read"); |
104 exit(1); | 100 exit(1); |
105 } | 101 } |
106 if (nread == 0) return; | 102 if (nread == 0) return; |
107 packet->resize(nread); | 103 packet->resize(nread); |
108 packet_pipe_->Send(packet.Pass()); | 104 packet_pipe_->Send(std::move(packet)); |
109 } | 105 } |
110 void OnFileCanWriteWithoutBlocking(int fd) final { NOTREACHED(); } | 106 void OnFileCanWriteWithoutBlocking(int fd) final { NOTREACHED(); } |
111 | 107 |
112 private: | 108 private: |
113 int input_fd_; | 109 int input_fd_; |
114 scoped_ptr<PacketPipe> packet_pipe_; | 110 scoped_ptr<PacketPipe> packet_pipe_; |
115 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; | 111 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; |
116 base::DefaultTickClock tick_clock_; | 112 base::DefaultTickClock tick_clock_; |
117 }; | 113 }; |
118 | 114 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 ByteCounter in_pipe_input_counter; | 165 ByteCounter in_pipe_input_counter; |
170 ByteCounter in_pipe_output_counter; | 166 ByteCounter in_pipe_output_counter; |
171 ByteCounter out_pipe_input_counter; | 167 ByteCounter out_pipe_input_counter; |
172 ByteCounter out_pipe_output_counter; | 168 ByteCounter out_pipe_output_counter; |
173 | 169 |
174 class ByteCounterPipe : public media::cast::test::PacketPipe { | 170 class ByteCounterPipe : public media::cast::test::PacketPipe { |
175 public: | 171 public: |
176 ByteCounterPipe(ByteCounter* counter) : counter_(counter) {} | 172 ByteCounterPipe(ByteCounter* counter) : counter_(counter) {} |
177 void Send(scoped_ptr<media::cast::Packet> packet) final { | 173 void Send(scoped_ptr<media::cast::Packet> packet) final { |
178 counter_->Increment(packet->size()); | 174 counter_->Increment(packet->size()); |
179 pipe_->Send(packet.Pass()); | 175 pipe_->Send(std::move(packet)); |
180 } | 176 } |
181 private: | 177 private: |
182 ByteCounter* counter_; | 178 ByteCounter* counter_; |
183 }; | 179 }; |
184 | 180 |
185 void SetupByteCounters(scoped_ptr<media::cast::test::PacketPipe>* pipe, | 181 void SetupByteCounters(scoped_ptr<media::cast::test::PacketPipe>* pipe, |
186 ByteCounter* pipe_input_counter, | 182 ByteCounter* pipe_input_counter, |
187 ByteCounter* pipe_output_counter) { | 183 ByteCounter* pipe_output_counter) { |
188 media::cast::test::PacketPipe* new_pipe = | 184 media::cast::test::PacketPipe* new_pipe = |
189 new ByteCounterPipe(pipe_input_counter); | 185 new ByteCounterPipe(pipe_input_counter); |
190 new_pipe->AppendToPipe(pipe->Pass()); | 186 new_pipe->AppendToPipe(std::move(*pipe)); |
191 new_pipe->AppendToPipe( | 187 new_pipe->AppendToPipe(scoped_ptr<media::cast::test::PacketPipe>( |
192 scoped_ptr<media::cast::test::PacketPipe>( | 188 new ByteCounterPipe(pipe_output_counter))); |
193 new ByteCounterPipe(pipe_output_counter)).Pass()); | |
194 pipe->reset(new_pipe); | 189 pipe->reset(new_pipe); |
195 } | 190 } |
196 | 191 |
197 void CheckByteCounters() { | 192 void CheckByteCounters() { |
198 base::TimeTicks now = base::TimeTicks::Now(); | 193 base::TimeTicks now = base::TimeTicks::Now(); |
199 in_pipe_input_counter.push(now); | 194 in_pipe_input_counter.push(now); |
200 in_pipe_output_counter.push(now); | 195 in_pipe_output_counter.push(now); |
201 out_pipe_input_counter.push(now); | 196 out_pipe_input_counter.push(now); |
202 out_pipe_output_counter.push(now); | 197 out_pipe_output_counter.push(now); |
203 if ((now - last_printout).InSeconds() >= 5) { | 198 if ((now - last_printout).InSeconds() >= 5) { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 fprintf(stderr, | 274 fprintf(stderr, |
280 "Where 'type' is one of perfect, good, wifi, bad or evil\n"); | 275 "Where 'type' is one of perfect, good, wifi, bad or evil\n"); |
281 exit(1); | 276 exit(1); |
282 } | 277 } |
283 | 278 |
284 scoped_ptr<media::cast::test::PacketPipe> in_pipe, out_pipe; | 279 scoped_ptr<media::cast::test::PacketPipe> in_pipe, out_pipe; |
285 std::string network_type = argv[3]; | 280 std::string network_type = argv[3]; |
286 if (network_type == "perfect") { | 281 if (network_type == "perfect") { |
287 // No action needed. | 282 // No action needed. |
288 } else if (network_type == "good") { | 283 } else if (network_type == "good") { |
289 in_pipe = media::cast::test::GoodNetwork().Pass(); | 284 in_pipe = media::cast::test::GoodNetwork(); |
290 out_pipe = media::cast::test::GoodNetwork().Pass(); | 285 out_pipe = media::cast::test::GoodNetwork(); |
291 } else if (network_type == "wifi") { | 286 } else if (network_type == "wifi") { |
292 in_pipe = media::cast::test::WifiNetwork().Pass(); | 287 in_pipe = media::cast::test::WifiNetwork(); |
293 out_pipe = media::cast::test::WifiNetwork().Pass(); | 288 out_pipe = media::cast::test::WifiNetwork(); |
294 } else if (network_type == "bad") { | 289 } else if (network_type == "bad") { |
295 in_pipe = media::cast::test::BadNetwork().Pass(); | 290 in_pipe = media::cast::test::BadNetwork(); |
296 out_pipe = media::cast::test::BadNetwork().Pass(); | 291 out_pipe = media::cast::test::BadNetwork(); |
297 } else if (network_type == "evil") { | 292 } else if (network_type == "evil") { |
298 in_pipe = media::cast::test::EvilNetwork().Pass(); | 293 in_pipe = media::cast::test::EvilNetwork(); |
299 out_pipe = media::cast::test::EvilNetwork().Pass(); | 294 out_pipe = media::cast::test::EvilNetwork(); |
300 } else { | 295 } else { |
301 fprintf(stderr, "Unknown network type.\n"); | 296 fprintf(stderr, "Unknown network type.\n"); |
302 exit(1); | 297 exit(1); |
303 } | 298 } |
304 | 299 |
305 SetupByteCounters(&in_pipe, &in_pipe_input_counter, &in_pipe_output_counter); | 300 SetupByteCounters(&in_pipe, &in_pipe_input_counter, &in_pipe_output_counter); |
306 SetupByteCounters( | 301 SetupByteCounters( |
307 &out_pipe, &out_pipe_input_counter, &out_pipe_output_counter); | 302 &out_pipe, &out_pipe_input_counter, &out_pipe_output_counter); |
308 | 303 |
309 int fd1 = tun_alloc(argv[1], IFF_TAP); | 304 int fd1 = tun_alloc(argv[1], IFF_TAP); |
310 int fd2 = tun_alloc(argv[2], IFF_TAP); | 305 int fd2 = tun_alloc(argv[2], IFF_TAP); |
311 | 306 |
312 base::MessageLoopForIO message_loop; | 307 base::MessageLoopForIO message_loop; |
313 last_printout = base::TimeTicks::Now(); | 308 last_printout = base::TimeTicks::Now(); |
314 media::cast::test::QueueManager qm1(fd1, fd2, in_pipe.Pass()); | 309 media::cast::test::QueueManager qm1(fd1, fd2, std::move(in_pipe)); |
315 media::cast::test::QueueManager qm2(fd2, fd1, out_pipe.Pass()); | 310 media::cast::test::QueueManager qm2(fd2, fd1, std::move(out_pipe)); |
316 CheckByteCounters(); | 311 CheckByteCounters(); |
317 printf("Press Ctrl-C when done.\n"); | 312 printf("Press Ctrl-C when done.\n"); |
318 message_loop.Run(); | 313 message_loop.Run(); |
319 } | 314 } |
OLD | NEW |