| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/command.h" | 5 #include "tools/android/forwarder2/command.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 // '08080' for port 8080) | 29 // '08080' for port 8080) |
| 30 // <type> is a 3-char zero-padded ASCII decimal integer | 30 // <type> is a 3-char zero-padded ASCII decimal integer |
| 31 // matching a command::Type value (e.g. 002 for | 31 // matching a command::Type value (e.g. 002 for |
| 32 // ACK). | 32 // ACK). |
| 33 // The column (:) is used as a separator for easier reading. | 33 // The column (:) is used as a separator for easier reading. |
| 34 const int kPortStringSize = 5; | 34 const int kPortStringSize = 5; |
| 35 const int kCommandTypeStringSize = 2; | 35 const int kCommandTypeStringSize = 2; |
| 36 // Command string size also includes the ':' separator char. | 36 // Command string size also includes the ':' separator char. |
| 37 const int kCommandStringSize = kPortStringSize + kCommandTypeStringSize + 1; | 37 const int kCommandStringSize = kPortStringSize + kCommandTypeStringSize + 1; |
| 38 | 38 |
| 39 const int kNoTimeout = -1; |
| 40 |
| 39 } // namespace | 41 } // namespace |
| 40 | 42 |
| 41 namespace forwarder2 { | 43 namespace forwarder2 { |
| 42 | 44 |
| 43 bool ReadCommand(Socket* socket, | 45 bool ReadCommand(Socket* socket, |
| 44 int* port_out, | 46 int* port_out, |
| 45 command::Type* command_type_out) { | 47 command::Type* command_type_out) { |
| 48 return ReadCommandWithTimeout(socket, port_out, command_type_out, kNoTimeout); |
| 49 } |
| 50 |
| 51 bool ReadCommandWithTimeout(Socket* socket, |
| 52 int* port_out, |
| 53 command::Type* command_type_out, |
| 54 int timeout_secs) { |
| 46 char command_buffer[kCommandStringSize + 1]; | 55 char command_buffer[kCommandStringSize + 1]; |
| 47 // To make logging easier. | 56 // To make logging easier. |
| 48 command_buffer[kCommandStringSize] = '\0'; | 57 command_buffer[kCommandStringSize] = '\0'; |
| 49 | 58 |
| 50 int bytes_read = socket->ReadNumBytes(command_buffer, kCommandStringSize); | 59 int bytes_read = socket->ReadNumBytesWithTimeout( |
| 60 command_buffer, kCommandStringSize, timeout_secs); |
| 51 if (bytes_read != kCommandStringSize) { | 61 if (bytes_read != kCommandStringSize) { |
| 52 if (bytes_read < 0) | 62 if (bytes_read < 0) |
| 53 LOG(ERROR) << "Read() error: " << base::safe_strerror(errno); | 63 LOG(ERROR) << "Read() error: " << base::safe_strerror(errno); |
| 54 else if (!bytes_read) | 64 else if (!bytes_read) |
| 55 LOG(ERROR) << "Read() error, endpoint was unexpectedly closed."; | 65 LOG(ERROR) << "Read() error, endpoint was unexpectedly closed."; |
| 56 else | 66 else |
| 57 LOG(ERROR) << "Read() error, not enough data received from the socket."; | 67 LOG(ERROR) << "Read() error, not enough data received from the socket."; |
| 58 return false; | 68 return false; |
| 59 } | 69 } |
| 60 | 70 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 79 | 89 |
| 80 bool SendCommand(command::Type command, int port, Socket* socket) { | 90 bool SendCommand(command::Type command, int port, Socket* socket) { |
| 81 char buffer[kCommandStringSize + 1]; | 91 char buffer[kCommandStringSize + 1]; |
| 82 int len = snprintf(buffer, sizeof(buffer), "%05d:%02d", port, command); | 92 int len = snprintf(buffer, sizeof(buffer), "%05d:%02d", port, command); |
| 83 CHECK_EQ(len, kCommandStringSize); | 93 CHECK_EQ(len, kCommandStringSize); |
| 84 // Write the full command minus the leading \0 char. | 94 // Write the full command minus the leading \0 char. |
| 85 return socket->WriteNumBytes(buffer, len) == len; | 95 return socket->WriteNumBytes(buffer, len) == len; |
| 86 } | 96 } |
| 87 | 97 |
| 88 bool ReceivedCommand(command::Type command, Socket* socket) { | 98 bool ReceivedCommand(command::Type command, Socket* socket) { |
| 99 return ReceivedCommandWithTimeout(command, socket, kNoTimeout); |
| 100 } |
| 101 |
| 102 bool ReceivedCommandWithTimeout(command::Type command, |
| 103 Socket* socket, |
| 104 int timeout_secs) { |
| 89 int port; | 105 int port; |
| 90 command::Type received_command; | 106 command::Type received_command; |
| 91 if (!ReadCommand(socket, &port, &received_command)) | 107 if (!ReadCommand(socket, &port, &received_command)) |
| 92 return false; | 108 return false; |
| 93 return received_command == command; | 109 return received_command == command; |
| 94 } | 110 } |
| 95 | 111 |
| 96 } // namespace forwarder | 112 } // namespace forwarder |
| OLD | NEW |