Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "net/test/test_server.h" | 5 #include "net/test/test_server.h" |
| 6 | 6 |
| 7 #include <poll.h> | 7 #include <poll.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 if (!base::LaunchApp(python_command.argv(), map_write_fd, false, | 90 if (!base::LaunchApp(python_command.argv(), map_write_fd, false, |
| 91 &process_handle_)) { | 91 &process_handle_)) { |
| 92 LOG(ERROR) << "Failed to launch " << python_command.command_line_string() | 92 LOG(ERROR) << "Failed to launch " << python_command.command_line_string() |
| 93 << " ..."; | 93 << " ..."; |
| 94 return false; | 94 return false; |
| 95 } | 95 } |
| 96 | 96 |
| 97 return true; | 97 return true; |
| 98 } | 98 } |
| 99 | 99 |
| 100 bool TestServer::WaitToStart() { | 100 namespace { |
| 101 uint16 port; | 101 |
| 102 uint8* buffer = reinterpret_cast<uint8*>(&port); | 102 bool ReadData(base::TimeDelta* remaining_time, |
|
cbentzel
2010/11/17 22:32:50
General style I've seen is only one anonymous name
akalin
2010/11/17 23:21:35
Yeah...I like putting utility functions close to w
cbentzel
2010/11/18 01:47:19
I prefer your approach as well.
| |
| 103 int fd, ssize_t bytes_max, uint8* buffer) { | |
| 103 ssize_t bytes_read = 0; | 104 ssize_t bytes_read = 0; |
| 104 ssize_t bytes_max = sizeof(port); | |
| 105 base::TimeDelta remaining_time = base::TimeDelta::FromMilliseconds( | |
| 106 TestTimeouts::action_max_timeout_ms()); | |
| 107 base::Time previous_time = base::Time::Now(); | 105 base::Time previous_time = base::Time::Now(); |
| 108 while (bytes_read < bytes_max) { | 106 while (bytes_read < bytes_max) { |
| 109 struct pollfd poll_fds[1]; | 107 struct pollfd poll_fds[1]; |
| 110 | 108 |
| 111 poll_fds[0].fd = child_fd_; | 109 poll_fds[0].fd = fd; |
| 112 poll_fds[0].events = POLLIN | POLLPRI; | 110 poll_fds[0].events = POLLIN | POLLPRI; |
| 113 poll_fds[0].revents = 0; | 111 poll_fds[0].revents = 0; |
| 114 | 112 |
| 115 int rv = HANDLE_EINTR(poll(poll_fds, 1, remaining_time.InMilliseconds())); | 113 int rv = HANDLE_EINTR(poll(poll_fds, 1, |
| 114 remaining_time->InMilliseconds())); | |
| 116 if (rv != 1) { | 115 if (rv != 1) { |
| 117 LOG(ERROR) << "Failed to poll for the child file descriptor."; | 116 LOG(ERROR) << "Failed to poll for the child file descriptor."; |
| 118 return false; | 117 return false; |
| 119 } | 118 } |
| 120 | 119 |
| 121 base::Time current_time = base::Time::Now(); | 120 base::Time current_time = base::Time::Now(); |
| 122 base::TimeDelta elapsed_time_cycle = current_time - previous_time; | 121 base::TimeDelta elapsed_time_cycle = current_time - previous_time; |
| 123 DCHECK(elapsed_time_cycle.InMilliseconds() >= 0); | 122 DCHECK(elapsed_time_cycle.InMilliseconds() >= 0); |
| 124 remaining_time -= elapsed_time_cycle; | 123 *remaining_time -= elapsed_time_cycle; |
| 125 previous_time = current_time; | 124 previous_time = current_time; |
| 126 | 125 |
| 127 ssize_t num_bytes = HANDLE_EINTR(read(child_fd_, buffer + bytes_read, | 126 ssize_t num_bytes = HANDLE_EINTR(read(fd, buffer + bytes_read, |
| 128 bytes_max - bytes_read)); | 127 bytes_max - bytes_read)); |
| 129 if (num_bytes <= 0) | 128 if (num_bytes <= 0) |
| 130 break; | 129 return false; |
| 131 bytes_read += num_bytes; | 130 bytes_read += num_bytes; |
| 132 } | 131 } |
| 133 | |
| 134 // We don't need the FD anymore. | |
| 135 child_fd_closer_.reset(NULL); | |
| 136 if (bytes_read < bytes_max) | |
| 137 return false; | |
| 138 host_port_pair_.set_port(port); | |
| 139 return true; | 132 return true; |
| 140 } | 133 } |
| 141 | 134 |
| 135 } // namespace | |
| 136 | |
| 137 bool TestServer::WaitToStart() { | |
| 138 file_util::ScopedFD child_fd_closer(child_fd_closer_.release()); | |
|
cbentzel
2010/11/17 22:32:50
Nice change over explicit reset.
| |
| 139 | |
| 140 base::TimeDelta remaining_time = base::TimeDelta::FromMilliseconds( | |
| 141 TestTimeouts::action_max_timeout_ms()); | |
| 142 uint32 server_data_len = 0; | |
| 143 if (!ReadData(&remaining_time, child_fd_, sizeof server_data_len, | |
|
cbentzel
2010/11/17 22:32:50
I haven't seen "sizeof <expression>" before, but i
akalin
2010/11/17 23:21:35
Done.
| |
| 144 reinterpret_cast<uint8*>(&server_data_len))) | |
| 145 return false; | |
| 146 std::string server_data(server_data_len, '\0'); | |
|
cbentzel
2010/11/17 22:32:50
Why can't you just reserve server_data_len bytes i
akalin
2010/11/17 23:21:35
reserve() doesn't actually change the size of the
cbentzel
2010/11/18 01:47:19
You're right, I wasn't thinking.
| |
| 147 if (!ReadData(&remaining_time, child_fd_, server_data_len, | |
| 148 reinterpret_cast<uint8*>(&server_data[0]))) | |
| 149 return false; | |
| 150 | |
| 151 if (!ParseServerData(server_data)) | |
| 152 return false; | |
| 153 return true; | |
| 154 } | |
| 155 | |
| 142 bool TestServer::CheckCATrusted() { | 156 bool TestServer::CheckCATrusted() { |
| 143 return true; | 157 return true; |
| 144 } | 158 } |
| 145 | 159 |
| 146 } // namespace net | 160 } // namespace net |
| OLD | NEW |