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( SocketTooMany ,
tx(
"socket accept error") )
58 G_EXCEPTION_CLASS( SocketBindErrorBase ,
tx(
"socket bind error") )
64 std::string m_reason ;
65 bool m_einuse {
false} ;
67 using size_type = G::ReadWrite::size_type ;
68 using ssize_type = G::ReadWrite::ssize_type ;
74 static bool supports( Address::Family ,
int type ,
int protocol ) ;
82 SOCKET
fd() const noexcept override ;
167 SocketBase( const
Raw & ,
int domain ,
int type ,
int protocol ) ;
170 ssize_type writeImp( const
char * buf , size_type len ) ;
175 static
bool error(
int rc ) ;
179 static
bool sizeError( ssize_type size ) ;
196 static
std::
string reasonString(
int ) ;
197 bool create(
int ,
int ,
int ) ;
198 bool prepare(
bool ) ;
199 void drop() noexcept ;
200 void destroy() noexcept ;
201 void unlink() noexcept ;
202 bool setNonBlocking() ;
234 int listen_queue {0} ;
235 bool connect_pureipv6 {
true} ;
236 bool bind_pureipv6 {
true} ;
237 bool bind_reuse {
true} ;
238 bool bind_exclusive {
false} ;
239 bool free_bind {
false} ;
240 Config & set_listen_queue(
int ) noexcept ;
241 Config & set_bind_reuse(
bool ) noexcept ;
242 Config & set_bind_exclusive(
bool ) noexcept ;
243 Config & set_free_bind(
bool ) noexcept ;
244 template <
typename T>
const T & set_last() ;
247 Address getLocalAddress()
const ;
250 std::pair<bool,Address> getPeerAddress()
const ;
258 bool bind(
const Address & , std::nothrow_t ) ;
261 static std::string canBindHint(
const Address & address ,
bool stream_socket ,
const Config & ) ;
267 unsigned long getBoundScopeId()
const ;
273 bool connect(
const Address & addr ,
bool *done =
nullptr ) ;
293 void shutdown(
int how = 1 ) ;
314 Socket( Address::Family ,
int type ,
int protocol ,
const Config & ) ;
320 void setOptionLinger(
int onoff ,
int time ) ;
321 void setOptionKeepAlive() ;
322 void setOptionFreeBind() ;
325 void setOption(
int ,
const char * ,
int ,
int ) ;
326 bool setOption(
int ,
const char * ,
int ,
int , std::nothrow_t ) ;
327 bool setOptionImp(
int ,
int ,
const void * , socklen_t ) ;
328 void setOptionsOnBind( Address::Family ) ;
329 void setOptionsOnConnect( Address::Family ) ;
330 void setOptionReuse() ;
331 void setOptionExclusive() ;
332 void setOptionPureV6() ;
333 bool setOptionPureV6( std::nothrow_t ) ;
337 unsigned long m_bound_scope_id {0UL} ;
347 std::unique_ptr<StreamSocket> socket_ptr ;
358 using size_type = Socket::size_type ;
359 using ssize_type = Socket::ssize_type ;
366 int create_linger_onoff {0} ;
367 int create_linger_time {0} ;
368 int accept_linger_onoff {0} ;
369 int accept_linger_time {0} ;
370 bool create_keepalive {
false} ;
371 bool accept_keepalive {
false} ;
372 Config & set_create_linger( std::pair<int,int> ) noexcept ;
373 Config & set_create_linger_onoff(
int ) noexcept ;
374 Config & set_create_linger_time(
int ) noexcept ;
375 Config & set_accept_linger( std::pair<int,int> ) noexcept ;
376 Config & set_accept_linger_onoff(
int ) noexcept ;
377 Config & set_accept_linger_time(
int ) noexcept ;
380 static bool supports( Address::Family ) ;
399 ssize_type
read(
char * buffer , size_type buffer_length )
override ;
402 ssize_type
write(
const char * buf , size_type len )
override ;
418 void setOptionsOnCreate( Address::Family ,
bool listener ) ;
419 void setOptionsOnAccept( Address::Family ) ;
441 ssize_type
read(
char * buffer , size_type len )
override ;
444 ssize_type
write(
const char * buffer , size_type len )
override ;
447 ssize_type readfrom(
char * buffer , size_type len ,
Address & src ) ;
452 ssize_type writeto(
const char * buffer , size_type len ,
const Address & dst ) ;
456 ssize_type writeto(
const std::vector<std::string_view> & ,
const Address & dst ) ;
464 std::size_t limit( std::size_t default_ = 1024U )
const ;
487 RawSocket(
int domain ,
int type ,
int protocol ) ;
490 ssize_type
read(
char * buffer , size_type buffer_length )
override ;
493 ssize_type
write(
const char * buf , size_type len )
override ;
504inline GNet::Socket::SocketBindError::SocketBindError(
const std::string & s ) : SocketBindErrorBase(s) , m_reason(s) {}
505inline GNet::Socket::SocketBindError::SocketBindError(
const Address & a ,
const std::string & s ,
bool b ) : SocketBindErrorBase(s) , m_address(a) , m_reason(s) , m_einuse(b) {}
507inline GNet::Socket::Config & GNet::Socket::Config::set_listen_queue(
int n )
noexcept { listen_queue = n ;
return *this ; }
508inline GNet::Socket::Config & GNet::Socket::Config::set_bind_reuse(
bool b )
noexcept { bind_reuse = b ;
return *this ; }
509inline GNet::Socket::Config & GNet::Socket::Config::set_bind_exclusive(
bool b )
noexcept { bind_exclusive = b ;
return *this ; }
510inline GNet::Socket::Config & GNet::Socket::Config::set_free_bind(
bool b )
noexcept { free_bind = b ;
return *this ; }
511template <
typename T>
const T & GNet::Socket::Config::set_last() {
return static_cast<const T&
>(*this) ; }
513inline 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 ; }
514inline GNet::StreamSocket::Config & GNet::StreamSocket::Config::set_create_linger_onoff(
int n )
noexcept { create_linger_onoff = n ;
return *this ; }
515inline GNet::StreamSocket::Config & GNet::StreamSocket::Config::set_create_linger_time(
int n )
noexcept { create_linger_time = n ;
return *this ; }
516inline 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 ; }
517inline GNet::StreamSocket::Config & GNet::StreamSocket::Config::set_accept_linger_onoff(
int n )
noexcept { accept_linger_onoff = n ;
return *this ; }
518inline 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 lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
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.
void addOtherHandler(EventHandler &, EventState)
Adds this socket to the event source list so that the given handler receives exception events.
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 addWriteHandler(EventHandler &, EventState)
Adds this socket to the event source list so that the given handler receives write events when flow c...
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.
void addReadHandler(EventHandler &, EventState)
Adds this socket to the event source list so that the given handler receives read events.
bool isFamily(Address::Family) const
Returns true if the socket family is as given.
static bool sizeError(ssize_type size)
Returns true if the given write() return value indicates an error.
bool eInUse() const
Returns true if the previous socket bind operation failed because the socket was already in use.
Descriptor fdd() const noexcept
Returns the socket descriptor.
static bool supports(Address::Family, int type, int protocol)
Returns true if sockets can be created with the given parameters.
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) noexcept
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.
Exception class for GNet::SocketBase bind failures.
Overload discriminator class for GNet::Socket.
A configuration structure for GNet::Socket.
A configuration structure for GNet::StreamSocket.
Overload discriminator class for GNet::StreamSocket.