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 == WSAEMSGSIZE ;
124 return m_reason == WSAEMFILE ;
127bool GNet::SocketBase::setNonBlocking()
129 unsigned long ul = 1 ;
130 return ioctlsocket( m_fd.fd() , FIONBIO , &ul ) != SOCKET_ERROR ;
133std::string GNet::SocketBase::reasonString(
int e )
135 const char * p = nullptr ;
136 if( e == WSAEINTR ) p =
"interupted" ;
138 if( e == WSAEACCES ) p =
"access denied" ;
140 if( e == WSAEINVAL ) p =
"invalid parameter" ;
155 if( e == WSAEADDRINUSE ) p =
"address already in use" ;
156 if( e == WSAEADDRNOTAVAIL ) p =
"address not available" ;
157 if( e == WSAENETDOWN ) p =
"network down" ;
158 if( e == WSAENETUNREACH ) p =
"network unreachable" ;
159 if( e == WSAENETRESET ) p =
"network reset" ;
164 if( e == WSAENOTCONN ) p =
"cannot connect" ;
167 if( e == WSAETIMEDOUT ) p =
"timed out" ;
168 if( e == WSAECONNREFUSED ) p =
"connection refused" ;
171 if( e == WSAEHOSTDOWN ) p =
"host down" ;
172 if( e == WSAEHOSTUNREACH ) p =
"host unreachable" ;
180 return std::string( p ) ;
193 return std::string() ;
196void GNet::Socket::setOptionReuse()
198 setOption( SOL_SOCKET ,
"so_reuseaddr" , SO_REUSEADDR , 1 ) ;
201void GNet::Socket::setOptionExclusive()
203 setOption( SOL_SOCKET ,
"so_exclusiveaddruse" , SO_EXCLUSIVEADDRUSE , 1 ) ;
206void GNet::Socket::setOptionPureV6()
211bool GNet::Socket::setOptionPureV6( std::nothrow_t )
216bool GNet::Socket::setOptionImp(
int level ,
int op ,
const void * arg , socklen_t n )
218 const char * cp =
static_cast<const char*
>(arg) ;
219 int rc = ::setsockopt( fd() , level , op , cp , n ) ;
220 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.
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.