[llvm] Add raw_socket_stream (PR #73603)
Michael Spencer via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 30 03:12:31 PST 2023
================
@@ -630,6 +630,31 @@ class raw_fd_stream : public raw_fd_ostream {
static bool classof(const raw_ostream *OS);
};
+//===----------------------------------------------------------------------===//
+// Socket Streams
+//===----------------------------------------------------------------------===//
+
+/// A raw stream for sockets reading/writing
+
+class raw_socket_stream : public raw_fd_ostream {
+ StringRef SocketPath;
+ bool ShouldUnlink;
+
+ uint64_t current_pos() const override { return 0; }
+
+public:
+ int get_socket() { return get_fd(); }
+
+ static int MakeServerSocket(StringRef SocketPath, unsigned int MaxBacklog,
+ std::error_code &EC);
+
+ raw_socket_stream(int SocketFD, StringRef SockPath, std::error_code &EC);
+ raw_socket_stream(StringRef SockPath, std::error_code &EC);
+ ~raw_socket_stream();
+
+ Expected<std::string> read_impl();
+};
----------------
Bigcheese wrote:
I think a better interface would be something like:
```suggestion
class raw_socket_stream;
class ListeningSocket {
ListeningSocket(int SocketFD);
// ...
public:
static Expected<ListeningSocket> createUnix(StringRef SocketPath);
static Expected<raw_socket_stream> accept();
~ListeningSocket();
};
class raw_socket_stream : public raw_fd_stream {
raw_socket_stream(int SocketFD);
uint64_t current_pos() const override { return 0; }
public:
/// Create a \p raw_socket_stream connected to the Unix domain socket at \p
/// SocketPath.
static Expected<raw_socket_stream> createConnectedUnix(StringRef SocketPath);
~raw_socket_stream();
};
```
This separates out the listening socket into its own type, and makes the constructors of `raw_socket_stream` much less confusing about what type of socket it's creating. It also means API users never need to deal with raw `int` FDs.
This is different from how `raw_fd_ostream` handles errors, but that was written a long time ago, this is the preferred way now.
Both classes should be made move only, and `ListeningSocket`'s destructor can handle removing the socket file.
https://github.com/llvm/llvm-project/pull/73603
More information about the llvm-commits
mailing list