Moves tcp_socket.cpp and tcp_socket.h into common

I'm planning on using these for the socket_forward_proxy. There isn't
anything in the tcp_sockets that I would expect any difference from on
host vs guest.
All the vnc changes are either modifying the #include path or renaming
cvd::vnc::Message to cvd::Message

Change-Id: I20f34d95347cfabd37bc97cc837f02e8ef67b1d2
(cherry picked from commit 0594a5c23b091fd47ba5f1348059268aedfc32a8)
diff --git a/common/libs/Android.bp b/common/libs/Android.bp
index 572e0fa..648d657 100644
--- a/common/libs/Android.bp
+++ b/common/libs/Android.bp
@@ -17,6 +17,7 @@
     "auto_resources",
     "fs",
     "net",
+    "tcp_socket",
     "threads",
     "time",
     "wifi",
diff --git a/common/libs/tcp_socket/Android.bp b/common/libs/tcp_socket/Android.bp
new file mode 100644
index 0000000..d60ccb2
--- /dev/null
+++ b/common/libs/tcp_socket/Android.bp
@@ -0,0 +1,31 @@
+//
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+cc_library_shared {
+    name: "cuttlefish_tcp_socket",
+    srcs: [
+        "tcp_socket.cpp",
+    ],
+    shared_libs: [
+        "cuttlefish_auto_resources",
+        "libbase",
+        "libcuttlefish_fs",
+        "liblog",
+    ],
+    header_libs: [
+        "cuttlefish_glog",
+    ],
+    defaults: ["cuttlefish_host_and_guest"],
+}
diff --git a/host/frontend/vnc_server/tcp_socket.cpp b/common/libs/tcp_socket/tcp_socket.cpp
similarity index 88%
rename from host/frontend/vnc_server/tcp_socket.cpp
rename to common/libs/tcp_socket/tcp_socket.cpp
index 92aed93..2c0e281 100644
--- a/host/frontend/vnc_server/tcp_socket.cpp
+++ b/common/libs/tcp_socket/tcp_socket.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "host/frontend/vnc_server/tcp_socket.h"
+#include "common/libs/tcp_socket/tcp_socket.h"
 
 #include <netinet/in.h>
 #include <sys/socket.h>
@@ -24,12 +24,10 @@
 
 #include <glog/logging.h>
 
-using cvd::SharedFD;
-using cvd::vnc::ClientSocket;
-using cvd::vnc::Message;
-using cvd::vnc::ServerSocket;
+using cvd::ClientSocket;
+using cvd::ServerSocket;
 
-Message ClientSocket::Recv(size_t length) {
+cvd::Message ClientSocket::Recv(size_t length) {
   Message buf(length);
   ssize_t total_read = 0;
   while (total_read < static_cast<ssize_t>(length)) {
@@ -53,7 +51,7 @@
   while (written < static_cast<ssize_t>(size)) {
     auto just_written = fd_->Write(data + written, size - written);
     if (just_written <= 0) {
-      LOG(INFO) << "Couldn't write to vnc client: " << strerror(errno);
+      LOG(INFO) << "Couldn't write to client: " << strerror(errno);
       return just_written;
     }
     written += just_written;
diff --git a/host/frontend/vnc_server/tcp_socket.h b/common/libs/tcp_socket/tcp_socket.h
similarity index 94%
rename from host/frontend/vnc_server/tcp_socket.h
rename to common/libs/tcp_socket/tcp_socket.h
index f926642..e22bd87 100644
--- a/host/frontend/vnc_server/tcp_socket.h
+++ b/common/libs/tcp_socket/tcp_socket.h
@@ -16,16 +16,17 @@
  * limitations under the License.
  */
 
-#include "host/frontend/vnc_server/vnc_utils.h"
+#include "common/libs/fs/shared_fd.h"
 
 #include <unistd.h>
 
 #include <cstddef>
 #include <cstdint>
 #include <mutex>
+#include <vector>
 
 namespace cvd {
-namespace vnc {
+using Message = std::vector<std::uint8_t>;
 
 class ServerSocket;
 
@@ -76,5 +77,4 @@
   cvd::SharedFD fd_;
 };
 
-}  // namespace vnc
 }  // namespace cvd
diff --git a/guest/frontend/vnc_server/Android.mk b/guest/frontend/vnc_server/Android.mk
index cf6fc7d..e0a4e0d 100644
--- a/guest/frontend/vnc_server/Android.mk
+++ b/guest/frontend/vnc_server/Android.mk
@@ -42,7 +42,6 @@
     jpeg_compressor.cpp \
     main.cpp \
     simulated_hw_composer.cpp \
-    tcp_socket.cpp \
     virtual_inputs.cpp \
     vnc_client_connection.cpp \
     vnc_server.cpp \
@@ -59,6 +58,7 @@
     libutils \
     libcutils \
     cuttlefish_auto_resources \
+    cuttlefish_tcp_socket \
     libcuttlefish_fs \
     vsoc_lib \
     libvsocframebuffer
diff --git a/guest/frontend/vnc_server/jpeg_compressor.cpp b/guest/frontend/vnc_server/jpeg_compressor.cpp
index 560f9d8..f0b2ca6 100644
--- a/guest/frontend/vnc_server/jpeg_compressor.cpp
+++ b/guest/frontend/vnc_server/jpeg_compressor.cpp
@@ -41,11 +41,11 @@
 }
 }  // namespace
 
-cvd::vnc::Message JpegCompressor::Compress(const Message& frame,
-                                           int jpeg_quality, std::uint16_t x,
-                                           std::uint16_t y, std::uint16_t width,
-                                           std::uint16_t height,
-                                           int screen_width) {
+cvd::Message JpegCompressor::Compress(const Message& frame,
+                                      int jpeg_quality, std::uint16_t x,
+                                      std::uint16_t y, std::uint16_t width,
+                                      std::uint16_t height,
+                                      int screen_width) {
   jpeg_compress_struct cinfo{};
   jpeg_error_mgr err{};
   InitCinfo(&cinfo, &err, width, height, jpeg_quality);
diff --git a/guest/frontend/vnc_server/tcp_socket.cpp b/guest/frontend/vnc_server/tcp_socket.cpp
deleted file mode 100644
index c83559b..0000000
--- a/guest/frontend/vnc_server/tcp_socket.cpp
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "tcp_socket.h"
-#include <cerrno>
-
-#include <cutils/sockets.h>
-#define LOG_TAG ""
-#include <cutils/log.h>
-
-using cvd::vnc::ClientSocket;
-using cvd::vnc::ServerSocket;
-using cvd::vnc::Message;
-
-Message ClientSocket::Recv(size_t length) {
-  Message buf(length);
-  ssize_t total_read = 0;
-  while (total_read < static_cast<ssize_t>(length)) {
-    auto just_read = read(fd_, &buf[total_read], buf.size() - total_read);
-    if (just_read <= 0) {
-      if (just_read < 0) {
-        ALOGE("read() error: %s", strerror(errno));
-      }
-      other_side_closed_ = true;
-      return Message{};
-    }
-    total_read += just_read;
-  }
-  ALOG_ASSERT(total_read == static_cast<ssize_t>(length));
-  return buf;
-}
-
-ssize_t ClientSocket::Send(const uint8_t* data, std::size_t size) {
-  std::lock_guard<std::mutex> lock(send_lock_);
-  ssize_t written{};
-  while (written < static_cast<ssize_t>(size)) {
-    auto just_written = write(fd_, data + written, size - written);
-    if (just_written <= 0) {
-      ALOGI("Couldn't write to vnc client: %s", strerror(errno));
-      return just_written;
-    }
-    written += just_written;
-  }
-  return written;
-}
-
-ssize_t ClientSocket::Send(const Message& message) {
-  return Send(&message[0], message.size());
-}
-
-ServerSocket::ServerSocket(int port)
-    : fd_{socket_inaddr_any_server(port, SOCK_STREAM)} {
-  if (fd_ < 0) {
-    LOG_FATAL("Couldn't open streaming server on port %d", port);
-  }
-}
-
-ClientSocket ServerSocket::Accept() {
-  int client = accept(fd_, nullptr, nullptr);
-  if (client < 0) {
-    LOG_FATAL("Error attemping to accept: %s", strerror(errno));
-  }
-  return ClientSocket{client};
-}
diff --git a/guest/frontend/vnc_server/tcp_socket.h b/guest/frontend/vnc_server/tcp_socket.h
deleted file mode 100644
index c8c1b32..0000000
--- a/guest/frontend/vnc_server/tcp_socket.h
+++ /dev/null
@@ -1,99 +0,0 @@
-#pragma once
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "vnc_utils.h"
-
-#include <mutex>
-#include <cstdint>
-#include <cstddef>
-
-#include <unistd.h>
-
-namespace cvd {
-namespace vnc {
-
-class ServerSocket;
-
-// Recv and Send wait until all data has been received or sent.
-// Send is thread safe in this regard, Recv is not.
-class ClientSocket {
- public:
-  ClientSocket(ClientSocket&& other) : fd_{other.fd_} {
-    other.fd_ = -1;
-  }
-
-  ClientSocket& operator=(ClientSocket&& other) {
-    if (fd_ >= 0) {
-      close(fd_);
-    }
-    fd_ = other.fd_;
-    other.fd_ = -1;
-    return *this;
-  }
-
-  ClientSocket(const ClientSocket&) = delete;
-  ClientSocket& operator=(const ClientSocket&) = delete;
-
-  ~ClientSocket() {
-    if (fd_ >= 0) {
-      close(fd_);
-    }
-  }
-
-  Message Recv(std::size_t length);
-  ssize_t Send(const std::uint8_t* data, std::size_t size);
-  ssize_t Send(const Message& message);
-
-  template <std::size_t N>
-  ssize_t Send(const std::uint8_t (&data)[N]) {
-    return Send(data, N);
-  }
-
-  bool closed() const {
-    return other_side_closed_;
-  }
-
- private:
-  friend ServerSocket;
-  explicit ClientSocket(int fd) : fd_(fd) {}
-
-  int fd_ = -1;
-  bool other_side_closed_{};
-  std::mutex send_lock_;
-};
-
-class ServerSocket {
- public:
-  explicit ServerSocket(int port);
-
-  ServerSocket(const ServerSocket&) = delete;
-  ServerSocket& operator=(const ServerSocket&) = delete;
-
-  ~ServerSocket() {
-    if (fd_ >= 0) {
-      close(fd_);
-    }
-  }
-
-  ClientSocket Accept();
-
- private:
-  int fd_ = -1;
-};
-
-}  // namespace vnc
-}  // namespace cvd
diff --git a/guest/frontend/vnc_server/vnc_client_connection.cpp b/guest/frontend/vnc_server/vnc_client_connection.cpp
index 6122017..957e7e7 100644
--- a/guest/frontend/vnc_server/vnc_client_connection.cpp
+++ b/guest/frontend/vnc_server/vnc_client_connection.cpp
@@ -16,9 +16,10 @@
 
 #include "vnc_client_connection.h"
 #include "keysyms.h"
-#include "tcp_socket.h"
 #include "vnc_utils.h"
 
+#include "common/libs/tcp_socket/tcp_socket.h"
+
 #include <guest/libs/legacy_framebuffer/vsoc_framebuffer.h>
 
 #include <netinet/in.h>
@@ -42,7 +43,7 @@
 #include <cutils/log.h>
 #include <cutils/sockets.h>
 
-using cvd::vnc::Message;
+using cvd::Message;
 using cvd::vnc::Stripe;
 using cvd::vnc::StripePtrVec;
 using cvd::vnc::VncClientConnection;
diff --git a/guest/frontend/vnc_server/vnc_client_connection.h b/guest/frontend/vnc_server/vnc_client_connection.h
index b2fe6e7..e17eb96 100644
--- a/guest/frontend/vnc_server/vnc_client_connection.h
+++ b/guest/frontend/vnc_server/vnc_client_connection.h
@@ -18,7 +18,8 @@
 #include "blackboard.h"
 #include "virtual_inputs.h"
 #include "vnc_utils.h"
-#include "tcp_socket.h"
+
+#include "common/libs/tcp_socket/tcp_socket.h"
 
 #include <android-base/thread_annotations.h>
 #include <common/libs/fs/shared_fd.h>
diff --git a/guest/frontend/vnc_server/vnc_server.cpp b/guest/frontend/vnc_server/vnc_server.cpp
index 4b1ce89..ba23c45 100644
--- a/guest/frontend/vnc_server/vnc_server.cpp
+++ b/guest/frontend/vnc_server/vnc_server.cpp
@@ -14,15 +14,18 @@
  * limitations under the License.
  */
 
+
+
 #include "blackboard.h"
 #include "frame_buffer_watcher.h"
 #include "jpeg_compressor.h"
-#include "tcp_socket.h"
 #include "virtual_inputs.h"
 #include "vnc_client_connection.h"
 #include "vnc_server.h"
 #include "vnc_utils.h"
 
+#include "common/libs/tcp_socket/tcp_socket.h"
+
 using cvd::vnc::VncServer;
 
 VncServer::VncServer(int port, bool aggressive)
diff --git a/guest/frontend/vnc_server/vnc_server.h b/guest/frontend/vnc_server/vnc_server.h
index f50947e..ed961ed 100644
--- a/guest/frontend/vnc_server/vnc_server.h
+++ b/guest/frontend/vnc_server/vnc_server.h
@@ -18,15 +18,18 @@
 #include "blackboard.h"
 #include "frame_buffer_watcher.h"
 #include "jpeg_compressor.h"
-#include "tcp_socket.h"
 #include "virtual_inputs.h"
 #include "vnc_client_connection.h"
 #include "vnc_utils.h"
 
+#include "common/libs/tcp_socket/tcp_socket.h"
+
 #include <thread>
 #include <string>
 #include <utility>
 
+
+
 namespace cvd {
 namespace vnc {
 
diff --git a/guest/frontend/vnc_server/vnc_utils.h b/guest/frontend/vnc_server/vnc_utils.h
index 5a996a9..3a23d45 100644
--- a/guest/frontend/vnc_server/vnc_utils.h
+++ b/guest/frontend/vnc_server/vnc_utils.h
@@ -22,6 +22,8 @@
 #include <utility>
 #include <cstdint>
 
+#include "common/libs/tcp_socket/tcp_socket.h"
+
 #undef D
 #ifdef VSOC_VNC_DEBUG
 #define D(...) ALOGD(__VA_ARGS__)
@@ -51,8 +53,6 @@
   std::uint64_t t_{};
 };
 
-using Message = std::vector<std::uint8_t>;
-
 constexpr int32_t kJpegMaxQualityEncoding = -23;
 constexpr int32_t kJpegMinQualityEncoding = -32;
 
diff --git a/host/frontend/vnc_server/Android.bp b/host/frontend/vnc_server/Android.bp
index b5ec78b..66db3f6 100644
--- a/host/frontend/vnc_server/Android.bp
+++ b/host/frontend/vnc_server/Android.bp
@@ -21,7 +21,6 @@
         "jpeg_compressor.cpp",
         "main.cpp",
         "simulated_hw_composer.cpp",
-        "tcp_socket.cpp",
         "virtual_inputs.cpp",
         "virtual_input_device.cpp",
         "vnc_client_connection.cpp",
@@ -34,6 +33,7 @@
     shared_libs: [
         "vsoc_lib",
         "libcuttlefish_fs",
+        "cuttlefish_tcp_socket",
         "cuttlefish_auto_resources",
         "libbase",
     ],
diff --git a/host/frontend/vnc_server/jpeg_compressor.cpp b/host/frontend/vnc_server/jpeg_compressor.cpp
index 711a762..87f75fd 100644
--- a/host/frontend/vnc_server/jpeg_compressor.cpp
+++ b/host/frontend/vnc_server/jpeg_compressor.cpp
@@ -39,11 +39,11 @@
 }
 }  // namespace
 
-cvd::vnc::Message JpegCompressor::Compress(const Message& frame,
-                                           int jpeg_quality, std::uint16_t x,
-                                           std::uint16_t y, std::uint16_t width,
-                                           std::uint16_t height,
-                                           int screen_width) {
+cvd::Message JpegCompressor::Compress(const Message& frame,
+                                      int jpeg_quality, std::uint16_t x,
+                                      std::uint16_t y, std::uint16_t width,
+                                      std::uint16_t height,
+                                      int screen_width) {
   jpeg_compress_struct cinfo{};
   jpeg_error_mgr err{};
   InitCinfo(&cinfo, &err, width, height, jpeg_quality);
diff --git a/host/frontend/vnc_server/vnc_client_connection.cpp b/host/frontend/vnc_server/vnc_client_connection.cpp
index e38a336..da94600 100644
--- a/host/frontend/vnc_server/vnc_client_connection.cpp
+++ b/host/frontend/vnc_server/vnc_client_connection.cpp
@@ -32,12 +32,12 @@
 
 #include <gflags/gflags.h>
 #include <glog/logging.h>
+#include "common/libs/tcp_socket/tcp_socket.h"
 #include "host/frontend/vnc_server/keysyms.h"
 #include "host/frontend/vnc_server/mocks.h"
-#include "host/frontend/vnc_server/tcp_socket.h"
 #include "host/frontend/vnc_server/vnc_utils.h"
 
-using cvd::vnc::Message;
+using cvd::Message;
 using cvd::vnc::Stripe;
 using cvd::vnc::StripePtrVec;
 using cvd::vnc::VncClientConnection;
diff --git a/host/frontend/vnc_server/vnc_client_connection.h b/host/frontend/vnc_server/vnc_client_connection.h
index 9b403bb..71beddf 100644
--- a/host/frontend/vnc_server/vnc_client_connection.h
+++ b/host/frontend/vnc_server/vnc_client_connection.h
@@ -25,8 +25,8 @@
 #include <thread>
 #include <vector>
 
+#include "common/libs/tcp_socket/tcp_socket.h"
 #include "host/frontend/vnc_server/blackboard.h"
-#include "host/frontend/vnc_server/tcp_socket.h"
 #include "host/frontend/vnc_server/virtual_inputs.h"
 #include "host/frontend/vnc_server/vnc_utils.h"
 
diff --git a/host/frontend/vnc_server/vnc_server.cpp b/host/frontend/vnc_server/vnc_server.cpp
index 2c013af..03f5dbe 100644
--- a/host/frontend/vnc_server/vnc_server.cpp
+++ b/host/frontend/vnc_server/vnc_server.cpp
@@ -17,10 +17,10 @@
 #include "host/frontend/vnc_server/vnc_server.h"
 
 #include <glog/logging.h>
+#include "common/libs/tcp_socket/tcp_socket.h"
 #include "host/frontend/vnc_server/blackboard.h"
 #include "host/frontend/vnc_server/frame_buffer_watcher.h"
 #include "host/frontend/vnc_server/jpeg_compressor.h"
-#include "host/frontend/vnc_server/tcp_socket.h"
 #include "host/frontend/vnc_server/virtual_inputs.h"
 #include "host/frontend/vnc_server/vnc_client_connection.h"
 #include "host/frontend/vnc_server/vnc_utils.h"
diff --git a/host/frontend/vnc_server/vnc_server.h b/host/frontend/vnc_server/vnc_server.h
index 7b50367..d88835c 100644
--- a/host/frontend/vnc_server/vnc_server.h
+++ b/host/frontend/vnc_server/vnc_server.h
@@ -20,10 +20,10 @@
 #include <thread>
 #include <utility>
 
+#include "common/libs/tcp_socket/tcp_socket.h"
 #include "host/frontend/vnc_server/blackboard.h"
 #include "host/frontend/vnc_server/frame_buffer_watcher.h"
 #include "host/frontend/vnc_server/jpeg_compressor.h"
-#include "host/frontend/vnc_server/tcp_socket.h"
 #include "host/frontend/vnc_server/virtual_inputs.h"
 #include "host/frontend/vnc_server/vnc_client_connection.h"
 #include "host/frontend/vnc_server/vnc_utils.h"
diff --git a/host/frontend/vnc_server/vnc_utils.h b/host/frontend/vnc_server/vnc_utils.h
index f6aaecc..f6bb44a 100644
--- a/host/frontend/vnc_server/vnc_utils.h
+++ b/host/frontend/vnc_server/vnc_utils.h
@@ -22,6 +22,7 @@
 #include <vector>
 
 #include "common/vsoc/lib/fb_bcast_region_view.h"
+#include "common/libs/tcp_socket/tcp_socket.h"
 
 namespace cvd {
 namespace vnc {
@@ -41,8 +42,6 @@
   std::uint64_t t_{};
 };
 
-using Message = std::vector<std::uint8_t>;
-
 constexpr int32_t kJpegMaxQualityEncoding = -23;
 constexpr int32_t kJpegMinQualityEncoding = -32;