E-MailRelay
Classes | Public Types | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
GNet::Socket Class Referenceabstract

A network socket base class, holding a non-blocking socket file descriptor. More...

#include <gsocket.h>

+ Inheritance diagram for GNet::Socket:

Classes

struct  Accepted
 Overload discriminator class for GNet::Socket. More...
 
struct  NoThrow
 Overload discriminator class for GNet::Socket. More...
 

Public Types

typedef G::ReadWrite::size_type size_type
 
typedef G::ReadWrite::ssize_type ssize_type
 
- Public Types inherited from G::ReadWrite
typedef size_t size_type
 
typedef ssize_t ssize_type
 

Public Member Functions

virtual ~Socket ()
 Destructor.
 
std::pair< bool, Address > getLocalAddress () const
 Retrieves local address of the socket. More...
 
std::pair< bool, Address > getPeerAddress () const
 Retrieves address of socket's peer. More...
 
bool hasPeer () const
 Returns true if the socket has a valid peer. More...
 
void bind (const Address &)
 Binds the socket with the given address.
 
bool bind (const Address &, NoThrow)
 No-throw overload. Returns false on error.
 
bool canBindHint (const Address &address)
 Returns true if the socket can probably be bound with the given address. More...
 
bool connect (const Address &addr, bool *done=nullptr)
 Initiates a connection to (or association with) the given address. More...
 
void listen (int backlog=1)
 Starts the socket listening on the bound address for incoming connections or incoming datagrams. More...
 
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. More...
 
virtual ssize_type read (char *buffer, size_type buffer_length) override=0
 Reads from the socket. More...
 
virtual ssize_type write (const char *buf, size_type len) override=0
 Writes to the socket. More...
 
virtual SOCKET fd () const override
 Returns the socket descriptor. Override from G::ReadWrite.
 
virtual bool eWouldBlock () override
 Returns true if the previous socket operation failed because the socket would have blocked. More...
 
bool eInProgress ()
 Returns true if the previous socket operation failed with the EINPROGRESS error status. More...
 
bool eMsgSize ()
 Returns true if the previous socket operation failed with the EMSGSIZE error status. More...
 
void addReadHandler (EventHandler &, ExceptionHandler &)
 Adds this socket to the event source list so that the given handler receives read events. More...
 
void dropReadHandler ()
 Reverses addReadHandler().
 
void addWriteHandler (EventHandler &, ExceptionHandler &)
 Adds this socket to the event source list so that the given handler receives write events when flow control is released. More...
 
void dropWriteHandler ()
 Reverses addWriteHandler().
 
void addOtherHandler (EventHandler &, ExceptionHandler &)
 Adds this socket to the event source list so that the given handler receives exception events. More...
 
void dropOtherHandler ()
 Reverses addOtherHandler().
 
std::string asString () const
 Returns the socket handle as a string. More...
 
std::string reason () const
 Returns the reason for the previous error.
 
- Public Member Functions inherited from G::ReadWrite
virtual ~ReadWrite ()
 Destructor.
 

Protected Member Functions

 Socket (int domain, int type, int protocol)
 Constructor used by derived classes. More...
 
 Socket (Descriptor s, const Accepted &)
 Constructor which creates a socket object from a socket handle from accept(). More...
 
bool create (int, int, int)
 
void saveReason ()
 
void saveReason () const
 
bool prepare (bool)
 
std::pair< bool, Address > getAddress (bool) const
 
void setOptionsOnBind (bool)
 
void setOptionsOnConnect (bool)
 
void setOptionLingerImp (int, int)
 
void setOptionNoLinger ()
 
void setOptionReuse ()
 
void setOptionExclusive ()
 
void setOptionPureV6 (bool)
 
bool setOptionPureV6 (bool, NoThrow)
 
void setOptionKeepAlive ()
 

Static Protected Member Functions

static std::string reasonString (int)
 
static bool error (int rc)
 
static bool sizeError (ssize_type size)
 

Protected Attributes

int m_reason
 
std::string m_reason_string
 
int m_domain
 
Descriptor m_socket
 
HANDLE m_handle
 

Detailed Description

A network socket base class, holding a non-blocking socket file descriptor.

Provides bind(), listen(), connect(), write(); derived classes provide accept() and read(). Also interfaces to the event loop with addReadHandler() and addWriteHandler().

Definition at line 49 of file gsocket.h.

Constructor & Destructor Documentation

GNet::Socket::Socket ( int  domain,
int  type,
int  protocol 
)
protected

Constructor used by derived classes.

Creates the socket using socket().

Definition at line 30 of file gsocket.cpp.

GNet::Socket::Socket ( Descriptor  s,
const Accepted &   
)
protected

Constructor which creates a socket object from a socket handle from accept().

Used only by StreamSocket::accept().

Definition at line 44 of file gsocket.cpp.

Member Function Documentation

void GNet::Socket::addOtherHandler ( EventHandler &  handler,
ExceptionHandler &  eh 
)

Adds this socket to the event source list so that the given handler receives exception events.

A TCP exception event should be treated as a disconnection event. (Not used for datagram sockets.)

Definition at line 221 of file gsocket.cpp.

void GNet::Socket::addReadHandler ( EventHandler &  handler,
ExceptionHandler &  eh 
)

Adds this socket to the event source list so that the given handler receives read events.

Definition at line 209 of file gsocket.cpp.

void GNet::Socket::addWriteHandler ( EventHandler &  handler,
ExceptionHandler &  eh 
)

Adds this socket to the event source list so that the given handler receives write events when flow control is released.

(Not used for datagram sockets.)

Definition at line 215 of file gsocket.cpp.

std::string GNet::Socket::asString ( ) const

Returns the socket handle as a string.

Only used in debugging.

Definition at line 242 of file gsocket.cpp.

bool GNet::Socket::canBindHint ( const Address &  address)

Returns true if the socket can probably be bound with the given address.

Some implementations will always return true. This method should be used on a temporary socket of the correct dynamic type since this socket may become unusable.

Definition at line 108 of file gsocket_unix.cpp.

bool GNet::Socket::connect ( const Address &  addr,
bool *  done = nullptr 
)

Initiates a connection to (or association with) the given address.

Returns false on error.

If successful, a 'done' flag is returned by reference indicating whether the connect completed immediately. Normally a stream socket connection will take some time to complete so the 'done' flag will be false: the completion will be indicated by a write event some time later.

For datagram sockets this sets up an association between two addresses. The socket should first be bound with a local address.

Definition at line 112 of file gsocket.cpp.

bool GNet::Socket::eInProgress ( )

Returns true if the previous socket operation failed with the EINPROGRESS error status.

When connecting this can be considered a non-error.

Definition at line 98 of file gsocket_unix.cpp.

bool GNet::Socket::eMsgSize ( )

Returns true if the previous socket operation failed with the EMSGSIZE error status.

When writing to a datagram socket this indicates that the message was too big to send atomically.

Definition at line 103 of file gsocket_unix.cpp.

bool GNet::Socket::eWouldBlock ( )
overridevirtual

Returns true if the previous socket operation failed because the socket would have blocked.

Override from G::ReadWrite.

Implements G::ReadWrite.

Definition at line 93 of file gsocket_unix.cpp.

std::pair< bool, GNet::Address > GNet::Socket::getLocalAddress ( ) const

Retrieves local address of the socket.

Pair.first is false on error.

Definition at line 194 of file gsocket.cpp.

std::pair< bool, GNet::Address > GNet::Socket::getPeerAddress ( ) const

Retrieves address of socket's peer.

'Pair.first' is false on error.

Definition at line 199 of file gsocket.cpp.

bool GNet::Socket::hasPeer ( ) const

Returns true if the socket has a valid peer.

This can be used to see if a connect succeeded.

Definition at line 204 of file gsocket.cpp.

void GNet::Socket::listen ( int  backlog = 1)

Starts the socket listening on the bound address for incoming connections or incoming datagrams.

Definition at line 166 of file gsocket.cpp.

virtual ssize_type GNet::Socket::read ( char *  buffer,
size_type  buffer_length 
)
overridepure virtual

Reads from the socket.

Override from G::ReadWrite.

Implements G::ReadWrite.

Implemented in GNet::DatagramSocket, and GNet::StreamSocket.

void GNet::Socket::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.

If receives are shut-down then anything received will be rejected with a RST.

If sends are shut-down then the transmit queue is drained and a final empty FIN packet is sent when fully acknowledged.

Errors are ignored.

Definition at line 249 of file gsocket.cpp.

GNet::Socket::ssize_type GNet::Socket::write ( const char *  buf,
size_type  len 
)
overridepure virtual

Writes to the socket.

This is a default implementation that can be called explicitly from derived classes. Override from G::ReadWrite.

Implements G::ReadWrite.

Implemented in GNet::DatagramSocket, and GNet::StreamSocket.

Definition at line 147 of file gsocket.cpp.


The documentation for this class was generated from the following files: