summaryrefslogtreecommitdiffstats
path: root/tools/android/forwarder2/command.cc
blob: 9b9e401ce365318a8b29e4dbebe764ec18218d19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "tools/android/forwarder2/command.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "base/logging.h"
#include "base/string_number_conversions.h"
#include "base/string_piece.h"
#include "tools/android/forwarder2/socket.h"

using base::StringPiece;

namespace {


// Command format:
//      <port>:<type>
//
// Where:
//   <port> is a 5-chars zero-padded ASCII decimal integer
//          matching the target port for the command (e.g.
//          '08080' for port 8080)
//   <type> is a 3-char zero-padded ASCII decimal integer
//          matching a command::Type value (e.g. 002 for
//          ACK).
// The column (:) is used as a separator for easier reading.
const int kPortStringSize = 5;
const int kCommandTypeStringSize = 2;
// Command string size also includes the ':' separator char.
const int kCommandStringSize = kPortStringSize + kCommandTypeStringSize + 1;

}  // namespace

namespace forwarder2 {

bool ReadCommand(Socket* socket,
                 int* port_out,
                 command::Type* command_type_out) {
  char command_buffer[kCommandStringSize + 1];
  // To make logging easier.
  command_buffer[kCommandStringSize] = '\0';

  if (socket->ReadNumBytes(command_buffer, kCommandStringSize) !=
      kCommandStringSize) {
    LOG(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);
  // Write the full command minus the leading \0 char.
  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;
}

}  // namespace forwarder