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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 } | 46 } |
| 47 return found_path_string && found_port_string; | 47 return found_path_string && found_port_string; |
| 48 } | 48 } |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 std::string path_string_; | 51 std::string path_string_; |
| 52 std::string port_string_; | 52 std::string port_string_; |
| 53 DISALLOW_COPY_AND_ASSIGN(OrphanedTestServerFilter); | 53 DISALLOW_COPY_AND_ASSIGN(OrphanedTestServerFilter); |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 // Given a file descriptor, reads into |buffer| until |bytes_max| | |
| 57 // bytes has been read or an error has been encountered. Returns true | |
| 58 // if the read was successful. |remaining_time| is used as a timeout. | |
| 59 bool ReadData(base::TimeDelta* remaining_time, | |
|
cbentzel
2010/11/19 14:54:57
Nit: remaining_time is an input/output argument. A
akalin
2010/11/19 16:45:28
Done.
| |
| 60 int fd, ssize_t bytes_max, uint8* buffer) { | |
| 61 ssize_t bytes_read = 0; | |
| 62 base::Time previous_time = base::Time::Now(); | |
| 63 while (bytes_read < bytes_max) { | |
| 64 struct pollfd poll_fds[1]; | |
| 65 | |
| 66 poll_fds[0].fd = fd; | |
| 67 poll_fds[0].events = POLLIN | POLLPRI; | |
| 68 poll_fds[0].revents = 0; | |
| 69 | |
| 70 int rv = HANDLE_EINTR(poll(poll_fds, 1, | |
| 71 remaining_time->InMilliseconds())); | |
| 72 if (rv != 1) { | |
| 73 LOG(ERROR) << "Failed to poll for the child file descriptor."; | |
| 74 return false; | |
| 75 } | |
| 76 | |
| 77 base::Time current_time = base::Time::Now(); | |
| 78 base::TimeDelta elapsed_time_cycle = current_time - previous_time; | |
| 79 DCHECK(elapsed_time_cycle.InMilliseconds() >= 0); | |
| 80 *remaining_time -= elapsed_time_cycle; | |
| 81 previous_time = current_time; | |
| 82 | |
| 83 ssize_t num_bytes = HANDLE_EINTR(read(fd, buffer + bytes_read, | |
| 84 bytes_max - bytes_read)); | |
| 85 if (num_bytes <= 0) | |
| 86 return false; | |
| 87 bytes_read += num_bytes; | |
| 88 } | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 56 } // namespace | 92 } // namespace |
| 57 | 93 |
| 58 namespace net { | 94 namespace net { |
| 59 | 95 |
| 60 bool TestServer::LaunchPython(const FilePath& testserver_path) { | 96 bool TestServer::LaunchPython(const FilePath& testserver_path) { |
| 61 CommandLine python_command(FilePath(FILE_PATH_LITERAL("python"))); | 97 CommandLine python_command(FilePath(FILE_PATH_LITERAL("python"))); |
| 62 python_command.AppendArgPath(testserver_path); | 98 python_command.AppendArgPath(testserver_path); |
| 63 if (!AddCommandLineArguments(&python_command)) | 99 if (!AddCommandLineArguments(&python_command)) |
| 64 return false; | 100 return false; |
| 65 | 101 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 91 &process_handle_)) { | 127 &process_handle_)) { |
| 92 LOG(ERROR) << "Failed to launch " << python_command.command_line_string() | 128 LOG(ERROR) << "Failed to launch " << python_command.command_line_string() |
| 93 << " ..."; | 129 << " ..."; |
| 94 return false; | 130 return false; |
| 95 } | 131 } |
| 96 | 132 |
| 97 return true; | 133 return true; |
| 98 } | 134 } |
| 99 | 135 |
| 100 bool TestServer::WaitToStart() { | 136 bool TestServer::WaitToStart() { |
| 101 uint16 port; | 137 file_util::ScopedFD child_fd_closer(child_fd_closer_.release()); |
| 102 uint8* buffer = reinterpret_cast<uint8*>(&port); | 138 |
| 103 ssize_t bytes_read = 0; | |
| 104 ssize_t bytes_max = sizeof(port); | |
| 105 base::TimeDelta remaining_time = base::TimeDelta::FromMilliseconds( | 139 base::TimeDelta remaining_time = base::TimeDelta::FromMilliseconds( |
| 106 TestTimeouts::action_max_timeout_ms()); | 140 TestTimeouts::action_max_timeout_ms()); |
| 107 base::Time previous_time = base::Time::Now(); | 141 uint32 server_data_len = 0; |
| 108 while (bytes_read < bytes_max) { | 142 if (!ReadData(&remaining_time, child_fd_, sizeof(server_data_len), |
| 109 struct pollfd poll_fds[1]; | 143 reinterpret_cast<uint8*>(&server_data_len))) |
| 144 return false; | |
| 145 std::string server_data(server_data_len, '\0'); | |
| 146 if (!ReadData(&remaining_time, child_fd_, server_data_len, | |
| 147 reinterpret_cast<uint8*>(&server_data[0]))) | |
| 148 return false; | |
| 110 | 149 |
| 111 poll_fds[0].fd = child_fd_; | 150 return ParseServerData(server_data); |
| 112 poll_fds[0].events = POLLIN | POLLPRI; | |
| 113 poll_fds[0].revents = 0; | |
| 114 | |
| 115 int rv = HANDLE_EINTR(poll(poll_fds, 1, remaining_time.InMilliseconds())); | |
| 116 if (rv != 1) { | |
| 117 LOG(ERROR) << "Failed to poll for the child file descriptor."; | |
| 118 return false; | |
| 119 } | |
| 120 | |
| 121 base::Time current_time = base::Time::Now(); | |
| 122 base::TimeDelta elapsed_time_cycle = current_time - previous_time; | |
| 123 DCHECK(elapsed_time_cycle.InMilliseconds() >= 0); | |
| 124 remaining_time -= elapsed_time_cycle; | |
| 125 previous_time = current_time; | |
| 126 | |
| 127 ssize_t num_bytes = HANDLE_EINTR(read(child_fd_, buffer + bytes_read, | |
| 128 bytes_max - bytes_read)); | |
| 129 if (num_bytes <= 0) | |
| 130 break; | |
| 131 bytes_read += num_bytes; | |
| 132 } | |
| 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; | |
| 140 } | 151 } |
| 141 | 152 |
| 142 bool TestServer::CheckCATrusted() { | 153 bool TestServer::CheckCATrusted() { |
| 143 return true; | 154 return true; |
| 144 } | 155 } |
| 145 | 156 |
| 146 } // namespace net | 157 } // namespace net |
| OLD | NEW |