41 class SocketProtocol ;
43 class DatagramSocket ;
55 G_EXCEPTION( SocketError ,
tx(
"socket error") ) ;
56 G_EXCEPTION_CLASS( SocketCreateError ,
tx(
"socket create error") ) ;
57 G_EXCEPTION_CLASS( SocketBindError ,
tx(
"socket bind error") ) ;
58 G_EXCEPTION_CLASS( SocketTooMany ,
tx(
"socket accept error") ) ;
59 using size_type = G::ReadWrite::size_type ;
60 using ssize_type = G::ReadWrite::ssize_type ;
66 static bool supports( Address::Family ,
int type ,
int protocol ) ;
74 SOCKET
fd() const noexcept override ;
152 SocketBase( const
Raw & ,
int domain ,
int type ,
int protocol ) ;
155 ssize_type writeImp( const
char * buf , size_type len ) ;
160 static
bool error(
int rc ) ;
164 static
bool sizeError( ssize_type size ) ;
181 static
std::
string reasonString(
int ) ;
182 bool create(
int ,
int ,
int ) ;
183 bool prepare(
bool ) ;
184 void drop() noexcept ;
185 void destroy() noexcept ;
186 void unlink() noexcept ;
187 bool setNonBlocking() ;
219 int listen_queue {0} ;
220 bool connect_pureipv6 {
true} ;
221 bool bind_pureipv6 {
true} ;
222 bool bind_reuse {
true} ;
223 bool bind_exclusive {
false} ;
224 bool free_bind {
false} ;
225 Config & set_listen_queue(
int ) noexcept ;
226 Config & set_bind_reuse(
bool ) noexcept ;
227 Config & set_bind_exclusive(
bool ) noexcept ;
228 Config & set_free_bind(
bool ) noexcept ;
229 template <
typename T>
const T & set_last() ;
232 Address getLocalAddress()
const ;
235 std::pair<bool,Address> getPeerAddress()
const ;
243 bool bind(
const Address & , std::nothrow_t ) ;
246 static std::string canBindHint(
const Address & address ,
bool stream_socket ,
const Config & ) ;
252 unsigned long getBoundScopeId()
const ;
258 bool connect(
const Address & addr ,
bool *done =
nullptr ) ;
278 void shutdown(
int how = 1 ) ;
299 Socket( Address::Family ,
int type ,
int protocol ,
const Config & ) ;
305 void setOptionLinger(
int onoff ,
int time ) ;
306 void setOptionKeepAlive() ;
307 void setOptionFreeBind() ;
310 void setOption(
int ,
const char * ,
int ,
int ) ;
311 bool setOption(
int ,
const char * ,
int ,
int , std::nothrow_t ) ;
312 bool setOptionImp(
int ,
int ,
const void * , socklen_t ) ;
313 void setOptionsOnBind( Address::Family ) ;
314 void setOptionsOnConnect( Address::Family ) ;
315 void setOptionReuse() ;
316 void setOptionExclusive() ;
317 void setOptionPureV6() ;
318 bool setOptionPureV6( std::nothrow_t ) ;
322 unsigned long m_bound_scope_id {0UL} ;
332 std::unique_ptr<StreamSocket> socket_ptr ;
343 using size_type = Socket::size_type ;
344 using ssize_type = Socket::ssize_type ;
351 int create_linger_onoff {0} ;
352 int create_linger_time {0} ;
353 int accept_linger_onoff {0} ;
354 int accept_linger_time {0} ;
355 bool create_keepalive {
false} ;
356 bool accept_keepalive {
false} ;
357 Config & set_create_linger( std::pair<int,int> ) noexcept ;
358 Config & set_create_linger_onoff(
int ) noexcept ;
359 Config & set_create_linger_time(
int ) noexcept ;
360 Config & set_accept_linger( std::pair<int,int> ) noexcept ;
361 Config & set_accept_linger_onoff(
int ) noexcept ;
362 Config & set_accept_linger_time(
int ) noexcept ;
365 static bool supports( Address::Family ) ;
384 ssize_type
read(
char * buffer , size_type buffer_length )
override ;
387 ssize_type
write(
const char * buf , size_type len )
override ;
403 void setOptionsOnCreate( Address::Family ,
bool listener ) ;
404 void setOptionsOnAccept( Address::Family ) ;
426 ssize_type
read(
char * buffer , size_type len )
override ;
429 ssize_type
write(
const char * buffer , size_type len )
override ;
432 ssize_type readfrom(
char * buffer , size_type len ,
Address & src ) ;
437 ssize_type writeto(
const char * buffer , size_type len ,
const Address & dst ) ;
441 ssize_type writeto(
const std::vector<G::string_view> & ,
const Address & dst ) ;
449 std::size_t limit( std::size_t default_ = 1024U )
const ;
472 RawSocket(
int domain ,
int type ,
int protocol ) ;
475 ssize_type
read(
char * buffer , size_type buffer_length )
override ;
478 ssize_type
write(
const char * buf , size_type len )
override ;
489inline GNet::Socket::Config & GNet::Socket::Config::set_listen_queue(
int n )
noexcept { listen_queue = n ;
return *this ; }
490inline GNet::Socket::Config & GNet::Socket::Config::set_bind_reuse(
bool b )
noexcept { bind_reuse = b ;
return *this ; }
491inline GNet::Socket::Config & GNet::Socket::Config::set_bind_exclusive(
bool b )
noexcept { bind_exclusive = b ;
return *this ; }
492inline GNet::Socket::Config & GNet::Socket::Config::set_free_bind(
bool b )
noexcept { free_bind = b ;
return *this ; }
493template <
typename T>
const T & GNet::Socket::Config::set_last() {
return static_cast<const T&
>(*this) ; }
495inline GNet::StreamSocket::Config & GNet::StreamSocket::Config::set_create_linger( std::pair<int,int> p )
noexcept { create_linger_onoff = p.first ; create_linger_time = p.second ;
return *this ; }
496inline GNet::StreamSocket::Config & GNet::StreamSocket::Config::set_create_linger_onoff(
int n )
noexcept { create_linger_onoff = n ;
return *this ; }
497inline GNet::StreamSocket::Config & GNet::StreamSocket::Config::set_create_linger_time(
int n )
noexcept { create_linger_time = n ;
return *this ; }
498inline GNet::StreamSocket::Config & GNet::StreamSocket::Config::set_accept_linger( std::pair<int,int> p )
noexcept { accept_linger_onoff = p.first ; accept_linger_time = p.second ;
return *this ; }
499inline GNet::StreamSocket::Config & GNet::StreamSocket::Config::set_accept_linger_onoff(
int n )
noexcept { accept_linger_onoff = n ;
return *this ; }
500inline GNet::StreamSocket::Config & GNet::StreamSocket::Config::set_accept_linger_time(
int n )
noexcept { accept_linger_time = n ;
return *this ; }
A move-only class which is used to return a new()ed socket to calling code, together with associated ...
The GNet::Address class encapsulates a TCP/UDP transport address.
static Address defaultAddress()
Returns a default address, being the IPv4 wildcard address with a zero port number.
A derivation of GNet::Socket for a datagram socket.
A class that encapsulates a network socket file descriptor and an associated windows event handle.
A base class for classes that have a file descriptor and handle asynchronous events from the event lo...
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
A derivation of GNet::SocketBase for a raw socket, typically of type AF_NETLINK or PF_ROUTE.
A socket base class that holds a non-blocking socket file descriptor and interfaces to the event loop...
bool eNotConn() const
Returns true if the previous socket operation failed with the ENOTCONN error status,...
bool eInProgress() const
Returns true if the previous socket operation failed with the EINPROGRESS error status.
~SocketBase() override
Destructor.
std::string reason() const
Returns the reason for the previous error.
SOCKET fd() const noexcept override
Returns the socket file descriptor.
void clearReason()
Clears the saved errno.
std::string asString() const
Returns the socket handle as a string.
void dropReadHandler() noexcept
Reverses addReadHandler().
void addOtherHandler(EventHandler &, ExceptionSink)
Adds this socket to the event source list so that the given handler receives exception events.
static bool error(int rc)
Returns true if the given return code indicates an error.
bool eMsgSize() const
Returns true if the previous socket operation failed with the EMSGSIZE error status.
bool isFamily(Address::Family) const
Returns true if the socket family is as given.
void addReadHandler(EventHandler &, ExceptionSink)
Adds this socket to the event source list so that the given handler receives read events.
static bool sizeError(ssize_type size)
Returns true if the given write() return value indicates an error.
static bool supports(Address::Family, int type, int protocol)
Returns true if sockets can be created with the given parameters.
void addWriteHandler(EventHandler &, ExceptionSink)
Adds this socket to the event source list so that the given handler receives write events when flow c...
void dropOtherHandler() noexcept
Reverses addOtherHandler().
bool eWouldBlock() const override
Returns true if the previous socket operation failed because the socket would have blocked.
bool eTooMany() const
Returns true if the previous socket operation failed with the EMFILE error status,...
void dropWriteHandler() noexcept
Reverses addWriteHandler().
void saveReason()
Saves the current errno following error()/sizeError().
An internet-protocol socket class.
A derivation of GNet::Socket for a stream socket.
An abstract interface for reading and writing from a non-blocking i/o channel.
virtual ssize_type write(const char *buf, size_type len)=0
Sends data.
virtual ssize_type read(char *buffer, size_type buffer_length)=0
Reads data.
constexpr const char * tx(const char *p)
A briefer alternative to G::gettext_noop().
A configuration structure for GNet::DatagramSocket.
Overload discriminator class for GNet::SocketBase.
Overload discriminator class for GNet::SocketBase.
Overload discriminator class for GNet::Socket.
A configuration structure for GNet::Socket.
A configuration structure for GNet::StreamSocket.
Overload discriminator class for GNet::StreamSocket.