| 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> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/safe_strerror_posix.h" |
| 13 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 14 #include "base/string_piece.h" | 15 #include "base/string_piece.h" |
| 15 #include "tools/android/forwarder2/socket.h" | 16 #include "tools/android/forwarder2/socket.h" |
| 16 | 17 |
| 17 using base::StringPiece; | 18 using base::StringPiece; |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 | 22 |
| 22 // Command format: | 23 // Command format: |
| (...skipping 16 matching lines...) Expand all Loading... |
| 39 | 40 |
| 40 namespace forwarder2 { | 41 namespace forwarder2 { |
| 41 | 42 |
| 42 bool ReadCommand(Socket* socket, | 43 bool ReadCommand(Socket* socket, |
| 43 int* port_out, | 44 int* port_out, |
| 44 command::Type* command_type_out) { | 45 command::Type* command_type_out) { |
| 45 char command_buffer[kCommandStringSize + 1]; | 46 char command_buffer[kCommandStringSize + 1]; |
| 46 // To make logging easier. | 47 // To make logging easier. |
| 47 command_buffer[kCommandStringSize] = '\0'; | 48 command_buffer[kCommandStringSize] = '\0'; |
| 48 | 49 |
| 49 if (socket->ReadNumBytes(command_buffer, kCommandStringSize) != | 50 int bytes_read = socket->ReadNumBytes(command_buffer, kCommandStringSize); |
| 50 kCommandStringSize) { | 51 if (bytes_read != kCommandStringSize) { |
| 51 LOG(ERROR) << "Not enough data received from the socket."; | 52 if (bytes_read < 0) |
| 53 LOG(ERROR) << "Read() error: " << safe_strerror(errno); |
| 54 else if (!bytes_read) |
| 55 LOG(ERROR) << "Read() error, endpoint was unexpectedly closed."; |
| 56 else |
| 57 LOG(ERROR) << "Read() error, not enough data received from the socket."; |
| 52 return false; | 58 return false; |
| 53 } | 59 } |
| 54 | 60 |
| 55 StringPiece port_str(command_buffer, kPortStringSize); | 61 StringPiece port_str(command_buffer, kPortStringSize); |
| 56 if (!StringToInt(port_str, port_out)) { | 62 if (!StringToInt(port_str, port_out)) { |
| 57 LOG(ERROR) << "Could not parse the command port string: " | 63 LOG(ERROR) << "Could not parse the command port string: " |
| 58 << port_str; | 64 << port_str; |
| 59 return false; | 65 return false; |
| 60 } | 66 } |
| 61 | 67 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 81 | 87 |
| 82 bool ReceivedCommand(command::Type command, Socket* socket) { | 88 bool ReceivedCommand(command::Type command, Socket* socket) { |
| 83 int port; | 89 int port; |
| 84 command::Type received_command; | 90 command::Type received_command; |
| 85 if (!ReadCommand(socket, &port, &received_command)) | 91 if (!ReadCommand(socket, &port, &received_command)) |
| 86 return false; | 92 return false; |
| 87 return received_command == command; | 93 return received_command == command; |
| 88 } | 94 } |
| 89 | 95 |
| 90 } // namespace forwarder | 96 } // namespace forwarder |
| OLD | NEW |