31 auto fd =
socket_guard { socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) };
33 throw std::runtime_error(
"cannot create socket");
35#if defined(__unix__) || defined(__APPLE__)
36 auto fdFlags = fcntl(*fd, F_GETFD, 0);
38 fcntl(*fd, F_SETFD, fdFlags | FD_CLOEXEC);
41 sockaddr_in addrin {};
42 addrin.sin_family = AF_INET;
43 addrin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
44 addrin.sin_port = htons(
static_cast<uint16_t
>(port));
46 if(connect(*fd,
reinterpret_cast<const sockaddr*
>(&addrin),
sizeof(addrin)) ==
SOCKET_ERROR)
52 mChannel = std::make_unique<BufferedIPCChannel>();
53 mChannel->StartConversation(fd.release(), callback);
61 auto result = WSAStartup(MAKEWORD(2, 2), &wsaData);
62 if (result != NO_ERROR)
63 throw std::runtime_error(
"WSAStartup failed");
65 mImpl = std::make_unique<Impl>(port, callback);
Interface for listening connection status changes.
virtual void OnConnectionError() noexcept=0
Called when connection attempt fails.
std::unique_ptr< BufferedIPCChannel > mChannel
Impl(int port, IPCChannelStatusCallback &callback)
std::unique_ptr< Impl > mImpl
IPCClient(int port, IPCChannelStatusCallback &callback)
Attempts to connect to a server, may fail with exception or with call to IPCChannelStatusCallback::On...
~IPCClient()
Closes connection if any.
RAII-style socket wrapper. Since socket is closed on wrapper destruction, initializing multiple guard...