arc: Move platform2/arc/network/ to platform2/patchpanel
Next step in the arc-networkd -> patchpanel rename, this patch moves the
location of the code.
BUG=b:151879931
TEST=units,flashed image to atlas
TEST=tasts arc.PlayStore, crostini.LaunchTerminal.download
Change-Id: I1b5cf8d670e1631d46f6449b725395157bf88dde
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2115863
Tested-by: Garrick Evans <garrick@chromium.org>
Commit-Queue: Garrick Evans <garrick@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: Eric Caruso <ejcaruso@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Hugo Benichi <hugobenichi@google.com>
diff --git a/patchpanel/socket_forwarder.h b/patchpanel/socket_forwarder.h
new file mode 100644
index 0000000..f4c631c
--- /dev/null
+++ b/patchpanel/socket_forwarder.h
@@ -0,0 +1,58 @@
+// Copyright 2019 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PATCHPANEL_SOCKET_FORWARDER_H_
+#define PATCHPANEL_SOCKET_FORWARDER_H_
+
+#include <netinet/ip.h>
+#include <sys/socket.h>
+
+#include <atomic>
+#include <memory>
+#include <string>
+
+#include <base/macros.h>
+#include <base/memory/weak_ptr.h>
+#include <base/threading/simple_thread.h>
+#include <brillo/brillo_export.h>
+
+#include "patchpanel/socket.h"
+
+namespace patchpanel {
+// Forwards data between a pair of sockets.
+// This is a simple implementation as a thread main function.
+class BRILLO_EXPORT SocketForwarder : public base::SimpleThread {
+ public:
+ SocketForwarder(const std::string& name,
+ std::unique_ptr<Socket> sock0,
+ std::unique_ptr<Socket> sock1);
+ virtual ~SocketForwarder();
+
+ // Runs the forwarder. The sockets are closed and released on exit,
+ // so this can only be run once.
+ void Run() override;
+ bool IsRunning() const;
+
+ private:
+ static constexpr int kBufSize = 4096;
+
+ void Poll();
+ bool ProcessEvents(uint32_t events, int efd, int cfd);
+
+ std::unique_ptr<Socket> sock0_;
+ std::unique_ptr<Socket> sock1_;
+ char buf0_[kBufSize] = {0};
+ char buf1_[kBufSize] = {0};
+ ssize_t len0_;
+ ssize_t len1_;
+
+ std::atomic<bool> poll_;
+ std::atomic<bool> done_;
+
+ DISALLOW_COPY_AND_ASSIGN(SocketForwarder);
+};
+
+} // namespace patchpanel
+
+#endif // PATCHPANEL_SOCKET_FORWARDER_H_