25#ifndef JOIN_CORE_ACCEPTOR_HPP
26#define JOIN_CORE_ACCEPTOR_HPP
36 template <
class Protocol>
41 using Socket =
typename Protocol::Socket;
42 using Stream =
typename Protocol::Stream;
67 : _handle (other._handle)
68 , _protocol (other._protocol)
71 other._protocol = Protocol ();
83 _handle = other._handle;
84 _protocol = other._protocol;
87 other._protocol = Protocol ();
114 _handle = ::socket (endpoint.protocol ().family (), endpoint.protocol ().type () | flags,
115 endpoint.protocol ().protocol ());
119 lastError = std::error_code (errno, std::generic_category ());
125 if (endpoint.protocol ().family () == AF_INET6)
129 if (::setsockopt (_handle, IPPROTO_IPV6, IPV6_V6ONLY, &off,
sizeof (off)) == -1)
132 lastError = std::error_code (errno, std::generic_category ());
139 if (endpoint.protocol ().family () == AF_UNIX)
141 ::unlink (endpoint.device ().c_str ());
143 else if (endpoint.protocol ().protocol () == IPPROTO_TCP)
147 if (::setsockopt (_handle, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof (on)) == -1)
150 lastError = std::error_code (errno, std::generic_category ());
157 if ((::bind (_handle, endpoint.addr (), endpoint.length ()) == -1) || (::listen (_handle, SOMAXCONN) == -1))
159 lastError = std::error_code (errno, std::generic_category ());
164 _protocol = endpoint.protocol ();
180 _protocol = Protocol ();
190 struct sockaddr_storage sa;
191 socklen_t sa_len =
sizeof (
struct sockaddr_storage);
193 int fd = ::accept4 (_handle,
reinterpret_cast<struct sockaddr*
> (&sa), &sa_len, flags);
196 lastError = std::error_code (errno, std::generic_category ());
200 return Socket (fd,
Endpoint (
reinterpret_cast<struct sockaddr*
> (&sa), sa_len),
201 (flags & SOCK_NONBLOCK) ? Socket::NonBlocking : Socket::Blocking);
211 stream.socket () =
accept ();
221 struct sockaddr_storage sa;
222 socklen_t sa_len =
sizeof (
struct sockaddr_storage);
224 if (::getsockname (_handle,
reinterpret_cast<struct sockaddr*
> (&sa), &sa_len) == -1)
226 lastError = std::error_code (errno, std::generic_category ());
230 return Endpoint (
reinterpret_cast<struct sockaddr*
> (&sa), sa_len);
239 return (_handle != -1);
248 return _protocol.family ();
257 return _protocol.type ();
266 return _protocol.protocol ();
basic stream acceptor class.
Definition protocol.hpp:53
BasicStreamAcceptor()=default
create the acceptor instance.
~BasicStreamAcceptor()
destroy instance.
Definition acceptor.hpp:95
bool opened() const noexcept
check if the socket is opened.
Definition acceptor.hpp:237
BasicStreamAcceptor(const BasicStreamAcceptor &other)=delete
copy constructor.
typename Protocol::Endpoint Endpoint
Definition acceptor.hpp:40
Endpoint localEndpoint() const
determine the local endpoint associated with this socket.
Definition acceptor.hpp:219
void close() noexcept
close acceptor.
Definition acceptor.hpp:172
int family() const noexcept
get address family.
Definition acceptor.hpp:246
typename Protocol::Stream Stream
Definition acceptor.hpp:42
int protocol() const noexcept
get acceptor protocol.
Definition acceptor.hpp:264
int type() const noexcept
get the protocol communication semantic.
Definition acceptor.hpp:255
Stream acceptStream() const
accept new connection and fill in the client object with connection parameters.
Definition acceptor.hpp:208
Socket accept(int flags=SOCK_NONBLOCK|SOCK_CLOEXEC) const
accept new connection and fill in the client object with connection parameters.
Definition acceptor.hpp:188
typename Protocol::Socket Socket
Definition acceptor.hpp:41
int handle() const noexcept
get socket native handle.
Definition acceptor.hpp:273
BasicStreamAcceptor(BasicStreamAcceptor &&other)
move constructor.
Definition acceptor.hpp:66
int create(const Endpoint &endpoint, int flags=SOCK_CLOEXEC) noexcept
create acceptor
Definition acceptor.hpp:106
BasicStreamAcceptor & operator=(const BasicStreamAcceptor &other)=delete
copy assignment operator.
Definition acceptor.hpp:32
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:195