| 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 #include "tools/android/forwarder2/forwarders_manager.h" | 5 #include "tools/android/forwarder2/forwarders_manager.h" |
| 6 | 6 |
| 7 #include <sys/select.h> | 7 #include <sys/select.h> |
| 8 #include <unistd.h> | 8 #include <unistd.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback_helpers.h" | 14 #include "base/callback_helpers.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/message_loop/message_loop_proxy.h" | |
| 18 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
| 18 #include "base/task_runner.h" |
| 19 #include "tools/android/forwarder2/forwarder.h" | 19 #include "tools/android/forwarder2/forwarder.h" |
| 20 #include "tools/android/forwarder2/socket.h" | 20 #include "tools/android/forwarder2/socket.h" |
| 21 | 21 |
| 22 namespace forwarder2 { | 22 namespace forwarder2 { |
| 23 | 23 |
| 24 ForwardersManager::ForwardersManager() : thread_("ForwardersManagerThread") { | 24 ForwardersManager::ForwardersManager() : thread_("ForwardersManagerThread") { |
| 25 thread_.Start(); | 25 thread_.Start(); |
| 26 WaitForEventsOnInternalThreadSoon(); | 26 WaitForEventsOnInternalThreadSoon(); |
| 27 } | 27 } |
| 28 | 28 |
| 29 | 29 |
| 30 ForwardersManager::~ForwardersManager() { | 30 ForwardersManager::~ForwardersManager() { |
| 31 deletion_notifier_.Notify(); | 31 deletion_notifier_.Notify(); |
| 32 } | 32 } |
| 33 | 33 |
| 34 void ForwardersManager::CreateAndStartNewForwarder(scoped_ptr<Socket> socket1, | 34 void ForwardersManager::CreateAndStartNewForwarder(scoped_ptr<Socket> socket1, |
| 35 scoped_ptr<Socket> socket2) { | 35 scoped_ptr<Socket> socket2) { |
| 36 // Note that the internal Forwarder vector is populated on the internal thread | 36 // Note that the internal Forwarder vector is populated on the internal thread |
| 37 // which is the only thread from which it's accessed. | 37 // which is the only thread from which it's accessed. |
| 38 thread_.message_loop_proxy()->PostTask( | 38 thread_.task_runner()->PostTask( |
| 39 FROM_HERE, | 39 FROM_HERE, |
| 40 base::Bind(&ForwardersManager::CreateNewForwarderOnInternalThread, | 40 base::Bind(&ForwardersManager::CreateNewForwarderOnInternalThread, |
| 41 base::Unretained(this), base::Passed(&socket1), | 41 base::Unretained(this), base::Passed(&socket1), |
| 42 base::Passed(&socket2))); | 42 base::Passed(&socket2))); |
| 43 | 43 |
| 44 // Guarantees that the CreateNewForwarderOnInternalThread callback posted to | 44 // Guarantees that the CreateNewForwarderOnInternalThread callback posted to |
| 45 // the internal thread gets executed immediately. | 45 // the internal thread gets executed immediately. |
| 46 wakeup_notifier_.Notify(); | 46 wakeup_notifier_.Notify(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void ForwardersManager::CreateNewForwarderOnInternalThread( | 49 void ForwardersManager::CreateNewForwarderOnInternalThread( |
| 50 scoped_ptr<Socket> socket1, | 50 scoped_ptr<Socket> socket1, |
| 51 scoped_ptr<Socket> socket2) { | 51 scoped_ptr<Socket> socket2) { |
| 52 DCHECK(thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); | 52 DCHECK(thread_.task_runner()->RunsTasksOnCurrentThread()); |
| 53 forwarders_.push_back(new Forwarder(socket1.Pass(), socket2.Pass())); | 53 forwarders_.push_back(new Forwarder(socket1.Pass(), socket2.Pass())); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void ForwardersManager::WaitForEventsOnInternalThreadSoon() { | 56 void ForwardersManager::WaitForEventsOnInternalThreadSoon() { |
| 57 thread_.message_loop_proxy()->PostTask( | 57 thread_.task_runner()->PostTask( |
| 58 FROM_HERE, | 58 FROM_HERE, base::Bind(&ForwardersManager::WaitForEventsOnInternalThread, |
| 59 base::Bind(&ForwardersManager::WaitForEventsOnInternalThread, | 59 base::Unretained(this))); |
| 60 base::Unretained(this))); | |
| 61 } | 60 } |
| 62 | 61 |
| 63 void ForwardersManager::WaitForEventsOnInternalThread() { | 62 void ForwardersManager::WaitForEventsOnInternalThread() { |
| 64 DCHECK(thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); | 63 DCHECK(thread_.task_runner()->RunsTasksOnCurrentThread()); |
| 65 fd_set read_fds; | 64 fd_set read_fds; |
| 66 fd_set write_fds; | 65 fd_set write_fds; |
| 67 | 66 |
| 68 FD_ZERO(&read_fds); | 67 FD_ZERO(&read_fds); |
| 69 FD_ZERO(&write_fds); | 68 FD_ZERO(&write_fds); |
| 70 | 69 |
| 71 // Populate the file descriptor sets. | 70 // Populate the file descriptor sets. |
| 72 int max_fd = -1; | 71 int max_fd = -1; |
| 73 for (ScopedVector<Forwarder>::iterator it = forwarders_.begin(); | 72 for (ScopedVector<Forwarder>::iterator it = forwarders_.begin(); |
| 74 it != forwarders_.end(); ++it) { | 73 it != forwarders_.end(); ++it) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 ++i; | 122 ++i; |
| 124 continue; | 123 continue; |
| 125 } | 124 } |
| 126 | 125 |
| 127 std::swap(forwarders_[i], forwarders_.back()); | 126 std::swap(forwarders_[i], forwarders_.back()); |
| 128 forwarders_.pop_back(); | 127 forwarders_.pop_back(); |
| 129 } | 128 } |
| 130 } | 129 } |
| 131 | 130 |
| 132 } // namespace forwarder2 | 131 } // namespace forwarder2 |
| OLD | NEW |