36 class SocketProtocol ;
38 class DatagramSocket ;
59 G_EXCEPTION( SocketError ,
"socket error" ) ;
60 G_EXCEPTION( SocketBindError ,
"socket bind error" ) ;
61 typedef G::ReadWrite::size_type size_type ;
62 typedef G::ReadWrite::ssize_type ssize_type ;
109 void listen(
int backlog = 1 ) ;
114 virtual ssize_type
read(
char * buffer , size_type buffer_length )
override = 0 ;
118 virtual ssize_type
write(
const char * buf , size_type len )
override = 0 ;
123 virtual SOCKET
fd()
const ;
177 void shutdown(
bool for_writing =
true ) ;
181 Socket(
int domain ,
int type ,
int protocol ) ;
191 static int reason() ;
192 static std::string reasonStringImp(
int ) ;
193 static bool error(
int rc ) ;
194 static bool sizeError( ssize_type size ) ;
196 void saveReason()
const ;
199 void setOptionNoLinger() ;
200 void setOptionReuse() ;
201 void setOptionExclusive() ;
202 void setOptionPureV6(
bool ) ;
203 bool setOptionPureV6(
bool ,
NoThrow ) ;
204 void setOptionKeepAlive() ;
205 void setOption(
int ,
const char * ,
int ,
int ) ;
206 bool setOption(
int ,
const char * ,
int ,
int ,
NoThrow ) ;
207 bool setOptionImp(
int ,
int ,
const void * , socklen_t ) ;
208 std::pair<bool,Address> getAddress(
bool )
const ;
216 std::string m_reason_string ;
222 void operator=(
const Socket & ) ;
235 shared_ptr<StreamSocket> first ;
248 typedef Socket::size_type size_type ;
249 typedef Socket::ssize_type ssize_type ;
258 virtual ssize_type
read(
char * buffer , size_type buffer_length ) ;
261 virtual ssize_type
write(
const char * buf , size_type len ) ;
288 virtual ssize_type
read(
char * buffer , size_type len ) ;
291 virtual ssize_type
write(
const char * buffer , size_type len ) ;
299 ssize_type
writeto(
const char * buffer , size_type len ,
const Address & dst ) ;
bool canBindHint(const Address &address)
Returns true if the socket can probably be bound with the given address.
virtual ssize_type read(char *buffer, size_type len)
Override from Socket::read().
An abstract interface for reading and writing from a non-blocking i/o channel.
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().
void addExceptionHandler(EventHandler &handler)
Adds this socket to the event source list so that the given handler receives exception events...
virtual bool eWouldBlock()
Returns true if the previous socket operation failed because the socket would have blocked...
void addWriteHandler(EventHandler &handler)
Adds this socket to the event source list so that the given handler receives write events when flow c...
virtual ~DatagramSocket()
Destructor.
The GNet::Address class encapsulates a TCP/UDP transport address.
The Socket class encapsulates a non-blocking Unix socket file descriptor or a Windows 'SOCKET' handle...
A derivation of GNet::Socket for a datagram socket.
void addReadHandler(EventHandler &handler)
Adds this socket to the event source list so that the given handler receives read events...
StreamSocket(int address_domain)
Constructor with a hint of the bind()/connect() address to be used later.
virtual SOCKET fd() const
Returns the socket descriptor. Override from G::ReadWrite.
virtual ~StreamSocket()
Destructor.
A class that encapsulates a network file descriptor and hides knowledge of its o/s-spefific error val...
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.
void dropExceptionHandler()
Reverses addExceptionHandler().
bool eMsgSize()
Returns true if the previous socket operation failed with the EMSGSIZE error status.
void dropWriteHandler()
Reverses addWriteHandler().
void bind(const Address &)
Binds the socket with the given address.
std::string reasonString() const
Returns the failure reason as a string.
void disconnect()
Releases the association between two datagram endpoints reversing the effect of the previous Socket::...
std::pair< bool, Address > getPeerAddress() const
Retrieves address of socket's peer.
A derivation of GNet::Socket for a stream socket.
static Address defaultAddress()
Returns a default address, being the IPv4 wildcard address with a zero port number.
virtual ~Socket()
Destructor.
Socket(int domain, int type, int protocol)
Constructor used by derived classes.
A class which is used to return a new()ed socket to calling code, together with associated informatio...
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 eInProgress()
Returns true if the previous socket operation failed with the EINPROGRESS error status.
bool hasPeer() const
Returns true if the socket has a valid peer.
virtual ssize_type read(char *buffer, size_type buffer_length)
Override from Socket::read().
virtual ssize_type read(char *buffer, size_type buffer_length) override=0
Reads from the socket.
virtual ssize_type write(const char *buf, size_type len)
Override from Socket::write().
void shutdown(bool for_writing=true)
Shuts the socket for writing (or reading).
AcceptPair accept()
Accepts an incoming connection, returning a new()ed socket and the peer address.
Overload discriminator class for GNet::Socket.
virtual ssize_type write(const char *buffer, size_type len)
Override from Socket::write().
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.