1 //===-- llvm/Support/raw_socket_stream.h - Socket streams --*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains raw_ostream implementations for streams to communicate 10 // via UNIX sockets 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_SUPPORT_RAW_SOCKET_STREAM_H 15 #define LLVM_SUPPORT_RAW_SOCKET_STREAM_H 16 17 #include "llvm/Support/Threading.h" 18 #include "llvm/Support/raw_ostream.h" 19 20 namespace llvm { 21 22 class raw_socket_stream; 23 24 // Make sure that calls to WSAStartup and WSACleanup are balanced. 25 #ifdef _WIN32 26 class WSABalancer { 27 public: 28 WSABalancer(); 29 ~WSABalancer(); 30 }; 31 #endif // _WIN32 32 33 class ListeningSocket { 34 int FD; 35 std::string SocketPath; 36 ListeningSocket(int SocketFD, StringRef SocketPath); 37 #ifdef _WIN32 38 WSABalancer _; 39 #endif // _WIN32 40 41 public: 42 static Expected<ListeningSocket> createUnix( 43 StringRef SocketPath, 44 int MaxBacklog = llvm::hardware_concurrency().compute_thread_count()); 45 Expected<std::unique_ptr<raw_socket_stream>> accept(); 46 ListeningSocket(ListeningSocket &&LS); 47 ~ListeningSocket(); 48 }; 49 class raw_socket_stream : public raw_fd_stream { current_pos()50 uint64_t current_pos() const override { return 0; } 51 #ifdef _WIN32 52 WSABalancer _; 53 #endif // _WIN32 54 55 public: 56 raw_socket_stream(int SocketFD); 57 /// Create a \p raw_socket_stream connected to the Unix domain socket at \p 58 /// SocketPath. 59 static Expected<std::unique_ptr<raw_socket_stream>> 60 createConnectedUnix(StringRef SocketPath); 61 ~raw_socket_stream(); 62 }; 63 64 } // end namespace llvm 65 66 #endif 67