void UnixSocketAcceptor<Protocol, Dispatcher>::open(UnixProtocol<Protocol> const &protocol, Error &err) { if (!err) { _fd = ::socket(protocol.domain(), protocol.type(), protocol.protocol()); if (_fd == -1) err.assign(errno); } }
void UnixSocketAcceptor<Protocol, Dispatcher>::bind(UnixEndpoint<Protocol> const &ep, Error &err) { if (!err) { int ret = ::bind(_fd, ep.addr(), ep.addrlen()); if (ret == -1) err.assign(errno); } }
void UnixSocketAcceptor<Protocol, Dispatcher>::listen(int max, Error &err) { if (!err) { int ret = ::listen(_fd, max); if (ret == -1) err.assign(errno); } }
void UnixSocketAcceptor<Protocol, Dispatcher>::set_option(UnixSocketBase::ReuseAddress const &opt, Error &err) { if (!err) { int ret; int b = opt; ret = setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&b, sizeof(b)); if (ret == -1) err.assign(errno); } }
void UnixSocketAcceptor<Protocol, Dispatcher>::close(Error &err) { if (!err) { int ret; cancel(err); ret = ::close(_fd); if (ret) err.assign(errno); _fd = -1; } }
void UnixSocketAcceptor<Protocol, Dispatcher>::accept(UnixSocket<Protocol, Dispatcher> &sock, UnixEndpoint<Protocol> &ep, Error &err) { if (!err) { int fd; socklen_t len = ep.addrlen(); fd = ::accept(_fd, ep.addr(), &len); //std::cout << "accepting " << _fd << " " << fd << " " << errno << std::endl; if (fd != -1) sock.assign(fd, err); else err.assign(errno); } }
void UnixSocketAcceptor<Protocol, Dispatcher>::set_option(UnixSocketBase::NonBlocking const &opt, Error &err) { if (!err) { int ret; #if defined(WIN32) unsigned long mode = (opt == true) ? 1 : 0; ret = ioctlsocket(_fd, FIONBIO, &mode); #else ret = fcntl(_fd, F_GETFL, 0); #endif if (ret == -1) { err.assign(errno); return ; } #if !defined(WIN32) ret = fcntl(_fd, F_SETFL, (opt == true) ? ret | O_NONBLOCK : ret & ~O_NONBLOCK); if (ret == -1) err.assign(errno); #endif } }
void UnixSocketAcceptor<Protocol, Dispatcher>::get_option(UnixSocketBase::NonBlocking &opt, Error &err) { #if !defined(WIN32) if (!err) { int ret; int b; ret = fcntl(_fd, F_GETFL, 0); if (ret == -1) err.assign(errno); else opt = (ret & O_NONBLOCK) ? true : false; } #endif }
UnixEndpoint<Protocol> UnixSocketAcceptor<Protocol, Dispatcher>::remote_endpoint(Error &err) { UnixEndpoint<Protocol> ep; if (!err) { int ret; socklen_t len = ep.addrlen(); ret = getpeername(_fd, ep.addr(), &len); if (ret == -1) err.assign(errno); } return ep; }
void UnixSocketAcceptor<Protocol, Dispatcher>::get_option(UnixSocketBase::ReuseAddress &opt, Error &err) { if (!err) { int ret; int b; #if defined(WIN32) ret = getsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&b, sizeof(b)); #else ret = getsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &b, sizeof(b)); #endif if (ret == -1) err.assign(errno); opt = b; } }