31 if(
fd == INVALID_SOCKET )
37bool GNet::SocketBase::create(
int domain ,
int type ,
int protocol )
39 m_fd = Descriptor( ::socket( domain , type , protocol ) , 0 ) ;
46 m_fd = Descriptor( m_fd.fd() , WSACreateEvent() ) ;
47 if( m_fd.h() == HNULL )
50 ::closesocket( m_fd.fd() ) ;
56bool GNet::SocketBase::prepare(
bool accepted )
60 G_ASSERT( m_fd.h() == HNULL ) ;
61 HANDLE h = WSACreateEvent() ;
67 m_fd = Descriptor( m_fd.fd() , h ) ;
70 if( !setNonBlocking() )
78void GNet::SocketBase::destroy() noexcept
80 if( m_fd.h() != HNULL )
81 WSACloseEvent( m_fd.h() ) ;
84 ::closesocket( m_fd.fd() ) ;
89 return rc == SOCKET_ERROR ;
94 m_reason = WSAGetLastError() ;
99 return size == SOCKET_ERROR ;
104 return m_reason == WSAENOTCONN ;
109 return m_reason == WSAEWOULDBLOCK ;
114 return m_reason == WSAEWOULDBLOCK ;
119 return m_reason == WSAEADDRINUSE ;
124 return m_reason == WSAEMSGSIZE ;
129 return m_reason == WSAEMFILE ;
132bool GNet::SocketBase::setNonBlocking()
134 unsigned long ul = 1 ;
135 return ioctlsocket( m_fd.fd() , FIONBIO , &ul ) != SOCKET_ERROR ;
138std::string GNet::SocketBase::reasonString(
int e )
140 const char * p = nullptr ;
141 if( e == WSAEINTR ) p =
"interupted" ;
143 if( e == WSAEACCES ) p =
"access denied" ;
145 if( e == WSAEINVAL ) p =
"invalid parameter" ;
160 if( e == WSAEADDRINUSE ) p =
"address already in use" ;
161 if( e == WSAEADDRNOTAVAIL ) p =
"address not available" ;
162 if( e == WSAENETDOWN ) p =
"network down" ;
163 if( e == WSAENETUNREACH ) p =
"network unreachable" ;
164 if( e == WSAENETRESET ) p =
"network reset" ;
169 if( e == WSAENOTCONN ) p =
"cannot connect" ;
172 if( e == WSAETIMEDOUT ) p =
"timed out" ;
173 if( e == WSAECONNREFUSED ) p =
"connection refused" ;
176 if( e == WSAEHOSTDOWN ) p =
"host down" ;
177 if( e == WSAEHOSTUNREACH ) p =
"host unreachable" ;
185 return std::string( p ) ;
198 return std::string() ;
201void GNet::Socket::setOptionReuse()
203 setOption( SOL_SOCKET ,
"so_reuseaddr" , SO_REUSEADDR , 1 ) ;
206void GNet::Socket::setOptionExclusive()
208 setOption( SOL_SOCKET ,
"so_exclusiveaddruse" , SO_EXCLUSIVEADDRUSE , 1 ) ;
211void GNet::Socket::setOptionPureV6()
216bool GNet::Socket::setOptionPureV6( std::nothrow_t )
221bool GNet::Socket::setOptionImp(
int level ,
int op ,
const void * arg , socklen_t n )
223 const char * cp =
static_cast<const char*
>(arg) ;
224 int rc = ::setsockopt( fd() , level , op , cp , n ) ;
225 bool ok = !error( rc ) ;
static int domain(Family) noexcept
Returns the address 'domain' for the given family, eg.
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 ...
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.
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...
static std::string strerror(int errno_)
Translates an 'errno' value into a meaningful diagnostic string.
static bool imatch(char, char) noexcept
Returns true if the two characters are the same, ignoring seven-bit case.
static std::string fromInt(int i)
Converts int 'i' to a string.