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 <stddef.h> | 7 #include <stddef.h> |
8 #include <sys/select.h> | 8 #include <sys/select.h> |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
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/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ptr_util.h" |
18 #include "base/posix/eintr_wrapper.h" | 19 #include "base/posix/eintr_wrapper.h" |
19 #include "tools/android/forwarder2/forwarder.h" | 20 #include "tools/android/forwarder2/forwarder.h" |
20 #include "tools/android/forwarder2/socket.h" | 21 #include "tools/android/forwarder2/socket.h" |
21 | 22 |
22 namespace forwarder2 { | 23 namespace forwarder2 { |
23 | 24 |
24 ForwardersManager::ForwardersManager() : thread_("ForwardersManagerThread") { | 25 ForwardersManager::ForwardersManager() : thread_("ForwardersManagerThread") { |
25 thread_.Start(); | 26 thread_.Start(); |
26 WaitForEventsOnInternalThreadSoon(); | 27 WaitForEventsOnInternalThreadSoon(); |
27 } | 28 } |
(...skipping 16 matching lines...) Expand all Loading... |
44 | 45 |
45 // Guarantees that the CreateNewForwarderOnInternalThread callback posted to | 46 // Guarantees that the CreateNewForwarderOnInternalThread callback posted to |
46 // the internal thread gets executed immediately. | 47 // the internal thread gets executed immediately. |
47 wakeup_notifier_.Notify(); | 48 wakeup_notifier_.Notify(); |
48 } | 49 } |
49 | 50 |
50 void ForwardersManager::CreateNewForwarderOnInternalThread( | 51 void ForwardersManager::CreateNewForwarderOnInternalThread( |
51 std::unique_ptr<Socket> socket1, | 52 std::unique_ptr<Socket> socket1, |
52 std::unique_ptr<Socket> socket2) { | 53 std::unique_ptr<Socket> socket2) { |
53 DCHECK(thread_.task_runner()->RunsTasksInCurrentSequence()); | 54 DCHECK(thread_.task_runner()->RunsTasksInCurrentSequence()); |
54 forwarders_.push_back(new Forwarder(std::move(socket1), std::move(socket2))); | 55 forwarders_.push_back( |
| 56 base::MakeUnique<Forwarder>(std::move(socket1), std::move(socket2))); |
55 } | 57 } |
56 | 58 |
57 void ForwardersManager::WaitForEventsOnInternalThreadSoon() { | 59 void ForwardersManager::WaitForEventsOnInternalThreadSoon() { |
58 thread_.task_runner()->PostTask( | 60 thread_.task_runner()->PostTask( |
59 FROM_HERE, | 61 FROM_HERE, |
60 base::Bind(&ForwardersManager::WaitForEventsOnInternalThread, | 62 base::Bind(&ForwardersManager::WaitForEventsOnInternalThread, |
61 base::Unretained(this))); | 63 base::Unretained(this))); |
62 } | 64 } |
63 | 65 |
64 void ForwardersManager::WaitForEventsOnInternalThread() { | 66 void ForwardersManager::WaitForEventsOnInternalThread() { |
65 DCHECK(thread_.task_runner()->RunsTasksInCurrentSequence()); | 67 DCHECK(thread_.task_runner()->RunsTasksInCurrentSequence()); |
66 fd_set read_fds; | 68 fd_set read_fds; |
67 fd_set write_fds; | 69 fd_set write_fds; |
68 | 70 |
69 FD_ZERO(&read_fds); | 71 FD_ZERO(&read_fds); |
70 FD_ZERO(&write_fds); | 72 FD_ZERO(&write_fds); |
71 | 73 |
72 // Populate the file descriptor sets. | 74 // Populate the file descriptor sets. |
73 int max_fd = -1; | 75 int max_fd = -1; |
74 for (ScopedVector<Forwarder>::iterator it = forwarders_.begin(); | 76 for (const auto& forwarder : forwarders_) |
75 it != forwarders_.end(); ++it) { | |
76 Forwarder* const forwarder = *it; | |
77 forwarder->RegisterFDs(&read_fds, &write_fds, &max_fd); | 77 forwarder->RegisterFDs(&read_fds, &write_fds, &max_fd); |
78 } | |
79 | 78 |
80 const int notifier_fds[] = { | 79 const int notifier_fds[] = { |
81 wakeup_notifier_.receiver_fd(), | 80 wakeup_notifier_.receiver_fd(), |
82 deletion_notifier_.receiver_fd(), | 81 deletion_notifier_.receiver_fd(), |
83 }; | 82 }; |
84 | 83 |
85 for (size_t i = 0; i < arraysize(notifier_fds); ++i) { | 84 for (size_t i = 0; i < arraysize(notifier_fds); ++i) { |
86 const int notifier_fd = notifier_fds[i]; | 85 const int notifier_fd = notifier_fds[i]; |
87 DCHECK_GT(notifier_fd, -1); | 86 DCHECK_GT(notifier_fd, -1); |
88 FD_SET(notifier_fd, &read_fds); | 87 FD_SET(notifier_fd, &read_fds); |
(...skipping 18 matching lines...) Expand all Loading... |
107 | 106 |
108 if (FD_ISSET(wakeup_notifier_.receiver_fd(), &read_fds)) { | 107 if (FD_ISSET(wakeup_notifier_.receiver_fd(), &read_fds)) { |
109 // Note that the events on FDs other than the wakeup notifier one, if any, | 108 // Note that the events on FDs other than the wakeup notifier one, if any, |
110 // will be processed upon the next select(). | 109 // will be processed upon the next select(). |
111 wakeup_notifier_.Reset(); | 110 wakeup_notifier_.Reset(); |
112 return; | 111 return; |
113 } | 112 } |
114 | 113 |
115 // Notify the Forwarder instances and remove the ones that are closed. | 114 // Notify the Forwarder instances and remove the ones that are closed. |
116 for (size_t i = 0; i < forwarders_.size(); ) { | 115 for (size_t i = 0; i < forwarders_.size(); ) { |
117 Forwarder* const forwarder = forwarders_[i]; | 116 Forwarder* const forwarder = forwarders_[i].get(); |
118 forwarder->ProcessEvents(read_fds, write_fds); | 117 forwarder->ProcessEvents(read_fds, write_fds); |
119 | 118 |
120 if (must_shutdown) | 119 if (must_shutdown) |
121 forwarder->Shutdown(); | 120 forwarder->Shutdown(); |
122 | 121 |
123 if (!forwarder->IsClosed()) { | 122 if (!forwarder->IsClosed()) { |
124 ++i; | 123 ++i; |
125 continue; | 124 continue; |
126 } | 125 } |
127 | 126 |
128 std::swap(forwarders_[i], forwarders_.back()); | 127 std::swap(forwarders_[i], forwarders_.back()); |
129 forwarders_.pop_back(); | 128 forwarders_.pop_back(); |
130 } | 129 } |
131 } | 130 } |
132 | 131 |
133 } // namespace forwarder2 | 132 } // namespace forwarder2 |
OLD | NEW |