| 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> |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 namespace media { | 39 namespace media { |
| 40 namespace cast { | 40 namespace cast { |
| 41 namespace test { | 41 namespace test { |
| 42 | 42 |
| 43 const size_t kMaxPacketSize = 4096; | 43 const size_t kMaxPacketSize = 4096; |
| 44 | 44 |
| 45 class SendToFDPipe : public PacketPipe { | 45 class SendToFDPipe : public PacketPipe { |
| 46 public: | 46 public: |
| 47 explicit SendToFDPipe(int fd) : fd_(fd) { | 47 explicit SendToFDPipe(int fd) : fd_(fd) { |
| 48 } | 48 } |
| 49 void Send(scoped_ptr<Packet> packet) final { | 49 void Send(std::unique_ptr<Packet> packet) final { |
| 50 while (1) { | 50 while (1) { |
| 51 int written = write( | 51 int written = write( |
| 52 fd_, | 52 fd_, |
| 53 reinterpret_cast<char*>(&packet->front()), | 53 reinterpret_cast<char*>(&packet->front()), |
| 54 packet->size()); | 54 packet->size()); |
| 55 if (written < 0) { | 55 if (written < 0) { |
| 56 if (errno == EINTR) continue; | 56 if (errno == EINTR) continue; |
| 57 perror("write"); | 57 perror("write"); |
| 58 exit(1); | 58 exit(1); |
| 59 } | 59 } |
| 60 if (written != static_cast<int>(packet->size())) { | 60 if (written != static_cast<int>(packet->size())) { |
| 61 fprintf(stderr, "Truncated write!\n"); | 61 fprintf(stderr, "Truncated write!\n"); |
| 62 exit(1); | 62 exit(1); |
| 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, int output_fd, scoped_ptr<PacketPipe> pipe) | 73 QueueManager(int input_fd, int output_fd, std::unique_ptr<PacketPipe> pipe) |
| 74 : input_fd_(input_fd), packet_pipe_(std::move(pipe)) { | 74 : input_fd_(input_fd), packet_pipe_(std::move(pipe)) { |
| 75 CHECK(base::MessageLoopForIO::current()->WatchFileDescriptor( | 75 CHECK(base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 76 input_fd_, true, base::MessageLoopForIO::WATCH_READ, | 76 input_fd_, true, base::MessageLoopForIO::WATCH_READ, |
| 77 &read_socket_watcher_, this)); | 77 &read_socket_watcher_, this)); |
| 78 | 78 |
| 79 scoped_ptr<PacketPipe> tmp(new SendToFDPipe(output_fd)); | 79 std::unique_ptr<PacketPipe> tmp(new SendToFDPipe(output_fd)); |
| 80 if (packet_pipe_) { | 80 if (packet_pipe_) { |
| 81 packet_pipe_->AppendToPipe(std::move(tmp)); | 81 packet_pipe_->AppendToPipe(std::move(tmp)); |
| 82 } else { | 82 } else { |
| 83 packet_pipe_ = std::move(tmp); | 83 packet_pipe_ = std::move(tmp); |
| 84 } | 84 } |
| 85 packet_pipe_->InitOnIOThread(base::ThreadTaskRunnerHandle::Get(), | 85 packet_pipe_->InitOnIOThread(base::ThreadTaskRunnerHandle::Get(), |
| 86 &tick_clock_); | 86 &tick_clock_); |
| 87 } | 87 } |
| 88 | 88 |
| 89 ~QueueManager() final {} | 89 ~QueueManager() final {} |
| 90 | 90 |
| 91 // MessageLoopForIO::Watcher methods | 91 // MessageLoopForIO::Watcher methods |
| 92 void OnFileCanReadWithoutBlocking(int fd) final { | 92 void OnFileCanReadWithoutBlocking(int fd) final { |
| 93 scoped_ptr<Packet> packet(new Packet(kMaxPacketSize)); | 93 std::unique_ptr<Packet> packet(new Packet(kMaxPacketSize)); |
| 94 int nread = read(input_fd_, | 94 int nread = read(input_fd_, |
| 95 reinterpret_cast<char*>(&packet->front()), | 95 reinterpret_cast<char*>(&packet->front()), |
| 96 kMaxPacketSize); | 96 kMaxPacketSize); |
| 97 if (nread < 0) { | 97 if (nread < 0) { |
| 98 if (errno == EINTR) return; | 98 if (errno == EINTR) return; |
| 99 perror("read"); | 99 perror("read"); |
| 100 exit(1); | 100 exit(1); |
| 101 } | 101 } |
| 102 if (nread == 0) return; | 102 if (nread == 0) return; |
| 103 packet->resize(nread); | 103 packet->resize(nread); |
| 104 packet_pipe_->Send(std::move(packet)); | 104 packet_pipe_->Send(std::move(packet)); |
| 105 } | 105 } |
| 106 void OnFileCanWriteWithoutBlocking(int fd) final { NOTREACHED(); } | 106 void OnFileCanWriteWithoutBlocking(int fd) final { NOTREACHED(); } |
| 107 | 107 |
| 108 private: | 108 private: |
| 109 int input_fd_; | 109 int input_fd_; |
| 110 scoped_ptr<PacketPipe> packet_pipe_; | 110 std::unique_ptr<PacketPipe> packet_pipe_; |
| 111 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; | 111 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; |
| 112 base::DefaultTickClock tick_clock_; | 112 base::DefaultTickClock tick_clock_; |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 } // namespace test | 115 } // namespace test |
| 116 } // namespace cast | 116 } // namespace cast |
| 117 } // namespace media | 117 } // namespace media |
| 118 | 118 |
| 119 base::TimeTicks last_printout; | 119 base::TimeTicks last_printout; |
| 120 | 120 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 }; | 163 }; |
| 164 | 164 |
| 165 ByteCounter in_pipe_input_counter; | 165 ByteCounter in_pipe_input_counter; |
| 166 ByteCounter in_pipe_output_counter; | 166 ByteCounter in_pipe_output_counter; |
| 167 ByteCounter out_pipe_input_counter; | 167 ByteCounter out_pipe_input_counter; |
| 168 ByteCounter out_pipe_output_counter; | 168 ByteCounter out_pipe_output_counter; |
| 169 | 169 |
| 170 class ByteCounterPipe : public media::cast::test::PacketPipe { | 170 class ByteCounterPipe : public media::cast::test::PacketPipe { |
| 171 public: | 171 public: |
| 172 ByteCounterPipe(ByteCounter* counter) : counter_(counter) {} | 172 ByteCounterPipe(ByteCounter* counter) : counter_(counter) {} |
| 173 void Send(scoped_ptr<media::cast::Packet> packet) final { | 173 void Send(std::unique_ptr<media::cast::Packet> packet) final { |
| 174 counter_->Increment(packet->size()); | 174 counter_->Increment(packet->size()); |
| 175 pipe_->Send(std::move(packet)); | 175 pipe_->Send(std::move(packet)); |
| 176 } | 176 } |
| 177 private: | 177 private: |
| 178 ByteCounter* counter_; | 178 ByteCounter* counter_; |
| 179 }; | 179 }; |
| 180 | 180 |
| 181 void SetupByteCounters(scoped_ptr<media::cast::test::PacketPipe>* pipe, | 181 void SetupByteCounters(std::unique_ptr<media::cast::test::PacketPipe>* pipe, |
| 182 ByteCounter* pipe_input_counter, | 182 ByteCounter* pipe_input_counter, |
| 183 ByteCounter* pipe_output_counter) { | 183 ByteCounter* pipe_output_counter) { |
| 184 media::cast::test::PacketPipe* new_pipe = | 184 media::cast::test::PacketPipe* new_pipe = |
| 185 new ByteCounterPipe(pipe_input_counter); | 185 new ByteCounterPipe(pipe_input_counter); |
| 186 new_pipe->AppendToPipe(std::move(*pipe)); | 186 new_pipe->AppendToPipe(std::move(*pipe)); |
| 187 new_pipe->AppendToPipe(scoped_ptr<media::cast::test::PacketPipe>( | 187 new_pipe->AppendToPipe(std::unique_ptr<media::cast::test::PacketPipe>( |
| 188 new ByteCounterPipe(pipe_output_counter))); | 188 new ByteCounterPipe(pipe_output_counter))); |
| 189 pipe->reset(new_pipe); | 189 pipe->reset(new_pipe); |
| 190 } | 190 } |
| 191 | 191 |
| 192 void CheckByteCounters() { | 192 void CheckByteCounters() { |
| 193 base::TimeTicks now = base::TimeTicks::Now(); | 193 base::TimeTicks now = base::TimeTicks::Now(); |
| 194 in_pipe_input_counter.push(now); | 194 in_pipe_input_counter.push(now); |
| 195 in_pipe_output_counter.push(now); | 195 in_pipe_output_counter.push(now); |
| 196 out_pipe_input_counter.push(now); | 196 out_pipe_input_counter.push(now); |
| 197 out_pipe_output_counter.push(now); | 197 out_pipe_output_counter.push(now); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 base::CommandLine::Init(argc, argv); | 269 base::CommandLine::Init(argc, argv); |
| 270 InitLogging(logging::LoggingSettings()); | 270 InitLogging(logging::LoggingSettings()); |
| 271 | 271 |
| 272 if (argc < 4) { | 272 if (argc < 4) { |
| 273 fprintf(stderr, "Usage: tap_proxy tap1 tap2 type\n"); | 273 fprintf(stderr, "Usage: tap_proxy tap1 tap2 type\n"); |
| 274 fprintf(stderr, | 274 fprintf(stderr, |
| 275 "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"); |
| 276 exit(1); | 276 exit(1); |
| 277 } | 277 } |
| 278 | 278 |
| 279 scoped_ptr<media::cast::test::PacketPipe> in_pipe, out_pipe; | 279 std::unique_ptr<media::cast::test::PacketPipe> in_pipe, out_pipe; |
| 280 std::string network_type = argv[3]; | 280 std::string network_type = argv[3]; |
| 281 if (network_type == "perfect") { | 281 if (network_type == "perfect") { |
| 282 // No action needed. | 282 // No action needed. |
| 283 } else if (network_type == "good") { | 283 } else if (network_type == "good") { |
| 284 in_pipe = media::cast::test::GoodNetwork(); | 284 in_pipe = media::cast::test::GoodNetwork(); |
| 285 out_pipe = media::cast::test::GoodNetwork(); | 285 out_pipe = media::cast::test::GoodNetwork(); |
| 286 } else if (network_type == "wifi") { | 286 } else if (network_type == "wifi") { |
| 287 in_pipe = media::cast::test::WifiNetwork(); | 287 in_pipe = media::cast::test::WifiNetwork(); |
| 288 out_pipe = media::cast::test::WifiNetwork(); | 288 out_pipe = media::cast::test::WifiNetwork(); |
| 289 } else if (network_type == "bad") { | 289 } else if (network_type == "bad") { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 305 int fd2 = tun_alloc(argv[2], IFF_TAP); | 305 int fd2 = tun_alloc(argv[2], IFF_TAP); |
| 306 | 306 |
| 307 base::MessageLoopForIO message_loop; | 307 base::MessageLoopForIO message_loop; |
| 308 last_printout = base::TimeTicks::Now(); | 308 last_printout = base::TimeTicks::Now(); |
| 309 media::cast::test::QueueManager qm1(fd1, fd2, std::move(in_pipe)); | 309 media::cast::test::QueueManager qm1(fd1, fd2, std::move(in_pipe)); |
| 310 media::cast::test::QueueManager qm2(fd2, fd1, std::move(out_pipe)); | 310 media::cast::test::QueueManager qm2(fd2, fd1, std::move(out_pipe)); |
| 311 CheckByteCounters(); | 311 CheckByteCounters(); |
| 312 printf("Press Ctrl-C when done.\n"); | 312 printf("Press Ctrl-C when done.\n"); |
| 313 message_loop.Run(); | 313 message_loop.Run(); |
| 314 } | 314 } |
| OLD | NEW |