43bool GNet::SocketBase::create(
int domain ,
int type ,
int protocol )
45 m_fd =
Descriptor( ::socket(domain,type,protocol) ) ;
54bool GNet::SocketBase::prepare(
bool )
56 static bool first = true ;
63 if( !setNonBlocking() )
71void GNet::SocketBase::destroy() noexcept
73 if( m_domain == PF_UNIX && !m_accepted ) unlink() ;
74 ::close( m_fd.fd() ) ;
77void GNet::SocketBase::unlink() noexcept
81 AddressStorage address_storage ;
82 int rc = ::getsockname( m_fd.fd() , address_storage.p1() , address_storage.p2() ) ;
83 std::string path = rc == 0 ? Address(address_storage).hostPartString() : std::string() ;
84 if( !path.empty() && path.at(0U) ==
'/' )
86 G_DEBUG(
"GNet::SocketBase::unlink: deleting unix-domain socket: fd=" << m_fd.fd() <<
" path=[" <<
G::Str::printable(path) <<
"]" ) ;
105bool GNet::SocketBase::setNonBlocking()
107 int mode = ::fcntl( m_fd.fd() , F_GETFL ) ;
111 int rc = ::fcntl( m_fd.fd() , F_SETFL , mode | O_NONBLOCK ) ;
122 return m_reason == ENOTCONN ;
127 return m_reason == EWOULDBLOCK || m_reason == EAGAIN || m_reason == EINTR ;
132 return m_reason == EINPROGRESS ;
137 return m_reason == EADDRINUSE ;
143 return m_reason == EMSGSIZE ;
149 return m_reason == EMFILE ;
152std::string GNet::SocketBase::reasonString(
int e )
162 if( address.
family() == Address::Family::ipv4 || address.
family() == Address::Family::ipv6 )
167 return s.
bind( address , std::nothrow ) ? std::string() : s.
reason() ;
173 return s.
bind( address , std::nothrow ) ? std::string() : s.
reason() ;
183void GNet::Socket::setOptionReuse()
186 setOption( SOL_SOCKET ,
"so_reuseaddr" , SO_REUSEADDR , 1 ) ;
189void GNet::Socket::setOptionExclusive()
194void GNet::Socket::setOptionPureV6()
196 #if GCONFIG_HAVE_IPV6
197 setOption( IPPROTO_IPV6 ,
"ipv6_v6only" , IPV6_V6ONLY , 1 ) ;
199 throw SocketError(
"cannot set socket option for pure ipv6" ) ;
203bool GNet::Socket::setOptionPureV6( std::nothrow_t )
205 #if GCONFIG_HAVE_IPV6
206 return setOption( IPPROTO_IPV6 ,
"ipv6_v6only" , IPV6_V6ONLY , 1 , std::nothrow ) ;
210bool GNet::Socket::setOptionImp(
int level ,
int op ,
const void * arg , socklen_t n )
212 int rc = ::setsockopt( fd() , level , op , arg , n ) ;
225 if( length == 0 )
return 0 ;
227 ssize_type nread =
G::Msg::recv( fd() , buffer , length , 0 ) ;
228 if( sizeError(nread) )
231 G_DEBUG(
"GNet::RawSocket::read: cannot read from " << fd() ) ;
239 return writeImp( buffer , length ) ;
248 socklen_t size =
sizeof(int) ;
249 int rc = ::getsockopt( fd() , SOL_SOCKET , SO_SNDBUF , &value , &size ) ;
250 if( rc == 0 && size ==
sizeof(
int) && value >= 0 &&
static_cast<std::size_t
>(value) > default_in )
251 return static_cast<std::size_t
>(value) ;
264 G_DEBUG(
"GNet::DatagramSocket::write: write error " << reason() ) ;
The GNet::Address class encapsulates a TCP/UDP transport address.
static int domain(Family) noexcept
Returns the address 'domain' for the given family, eg.
socklen_t length() const
Returns the size of the sockaddr address. See address().
const sockaddr * address() const
Returns the sockaddr address.
Family family() const noexcept
Returns the address family enumeration.
A derivation of GNet::Socket for a datagram socket.
ssize_type writeto(const char *buffer, size_type len, const Address &dst)
Sends a datagram to the given address.
std::size_t limit(std::size_t default_=1024U) const
Returns the systems's maximum datagram size if the value is known and greater than the given default ...
A class that encapsulates a network socket file descriptor and an associated windows event handle.
static Descriptor invalid() noexcept
Returns a descriptor with an invalid socket part and a zero handle.
RawSocket(int domain, int type, int protocol)
Constructor.
ssize_type write(const char *buf, size_type len) override
Writes to the socket.
ssize_type read(char *buffer, size_type buffer_length) override
Reads from the socket.
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.
std::string reason() const
Returns the reason for the previous error.
SOCKET fd() const noexcept override
Returns the socket file descriptor.
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.
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.
static bool supports(Address::Family, int type, int protocol)
Returns true if sockets can be created with the given parameters.
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 saveReason()
Saves the current errno following error()/sizeError().
static std::string canBindHint(const Address &address, bool stream_socket, const Config &)
Returns the empty string if a socket could probably be bound with the given address or a failure reas...
void bind(const Address &)
Binds the socket with the given address.
A derivation of GNet::Socket for a stream socket.
static void init()
An optional early-initialisation function. May be called more than once.
static bool remove(const Path &path, std::nothrow_t) noexcept
Deletes the file or directory. Returns false on error.
static ssize_t recv(SOCKET, void *, std::size_t, int flags) noexcept
A recv() wrapper.
static ssize_t sendto(SOCKET, const void *, std::size_t, int flags, const sockaddr *, socklen_t) noexcept
A sendto() wrapper.
A Path object represents a file system path.
static std::string strerror(int errno_)
Translates an 'errno' value into a meaningful diagnostic string.
static int errno_(const SignalSafe &=G::SignalSafe()) noexcept
Returns the process's current 'errno' value.
static std::string printable(const std::string &in, char escape='\\')
Returns a printable representation of the given input string, using chacter code ranges 0x20 to 0x7e ...
A configuration structure for GNet::DatagramSocket.
Overload discriminator class for GNet::SocketBase.
A configuration structure for GNet::Socket.
A configuration structure for GNet::StreamSocket.