20 #include <sys/socket.h>
28 socketPath(_socketPath)
34 openedAsReader = !sender;
35 struct sockaddr_un addr;
36 if ((reader_fd = ::socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
41 memset(&addr, 0,
sizeof(addr));
42 addr.sun_family = AF_UNIX;
44 if (socketPath.empty()) {
45 *addr.sun_path =
'\0';
46 strncpy(addr.sun_path + 1, socketPath.c_str() + 1,
sizeof(addr.sun_path) - 2);
48 strncpy(addr.sun_path, socketPath.c_str(),
sizeof(addr.sun_path) - 1);
50 ::unlink(socketPath.c_str());
57 while (attempts < maxAttempts) {
58 int result = ::connect(reader_fd,
reinterpret_cast<struct sockaddr*
>(&addr),
sizeof(addr));
66 if (attempts >= maxAttempts) {
71 if (::bind(reader_fd,
reinterpret_cast<struct sockaddr*
>(&addr),
sizeof(addr)) == -1) {
77 if (::listen(reader_fd, 2) == -1) {
81 struct sockaddr_un remote;
82 uint lenRemote =
sizeof(remote);
84 if ((sender_fd = ::accept(reader_fd,
reinterpret_cast<struct sockaddr*
>(&remote), &lenRemote)) == -1) {
115 return remoteAddress;
120 localAddress = _localAddress;
125 remoteAddress = _remoteAddress;
140 ::shutdown(sender_fd, SHUT_RDWR);
146 ::shutdown(reader_fd, SHUT_RDWR);
151 ::unlink(socketPath.c_str());
157 if (closed || !happy) {
161 result =
::read(openedAsReader ? sender_fd : reader_fd, b.
get(), b.
length());
162 if (closed || result == 0) {
179 int writtenMem =
::write(openedAsReader ? sender_fd : reader_fd, b.
get(), b.
length());
180 if (writtenMem < 0) {
182 if (errno != ETIMEDOUT) {