| 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 |
| 20 #include <deque> | 21 #include <deque> |
| 21 #include <map> | 22 #include <map> |
| 23 #include <memory> |
| 22 #include <utility> | 24 #include <utility> |
| 23 | 25 |
| 24 #include "base/at_exit.h" | 26 #include "base/at_exit.h" |
| 25 #include "base/bind.h" | 27 #include "base/bind.h" |
| 26 #include "base/command_line.h" | 28 #include "base/command_line.h" |
| 29 #include "base/files/file_descriptor_watcher_posix.h" |
| 27 #include "base/logging.h" | 30 #include "base/logging.h" |
| 28 #include "base/rand_util.h" | 31 #include "base/rand_util.h" |
| 29 #include "base/run_loop.h" | 32 #include "base/run_loop.h" |
| 30 #include "base/single_thread_task_runner.h" | 33 #include "base/single_thread_task_runner.h" |
| 31 #include "base/synchronization/waitable_event.h" | 34 #include "base/synchronization/waitable_event.h" |
| 32 #include "base/threading/thread.h" | 35 #include "base/threading/thread.h" |
| 33 #include "base/threading/thread_task_runner_handle.h" | 36 #include "base/threading/thread_task_runner_handle.h" |
| 34 #include "base/time/default_tick_clock.h" | 37 #include "base/time/default_tick_clock.h" |
| 35 #include "media/cast/test/utility/udp_proxy.h" | 38 #include "media/cast/test/utility/udp_proxy.h" |
| 36 #include "net/base/io_buffer.h" | 39 #include "net/base/io_buffer.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 62 fprintf(stderr, "Truncated write!\n"); | 65 fprintf(stderr, "Truncated write!\n"); |
| 63 exit(1); | 66 exit(1); |
| 64 } | 67 } |
| 65 break; | 68 break; |
| 66 } | 69 } |
| 67 } | 70 } |
| 68 private: | 71 private: |
| 69 int fd_; | 72 int fd_; |
| 70 }; | 73 }; |
| 71 | 74 |
| 72 class QueueManager : public base::MessageLoopForIO::Watcher { | 75 class QueueManager { |
| 73 public: | 76 public: |
| 74 QueueManager(int input_fd, int output_fd, std::unique_ptr<PacketPipe> pipe) | 77 QueueManager(int input_fd, int output_fd, std::unique_ptr<PacketPipe> pipe) |
| 75 : input_fd_(input_fd), packet_pipe_(std::move(pipe)) { | 78 : input_fd_(input_fd), packet_pipe_(std::move(pipe)) { |
| 76 CHECK(base::MessageLoopForIO::current()->WatchFileDescriptor( | 79 read_socket_watch_controller_ = base::FileDescriptorWatcher::WatchReadable( |
| 77 input_fd_, true, base::MessageLoopForIO::WATCH_READ, | 80 input_fd_, base::Bind(&QueueManager::OnFileCanReadWithoutBlocking, |
| 78 &read_socket_watcher_, this)); | 81 base::Unretained(this))); |
| 79 | 82 |
| 80 std::unique_ptr<PacketPipe> tmp(new SendToFDPipe(output_fd)); | 83 std::unique_ptr<PacketPipe> tmp(new SendToFDPipe(output_fd)); |
| 81 if (packet_pipe_) { | 84 if (packet_pipe_) { |
| 82 packet_pipe_->AppendToPipe(std::move(tmp)); | 85 packet_pipe_->AppendToPipe(std::move(tmp)); |
| 83 } else { | 86 } else { |
| 84 packet_pipe_ = std::move(tmp); | 87 packet_pipe_ = std::move(tmp); |
| 85 } | 88 } |
| 86 packet_pipe_->InitOnIOThread(base::ThreadTaskRunnerHandle::Get(), | 89 packet_pipe_->InitOnIOThread(base::ThreadTaskRunnerHandle::Get(), |
| 87 &tick_clock_); | 90 &tick_clock_); |
| 88 } | 91 } |
| 89 | 92 |
| 90 ~QueueManager() final {} | 93 private: |
| 91 | 94 void OnFileCanReadWithoutBlocking() { |
| 92 // MessageLoopForIO::Watcher methods | |
| 93 void OnFileCanReadWithoutBlocking(int fd) final { | |
| 94 std::unique_ptr<Packet> packet(new Packet(kMaxPacketSize)); | 95 std::unique_ptr<Packet> packet(new Packet(kMaxPacketSize)); |
| 95 int nread = read(input_fd_, | 96 int nread = read(input_fd_, |
| 96 reinterpret_cast<char*>(&packet->front()), | 97 reinterpret_cast<char*>(&packet->front()), |
| 97 kMaxPacketSize); | 98 kMaxPacketSize); |
| 98 if (nread < 0) { | 99 if (nread < 0) { |
| 99 if (errno == EINTR) return; | 100 if (errno == EINTR) return; |
| 100 perror("read"); | 101 perror("read"); |
| 101 exit(1); | 102 exit(1); |
| 102 } | 103 } |
| 103 if (nread == 0) return; | 104 if (nread == 0) return; |
| 104 packet->resize(nread); | 105 packet->resize(nread); |
| 105 packet_pipe_->Send(std::move(packet)); | 106 packet_pipe_->Send(std::move(packet)); |
| 106 } | 107 } |
| 107 void OnFileCanWriteWithoutBlocking(int fd) final { NOTREACHED(); } | |
| 108 | 108 |
| 109 private: | |
| 110 int input_fd_; | 109 int input_fd_; |
| 111 std::unique_ptr<PacketPipe> packet_pipe_; | 110 std::unique_ptr<PacketPipe> packet_pipe_; |
| 112 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; | 111 std::unique_ptr<base::FileDescriptorWatcher::Controller> |
| 112 read_socket_watch_controller_; |
| 113 base::DefaultTickClock tick_clock_; | 113 base::DefaultTickClock tick_clock_; |
| 114 }; | 114 }; |
| 115 | 115 |
| 116 } // namespace test | 116 } // namespace test |
| 117 } // namespace cast | 117 } // namespace cast |
| 118 } // namespace media | 118 } // namespace media |
| 119 | 119 |
| 120 base::TimeTicks last_printout; | 120 base::TimeTicks last_printout; |
| 121 | 121 |
| 122 class ByteCounter { | 122 class ByteCounter { |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 } | 299 } |
| 300 | 300 |
| 301 SetupByteCounters(&in_pipe, &in_pipe_input_counter, &in_pipe_output_counter); | 301 SetupByteCounters(&in_pipe, &in_pipe_input_counter, &in_pipe_output_counter); |
| 302 SetupByteCounters( | 302 SetupByteCounters( |
| 303 &out_pipe, &out_pipe_input_counter, &out_pipe_output_counter); | 303 &out_pipe, &out_pipe_input_counter, &out_pipe_output_counter); |
| 304 | 304 |
| 305 int fd1 = tun_alloc(argv[1], IFF_TAP); | 305 int fd1 = tun_alloc(argv[1], IFF_TAP); |
| 306 int fd2 = tun_alloc(argv[2], IFF_TAP); | 306 int fd2 = tun_alloc(argv[2], IFF_TAP); |
| 307 | 307 |
| 308 base::MessageLoopForIO message_loop; | 308 base::MessageLoopForIO message_loop; |
| 309 base::FileDescriptorWatcher file_descriptor_watcher(&message_loop); |
| 309 last_printout = base::TimeTicks::Now(); | 310 last_printout = base::TimeTicks::Now(); |
| 310 media::cast::test::QueueManager qm1(fd1, fd2, std::move(in_pipe)); | 311 media::cast::test::QueueManager qm1(fd1, fd2, std::move(in_pipe)); |
| 311 media::cast::test::QueueManager qm2(fd2, fd1, std::move(out_pipe)); | 312 media::cast::test::QueueManager qm2(fd2, fd1, std::move(out_pipe)); |
| 312 CheckByteCounters(); | 313 CheckByteCounters(); |
| 313 printf("Press Ctrl-C when done.\n"); | 314 printf("Press Ctrl-C when done.\n"); |
| 314 base::RunLoop().Run(); | 315 base::RunLoop().Run(); |
| 315 } | 316 } |
| OLD | NEW |