21 #ifndef G_NET_SOCKET__H
22 #define G_NET_SOCKET__H
35 class SocketProtocol ;
37 class DatagramSocket ;
52 G_EXCEPTION( SocketError ,
"socket error" ) ;
53 G_EXCEPTION( SocketBindError ,
"socket bind error" ) ;
54 typedef G::ReadWrite::size_type size_type ;
55 typedef G::ReadWrite::ssize_type ssize_type ;
104 void listen(
int backlog = 1 ) ;
122 virtual ssize_type
read(
char * buffer , size_type buffer_length )
override = 0 ;
126 virtual ssize_type
write(
const char * buf , size_type len )
override = 0 ;
131 virtual SOCKET
fd()
const override ;
181 std::string
reason()
const ;
185 Socket(
int domain ,
int type ,
int protocol ) ;
195 static std::string reasonString(
int ) ;
196 static bool error(
int rc ) ;
197 static bool sizeError( ssize_type size ) ;
198 bool create(
int ,
int ,
int ) ;
200 void saveReason()
const ;
201 bool prepare(
bool ) ;
202 std::pair<bool,Address> getAddress(
bool )
const ;
203 void setOptionsOnBind(
bool ) ;
204 void setOptionsOnConnect(
bool ) ;
205 void setOptionLingerImp(
int ,
int ) ;
206 void setOptionNoLinger() ;
207 void setOptionReuse() ;
208 void setOptionExclusive() ;
209 void setOptionPureV6(
bool ) ;
210 bool setOptionPureV6(
bool ,
NoThrow ) ;
211 void setOptionKeepAlive() ;
215 std::string m_reason_string ;
222 void operator=(
const Socket & ) ;
226 void setOption(
int ,
const char * ,
int ,
int ) ;
227 bool setOption(
int ,
const char * ,
int ,
int ,
NoThrow ) ;
228 bool setOptionImp(
int ,
int ,
const void * , socklen_t ) ;
238 shared_ptr<StreamSocket> first ;
249 typedef Socket::size_type size_type ;
250 typedef Socket::ssize_type ssize_type ;
265 virtual ssize_type
read(
char * buffer , size_type buffer_length )
override ;
268 virtual ssize_type
write(
const char * buf , size_type len )
override ;
279 void setOptionsOnCreate(
bool ) ;
280 void setOptionsOnAccept() ;
295 virtual ssize_type
read(
char * buffer , size_type len )
override ;
298 virtual ssize_type
write(
const char * buffer , size_type len )
override ;
306 ssize_type
writeto(
const char * buffer , size_type len ,
const Address & dst ) ;
void addReadHandler(EventHandler &, ExceptionHandler &)
Adds this socket to the event source list so that the given handler receives read events...
bool canBindHint(const Address &address)
Returns true if the socket can probably be bound with the given address.
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.
An abstract interface for reading and writing from a non-blocking i/o channel.
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.
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().
A network socket base class, holding a non-blocking socket file descriptor.
A derivation of GNet::Socket for a datagram socket.
StreamSocket(int address_domain)
Constructor with a hint of the bind()/connect() address to be used later.
virtual ~StreamSocket()
Destructor.
A class that encapsulates a network socket file descriptor and an associated windows event handle...
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.
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.
Overload discriminator class for GNet::Socket.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
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.
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.
Overload discriminator class for GNet::StreamSocket.
virtual ssize_type write(const char *buffer, size_type len) override
Override from Socket::write().
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...
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.
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().
virtual ssize_type read(char *buffer, size_type buffer_length) override=0
Reads from the socket.
AcceptPair accept()
Accepts an incoming connection, returning a new()ed socket and the peer address.
virtual bool eWouldBlock() override
Returns true if the previous socket operation failed because the socket would have blocked...
Overload discriminator class for GNet::Socket.
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.