34 if( !create(domain,type,protocol ) )
35 throw SocketError(
"cannot create socket" , m_reason_string ) ;
40 throw SocketError(
"cannot prepare socket" , m_reason_string ) ;
51 throw SocketError(
"cannot prepare socket" , m_reason_string ) ;
67 void GNet::Socket::drop()
74 void GNet::Socket::saveReason()
const
81 G_DEBUG(
"Socket::bind: binding " << local_address.
displayString() <<
" on fd " << m_socket ) ;
83 if( local_address.
domain() != m_domain )
84 throw SocketBindError(
"address family does not match the socket domain" ) ;
86 setOptionsOnBind( local_address.
family() == Address::Family::ipv6() ) ;
88 int rc = ::bind( m_socket.fd() , local_address.
address() , local_address.
length() ) ;
92 throw SocketBindError( local_address.
displayString() , m_reason_string ) ;
98 G_DEBUG(
"Socket::bind: binding " << local_address.
displayString() <<
" on fd " << m_socket ) ;
99 if( local_address.
domain() != m_domain )
return false ;
101 setOptionsOnBind( local_address.
family() == Address::Family::ipv6() ) ;
103 int rc = ::bind( m_socket.fd() , local_address.
address() , local_address.
length() ) ;
114 G_DEBUG(
"GNet::Socket::connect: connecting to " << address.
displayString() ) ;
115 if( address.
domain() != m_domain )
117 G_WARNING(
"GNet::Socket::connect: cannot connect: address family does not match "
118 "the socket domain (" << address.
domain() <<
"," << m_domain <<
")" ) ;
122 setOptionsOnConnect( address.
family() == Address::Family::ipv6() ) ;
124 int rc = ::connect( m_socket.fd() , address.
address() , address.
length() ) ;
134 G_DEBUG(
"GNet::Socket::connect: connection in progress" ) ;
135 if( done !=
nullptr ) *done = false ;
139 G_DEBUG(
"GNet::Socket::connect: synchronous connect failure: " << m_reason ) ;
143 if( done !=
nullptr ) *done = true ;
149 if( static_cast<ssize_type>(len) < 0 )
150 G_WARNING(
"GNet::Socket::write: too big" ) ;
152 ssize_type nsent =
G::Msg::send( m_socket.fd() , buf , len , MSG_NOSIGNAL ) ;
153 if( sizeError(nsent) )
156 G_DEBUG(
"GNet::Socket::write: write error " << m_reason ) ;
159 else if( nsent < 0 || static_cast<size_type>(nsent) < len )
168 int rc = ::listen( m_socket.fd() , backlog ) ;
172 throw SocketError(
"cannot listen on socket" , m_reason_string ) ;
176 std::pair<bool,GNet::Address> GNet::Socket::getAddress(
bool local )
const
182 ::getsockname( m_socket.fd() , address_storage.
p1() , address_storage.
p2() ) :
183 ::getpeername( m_socket.fd() , address_storage.
p1() , address_storage.
p2() ) ;
191 return std::pair<bool,Address>( true , Address(address_storage) ) ;
196 return getAddress(
true ) ;
201 return getAddress(
false ) ;
206 return getPeerAddress().first ;
211 G_DEBUG(
"GNet::Socket::addReadHandler: fd " << m_socket ) ;
217 G_DEBUG(
"GNet::Socket::addWriteHandler: fd " << m_socket ) ;
223 G_DEBUG(
"GNet::Socket::addOtherHandler: fd " << m_socket ) ;
244 std::ostringstream ss ;
252 ::shutdown( m_socket.fd() , how ) ;
257 return m_socket.fd() ;
262 if( m_reason == 0 )
return std::string() ;
263 return reasonString( m_reason ) ;
269 Socket(address_domain,SOCK_STREAM,0)
271 setOptionsOnCreate(
false ) ;
275 Socket(address_domain,SOCK_STREAM,0)
277 setOptionsOnCreate(
true ) ;
283 setOptionsOnAccept() ;
296 ssize_type nread =
G::Msg::recv( m_socket.fd() , buf , len , 0 ) ;
297 if( sizeError(nread) )
300 G_DEBUG(
"GNet::StreamSocket::read: cannot read from " << m_socket.fd() ) ;
314 Descriptor new_fd( ::accept(m_socket.fd(),addr.
p1(),addr.
p2()) ) ;
315 if( ! new_fd.
valid() )
318 throw SocketError(
"cannot accept on socket" , m_reason_string ) ;
322 throw SocketError(
"testing" ) ;
325 pair.second =
Address( addr.
p() , addr.
n() ) ;
328 G_DEBUG(
"GNet::StreamSocket::accept: accepted from " << m_socket.fd()
329 <<
" to " << new_fd <<
" (" << pair.second.
displayString() <<
")" ) ;
337 Socket( address_domain , SOCK_DGRAM , 0 )
347 int rc = ::connect( m_socket.fd() , 0 , 0 ) ;
354 if( len == 0 )
return 0 ;
356 socklen_t sender_len =
sizeof(sender) ;
357 ssize_type nread =
G::Msg::recvfrom( m_socket.fd() , buf , len , 0 , &sender , &sender_len ) ;
358 if( sizeError(nread) )
368 if( len == 0 )
return 0 ;
370 socklen_t sender_len =
sizeof(sender) ;
371 ssize_type nread =
G::Msg::recvfrom( m_socket.fd() , buf , len , 0 , &sender , &sender_len ) ;
372 if( sizeError(nread) )
377 src_address =
Address( &sender , sender_len ) ;
387 G_DEBUG(
"GNet::DatagramSocket::write: write error " << m_reason ) ;
398 void GNet::StreamSocket::setOptionsOnCreate(
bool )
401 setOptionLingerImp( 1 , 0 ) ;
405 setOptionNoLinger() ;
408 setOptionKeepAlive() ;
411 void GNet::StreamSocket::setOptionsOnAccept()
414 setOptionLingerImp( 1 , 0 ) ;
418 setOptionNoLinger() ;
421 setOptionKeepAlive() ;
424 void GNet::Socket::setOptionsOnConnect(
bool ipv6 )
426 setOptionPureV6( ipv6 , NoThrow() ) ;
429 void GNet::Socket::setOptionsOnBind(
bool ipv6 )
437 setOptionPureV6( ipv6 ) ;
440 void GNet::Socket::setOptionKeepAlive()
442 setOption( SOL_SOCKET ,
"so_keepalive" , SO_KEEPALIVE , 1 ) ;
445 void GNet::Socket::setOptionNoLinger()
447 setOptionLingerImp( 0 , 0 ) ;
450 void GNet::Socket::setOptionLingerImp(
int onoff ,
int time )
452 struct linger options ;
453 options.l_onoff = onoff ;
454 options.l_linger = time ;
455 bool ok = setOptionImp( SOL_SOCKET , SO_LINGER , reinterpret_cast<char*>(&options) ,
sizeof(options) ) ;
459 throw SocketError(
"cannot set no_linger" , m_reason_string ) ;
463 bool GNet::Socket::setOption(
int level ,
const char * ,
int op ,
int arg , NoThrow )
465 const void *
const vp =
reinterpret_cast<const void*
>(&arg) ;
466 bool ok = setOptionImp( level , op , vp ,
sizeof(
int) ) ;
472 void GNet::Socket::setOption(
int level ,
const char * opp ,
int op ,
int arg )
474 if( !setOption( level , opp , op , arg , NoThrow() ) )
475 throw SocketError( opp , m_reason_string ) ;
void addReadHandler(EventHandler &, ExceptionHandler &)
Adds this socket to the event source list so that the given handler receives read events...
Family family() const
Returns the address family.
void shutdown(int how=1)
Modifies the local socket state so that so that new sends (1 or 2) and/or receives (0 or 2) will fail...
std::string reason() const
Returns the reason for the previous error.
virtual ssize_type read(char *buffer, size_type buffer_length) override
Override from Socket::read().
std::pair< bool, Address > getLocalAddress() const
Retrieves local address of the socket.
void listen(int backlog=1)
Starts the socket listening on the bound address for incoming connections or incoming datagrams...
bool connect(const Address &addr, bool *done=nullptr)
Initiates a connection to (or association with) the given address.
void dropReadHandler()
Reverses addReadHandler().
virtual ~DatagramSocket()
Destructor.
A helper class for calling accept(), getsockname() and getpeername() and hiding the definition of soc...
void dropOtherHandler()
Reverses addOtherHandler().
The GNet::Address class encapsulates a TCP/UDP transport address.
virtual ssize_type read(char *buffer, size_type len) override
Override from Socket::read().
static ssize_t recv(SOCKET, void *, size_t, int)
A recv() wrapper.
A network socket base class, holding a non-blocking socket file descriptor.
socklen_t * p2()
Returns the length pointer for accept()/getsockname()/getpeername() to write into.
static ssize_t recvfrom(SOCKET, void *, size_t, int, sockaddr *, socklen_t *, int *fd_received_p=nullptr)
A recvfrom() replacement using recvmsg().
sockaddr * p1()
Returns the sockaddr pointer for accept()/getsockname()/getpeername() to write into.
virtual void dropRead(Descriptor fd)=0
Removes the given event source descriptor from the list of read sources.
StreamSocket(int address_domain)
Constructor with a hint of the bind()/connect() address to be used later.
static ssize_t send(SOCKET, const void *, size_t, int, int fd_to_send=-1)
A send() replacement using sendmsg().
virtual ~StreamSocket()
Destructor.
A class that encapsulates a network socket file descriptor and an associated windows event handle...
const sockaddr * address() const
Returns the sockaddr address.
ssize_type writeto(const char *buffer, size_type len, const Address &dst)
Sends a datagram to the given address.
DatagramSocket(int address_domain)
Constructor with a hint of a local address.
static ssize_t sendto(SOCKET, const void *, size_t, int, const sockaddr *, socklen_t, int fd_to_send=-1)
A sendto() replacement using sendmsg().
void dropWriteHandler()
Reverses addWriteHandler().
void bind(const Address &)
Binds the socket with the given address.
virtual void addRead(Descriptor fd, EventHandler &, ExceptionHandler &)=0
Adds the given event source descriptor and associated handler to the read list.
Overload discriminator class for GNet::Socket.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
const sockaddr * p() const
Returns the pointer.
void disconnect()
Releases the association between two datagram endpoints reversing the effect of the previous Socket::...
virtual void addWrite(Descriptor fd, EventHandler &, ExceptionHandler &)=0
Adds the given event source descriptor and associated handler to the write list.
std::pair< bool, Address > getPeerAddress() const
Retrieves address of socket's peer.
A derivation of GNet::Socket for a stream socket.
virtual SOCKET fd() const override
Returns the socket descriptor. Override from G::ReadWrite.
static Address defaultAddress()
Returns a default address, being the IPv4 wildcard address with a zero port number.
static bool enabled()
Returns true if test features are enabled.
Overload discriminator class for GNet::StreamSocket.
socklen_t length() const
Returns the size of the sockaddr address. See address().
virtual ssize_type write(const char *buffer, size_type len) override
Override from Socket::write().
bool valid() const
Returns true if the socket part is valid, ignoring the handle.
virtual ~Socket()
Destructor.
Socket(int domain, int type, int protocol)
Constructor used by derived classes.
void addOtherHandler(EventHandler &, ExceptionHandler &)
Adds this socket to the event source list so that the given handler receives exception events...
A class which is used to return a new()ed socket to calling code, together with associated address in...
int domain() const
Returns the address 'domain', eg. PF_INET.
A base class for classes that handle asynchronous events from the event loop.
virtual ssize_type write(const char *buf, size_type len) override=0
Writes to the socket.
bool hasPeer() const
Returns true if the socket has a valid peer.
void addWriteHandler(EventHandler &, ExceptionHandler &)
Adds this socket to the event source list so that the given handler receives write events when flow c...
virtual ssize_type write(const char *buf, size_type len) override
Override from Socket::write().
AcceptPair accept()
Accepts an incoming connection, returning a new()ed socket and the peer address.
Overload discriminator class for GNet::Socket.
socklen_t n() const
Returns the length.
virtual void dropOther(Descriptor fd)=0
Removes the given event source descriptor from the list of exception sources.
virtual void dropWrite(Descriptor fd)=0
Removes the given event source descriptor from the list of write sources.
std::string displayString() const
Returns a string which represents the transport address for debugging and diagnostics purposes...
virtual void addOther(Descriptor fd, EventHandler &, ExceptionHandler &)=0
Adds the given event source descriptor and associated handler to the exception list.
static EventLoop & instance()
Returns a reference to an instance of the class, if any.
ssize_type readfrom(char *buffer, size_type len, Address &src)
Reads a datagram and returns the sender's address by reference.
std::string asString() const
Returns the socket handle as a string.