39 throw std::runtime_error(
"cannot create socket");
41 sockaddr_in addrhint{};
42 addrhint.sin_family = AF_INET;
43 addrhint.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
44 addrhint.sin_port = htons(INADDR_ANY);
46 static const int yes { 1 };
47 if(setsockopt(*
mListenSocket, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<const char*
>(&yes),
sizeof(yes)) ==
SOCKET_ERROR)
48 throw std::runtime_error(
"cannot configure listen socket");
51 throw std::runtime_error(
"socket bind error");
54 throw std::runtime_error(
"socket listen error");
57 socklen_t addr_len {
sizeof (addr) };
59 throw std::runtime_error(
"failed to get socket name");
63 mChannel = std::make_unique<BufferedIPCChannel>();
73 std::lock_guard lck {
mSync};
93 fd_set readfds, exceptfds;
124 std::lock_guard lck{
mSync};
140 auto result = WSAStartup(MAKEWORD(2, 2), &wsaData);
141 if (result != NO_ERROR)
142 throw std::runtime_error(
"WSAStartup failed");
144 mImpl = std::make_unique<Impl>(callback);
151 return mImpl->GetConnectPort();
Interface for listening connection status changes.
virtual void OnConnectionError() noexcept=0
Called when connection attempt fails.
socket_guard mListenSocket
int GetConnectPort() const noexcept
std::unique_ptr< BufferedIPCChannel > mChannel
Impl(IPCChannelStatusCallback &callback)
std::unique_ptr< std::thread > mConnectionRoutine
std::unique_ptr< Impl > mImpl
IPCServer(IPCChannelStatusCallback &callback)
May fail with exception or with call to IPCChannelStatusCallback::OnConnectionError....
~IPCServer()
Closes connection if any.
int GetConnectPort() const noexcept
RAII-style socket wrapper. Since socket is closed on wrapper destruction, initializing multiple guard...
void reset() noexcept
Closes the socket.
SOCKET release() noexcept
Returns the socket descriptor and releases ownership.