Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(394)

Unified Diff: tools/android/forwarder2/command.cc

Issue 10918057: Add forwarder2 (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/android/forwarder2/command.h ('k') | tools/android/forwarder2/device_controller.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/android/forwarder2/command.cc
diff --git a/tools/android/forwarder2/command.cc b/tools/android/forwarder2/command.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0a79a539a87624fd7e2fcb831188898b7a87a2ee
--- /dev/null
+++ b/tools/android/forwarder2/command.cc
@@ -0,0 +1,84 @@
+// 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:COMMAND_TYPE
+// This is a non-null-terminated string.
+// _PORT is a 5-char zero padded ascii number in decimal format (including the
+// '_' char).
+// ':' is used for separating the port and the command_type for simpler reading.
+// COMMAND_TYPE is the enum command::Type printed as a decimal ASCII string. It
+// is a 3-char zero padded ascii number in decimal format.
+const int kPortStringSize = 5;
+const int kCommandTypeStringSize = 2;
+// Command string size also includes the ':' separator char.
+const int kCommandStringSize = kPortStringSize + kCommandTypeStringSize + 1;
+
+} // namespace
+
+namespace forwarder {
+
+bool ReadCommand(Socket* socket, int* port, command::Type* command_type) {
+ char command_str[kCommandStringSize + 1];
+ // To make logging easier.
+ command_str[kCommandStringSize] = '\0';
+
+ if (socket->ReadNumBytes(command_str, kCommandStringSize) !=
+ kCommandStringSize) {
+ LOG(ERROR) << "Not enough data received from the socket.";
+ return false;
+ }
+
+ StringPiece port_str(command_str, kPortStringSize);
+ if (!StringToInt(port_str, port)) {
+ LOG(ERROR) << "Could not parse the command port string: "
+ << port_str;
+ return false;
+ }
+
+ StringPiece command_type_str(
+ &command_str[kPortStringSize + 1], kCommandTypeStringSize);
+ int cmd_type;
+ if (!StringToInt(command_type_str, &cmd_type)) {
+ LOG(ERROR) << "Could not parse the command type string: "
+ << command_type_str;
+ return false;
+ }
+ *command_type = static_cast<command::Type>(cmd_type);
+ return true;
+}
+
+bool SendCommand(command::Type cmd, int port, Socket* socket) {
+ char buffer[kCommandStringSize + 1];
+ int len = snprintf(buffer, sizeof(buffer), "%05d:%02d", port, cmd);
+ // Write the full command minus the leading \0 char.
+ return socket->WriteNumBytes(buffer, len) == len;
+}
+
+bool ReceivedCommand(command::Type cmd, Socket* socket) {
+ int port;
+ command::Type received_cmd;
+ if (!ReadCommand(socket, &port, &received_cmd)) {
+ return false;
+ }
+ return received_cmd == cmd;
+}
+
+} // namespace forwarder
« no previous file with comments | « tools/android/forwarder2/command.h ('k') | tools/android/forwarder2/device_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698