This source file includes following definitions.
- ReadCommand
- SendCommand
- ReceivedCommand
#include "tools/android/forwarder2/command.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base/logging.h"
#include "base/safe_strerror_posix.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "tools/android/forwarder2/socket.h"
using base::StringPiece;
namespace {
const int kPortStringSize = 5;
const int kCommandTypeStringSize = 2;
const int kCommandStringSize = kPortStringSize + kCommandTypeStringSize + 1;
}
namespace forwarder2 {
bool ReadCommand(Socket* socket,
int* port_out,
command::Type* command_type_out) {
char command_buffer[kCommandStringSize + 1];
command_buffer[kCommandStringSize] = '\0';
int bytes_read = socket->ReadNumBytes(command_buffer, kCommandStringSize);
if (bytes_read != kCommandStringSize) {
if (bytes_read < 0)
LOG(ERROR) << "Read() error: " << safe_strerror(errno);
else if (!bytes_read)
LOG(ERROR) << "Read() error, endpoint was unexpectedly closed.";
else
LOG(ERROR) << "Read() error, not enough data received from the socket.";
return false;
}
StringPiece port_str(command_buffer, kPortStringSize);
if (!StringToInt(port_str, port_out)) {
LOG(ERROR) << "Could not parse the command port string: "
<< port_str;
return false;
}
StringPiece command_type_str(
&command_buffer[kPortStringSize + 1], kCommandTypeStringSize);
int command_type;
if (!StringToInt(command_type_str, &command_type)) {
LOG(ERROR) << "Could not parse the command type string: "
<< command_type_str;
return false;
}
*command_type_out = static_cast<command::Type>(command_type);
return true;
}
bool SendCommand(command::Type command, int port, Socket* socket) {
char buffer[kCommandStringSize + 1];
int len = snprintf(buffer, sizeof(buffer), "%05d:%02d", port, command);
CHECK_EQ(len, kCommandStringSize);
return socket->WriteNumBytes(buffer, len) == len;
}
bool ReceivedCommand(command::Type command, Socket* socket) {
int port;
command::Type received_command;
if (!ReadCommand(socket, &port, &received_command))
return false;
return received_command == command;
}
}