30 {
31 auto fd =
socket_guard { socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) };
32 if(!fd)
33 throw std::runtime_error("cannot create socket");
34
35#if defined(__unix__) || defined(__APPLE__)
36 auto fdFlags = fcntl(*fd, F_GETFD, 0);
37 if(fdFlags != -1)
38 fcntl(*fd, F_SETFD, fdFlags | FD_CLOEXEC);
39#endif
40
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));
45
46 if(connect(*fd,
reinterpret_cast<const sockaddr*
>(&addrin),
sizeof(addrin)) ==
SOCKET_ERROR)
47 {
49 return;
50 }
51
52 mChannel = std::make_unique<BufferedIPCChannel>();
53 mChannel->StartConversation(fd.release(), callback);
54 }
virtual void OnConnectionError() noexcept=0
Called when connection attempt fails.
std::unique_ptr< BufferedIPCChannel > mChannel
RAII-style socket wrapper. Since socket is closed on wrapper destruction, initializing multiple guard...