15#include "llvm/Config/config.h"
25#include <sys/socket.h>
38#if defined(HAVE_UNISTD_H)
45WSABalancer::WSABalancer() {
47 ::memset(&WsaData, 0,
sizeof(WsaData));
48 if (WSAStartup(MAKEWORD(2, 2), &WsaData) != 0) {
53WSABalancer::~WSABalancer() { WSACleanup(); }
58 return std::error_code(::WSAGetLastError(), std::system_category());
65 struct sockaddr_un
Addr;
67 Addr.sun_family = AF_UNIX;
68 strncpy(
Addr.sun_path, SocketPath.
str().c_str(),
sizeof(
Addr.sun_path) - 1);
74 SOCKET Socket = socket(AF_UNIX, SOCK_STREAM, 0);
75 if (Socket == INVALID_SOCKET) {
77 int Socket = socket(AF_UNIX, SOCK_STREAM, 0);
81 "Create socket failed");
85 if (::connect(Socket, (
struct sockaddr *)&
Addr,
sizeof(
Addr)) == -1)
87 "Connect socket failed");
90 return _open_osfhandle(Socket, 0);
96ListeningSocket::ListeningSocket(
int SocketFD,
StringRef SocketPath,
98 : FD(SocketFD), SocketPath(SocketPath), PipeFD{PipeFD[0], PipeFD[1]} {}
101 : FD(LS.FD.
load()), SocketPath(LS.SocketPath),
102 PipeFD{LS.PipeFD[0], LS.PipeFD[1]} {
105 LS.SocketPath.clear();
127 return llvm::make_error<StringError>(
128 std::make_error_code(std::errc::file_exists),
129 "Socket address unavailable");
131 ::close(std::move(*MaybeFD));
134 return llvm::make_error<StringError>(
135 std::make_error_code(std::errc::address_in_use),
136 "Socket address unavailable");
141 SOCKET Socket = socket(AF_UNIX, SOCK_STREAM, 0);
142 if (Socket == INVALID_SOCKET)
144 int Socket = socket(AF_UNIX, SOCK_STREAM, 0);
148 "socket create failed");
151 if (::bind(Socket, (
struct sockaddr *)&
Addr,
sizeof(
Addr)) == -1) {
155 return llvm::make_error<StringError>(EC,
"Bind error");
159 if (::listen(Socket, MaxBacklog) == -1)
166 if (::_pipe(PipeFD, 1, 0) == -1)
168 if (::pipe(PipeFD) == -1)
174 return ListeningSocket{_open_osfhandle(Socket, 0), SocketPath, PipeFD};
183 struct pollfd FDs[2];
184 FDs[0].events = POLLIN;
186 SOCKET WinServerSock = _get_osfhandle(FD);
187 FDs[0].fd = WinServerSock;
191 FDs[1].events = POLLIN;
192 FDs[1].fd = PipeFD[0];
196 int RemainingTime = Timeout.count();
197 std::chrono::milliseconds ElapsedTime = std::chrono::milliseconds(0);
200 while (PollStatus == -1 && (Timeout.count() == -1 || ElapsedTime < Timeout)) {
201 if (Timeout.count() != -1)
202 RemainingTime -= ElapsedTime.count();
204 auto Start = std::chrono::steady_clock::now();
206 PollStatus = WSAPoll(FDs, 2, RemainingTime);
207 if (PollStatus == SOCKET_ERROR) {
209 PollStatus = ::poll(FDs, 2, RemainingTime);
210 if (PollStatus == -1) {
214 if (PollErrCode != std::errc::interrupted)
215 return llvm::make_error<StringError>(PollErrCode,
"FD poll failed");
219 return llvm::make_error<StringError>(
220 std::make_error_code(std::errc::timed_out),
221 "No client requests within timeout window");
223 if (FDs[0].revents & POLLNVAL)
224 return llvm::make_error<StringError>(
225 std::make_error_code(std::errc::bad_file_descriptor),
226 "File descriptor closed by another thread");
228 if (FDs[1].revents & POLLIN)
229 return llvm::make_error<StringError>(
230 std::make_error_code(std::errc::operation_canceled),
233 auto Stop = std::chrono::steady_clock::now();
235 std::chrono::duration_cast<std::chrono::milliseconds>(Stop - Start);
240 SOCKET WinAcceptSock =
::accept(WinServerSock, NULL, NULL);
241 AcceptFD = _open_osfhandle(WinAcceptSock, 0);
243 AcceptFD =
::accept(FD, NULL, NULL);
248 "Socket accept failed");
249 return std::make_unique<raw_socket_stream>(AcceptFD);
253 int ObservedFD = FD.load();
255 if (ObservedFD == -1)
260 if (!FD.compare_exchange_strong(ObservedFD, -1))
264 ::unlink(SocketPath.c_str());
268 ssize_t written =
::write(PipeFD[1], &Byte, 1);
303 return std::make_unique<raw_socket_stream>(*FD);
AMDGPU Mark last scratch load
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
Manages a passive (i.e., listening) UNIX domain socket.
static Expected< ListeningSocket > createUnix(StringRef SocketPath, int MaxBacklog=llvm::hardware_concurrency().compute_thread_count())
Creates a listening socket bound to the specified file system path.
void shutdown()
Closes the FD, unlinks the socket file, and writes to PipeFD.
Expected< std::unique_ptr< raw_socket_stream > > accept(std::chrono::milliseconds Timeout=std::chrono::milliseconds(-1))
Accepts an incoming connection on the listening socket.
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
A raw_ostream of a file for reading/writing/seeking.
static Expected< std::unique_ptr< raw_socket_stream > > createConnectedUnix(StringRef SocketPath)
Create a raw_socket_stream connected to the UNIX domain socket at SocketPath.
raw_socket_stream(int SocketFD)
bool exists(const basic_file_status &status)
Does file exist?
This is an optimization pass for GlobalISel generic memory operations.
Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
std::error_code errnoAsErrorCode()
Helper to get errno as an std::error_code.
void consumeError(Error Err)
Consume a Error without doing anything.
static Expected< int > getSocketFD(StringRef SocketPath)
static std::error_code getLastSocketErrorCode()
static sockaddr_un setSocketAddr(StringRef SocketPath)