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

An internet-protocol socket class. More...

#include <gsocket.h>

+ Inheritance diagram for GNet::Socket:
+ Collaboration diagram for GNet::Socket:

Classes

struct  Adopted
 Overload discriminator class for GNet::Socket. More...
 
struct  Config
 A configuration structure for GNet::Socket. More...
 

Public Member Functions

Address getLocalAddress () const
 Retrieves the local address of the socket. More...
 
std::pair< bool, AddressgetPeerAddress () const
 Retrieves address of socket's peer. More...
 
void bind (const Address &)
 Binds the socket with the given address. More...
 
bool bind (const Address &, std::nothrow_t)
 No-throw overload. Returns false on error. More...
 
unsigned long getBoundScopeId () const
 Returns the scope-id of the address last successfully bind()ed. More...
 
bool connect (const Address &addr, bool *done=nullptr)
 Initiates a connection to (or association with) the given address. More...
 
void listen ()
 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 new sends (1 or 2) and/or receives (0 or 2) will fail. More...
 
 Socket (const Socket &)=delete
 
 Socket (Socket &&)=delete
 
Socketoperator= (const Socket &)=delete
 
Socketoperator= (Socket &&)=delete
 
- Public Member Functions inherited from GNet::SocketBase
 ~SocketBase () override
 Destructor. More...
 
SOCKET fd () const noexcept override
 Returns the socket file descriptor. More...
 
Descriptor fdd () const noexcept
 Returns the socket descriptor. More...
 
bool eWouldBlock () const override
 Returns true if the previous socket operation failed because the socket would have blocked. More...
 
bool eInProgress () const
 Returns true if the previous socket operation failed with the EINPROGRESS error status. More...
 
bool eInUse () const
 Returns true if the previous socket bind operation failed because the socket was already in use. More...
 
bool eMsgSize () const
 Returns true if the previous socket operation failed with the EMSGSIZE error status. More...
 
bool eTooMany () const
 Returns true if the previous socket operation failed with the EMFILE error status, or similar. More...
 
bool eNotConn () const
 Returns true if the previous socket operation failed with the ENOTCONN error status, or similar. More...
 
void addReadHandler (EventHandler &, EventState)
 Adds this socket to the event source list so that the given handler receives read events. More...
 
void dropReadHandler () noexcept
 Reverses addReadHandler(). More...
 
void addWriteHandler (EventHandler &, EventState)
 Adds this socket to the event source list so that the given handler receives write events when flow control is released. More...
 
void dropWriteHandler () noexcept
 Reverses addWriteHandler(). More...
 
void addOtherHandler (EventHandler &, EventState)
 Adds this socket to the event source list so that the given handler receives exception events. More...
 
void dropOtherHandler () noexcept
 Reverses addOtherHandler(). More...
 
std::string asString () const
 Returns the socket handle as a string. More...
 
std::string reason () const
 Returns the reason for the previous error. More...
 
 SocketBase (const SocketBase &)=delete
 
 SocketBase (SocketBase &&)=delete
 
SocketBaseoperator= (const SocketBase &)=delete
 
SocketBaseoperator= (SocketBase &&)=delete
 
- Public Member Functions inherited from G::ReadWrite
virtual ssize_type read (char *buffer, size_type buffer_length)=0
 Reads data. More...
 
virtual ssize_type write (const char *buf, size_type len)=0
 Sends data. More...
 
virtual bool eWouldBlock () const =0
 See read() and write(). More...
 
virtual SOCKET fd () const noexcept=0
 Returns the file descriptor. More...
 
virtual ~ReadWrite ()=default
 Destructor.
 

Static Public Member Functions

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 reason. More...
 
- Static Public Member Functions inherited from GNet::SocketBase
static bool supports (Address::Family, int type, int protocol)
 Returns true if sockets can be created with the given parameters. More...
 

Protected Member Functions

 Socket (Address::Family, int type, int protocol, const Config &)
 
 Socket (Address::Family, Descriptor s, const Accepted &, const Config &)
 
 Socket (Address::Family, Descriptor s, const Adopted &, const Config &)
 
void setOptionLinger (int onoff, int time)
 
void setOptionKeepAlive ()
 
void setOptionFreeBind ()
 
- Protected Member Functions inherited from GNet::SocketBase
 SocketBase (Address::Family, int type, int protocol)
 Constructor used by derived classes. More...
 
 SocketBase (Address::Family, Descriptor s)
 Constructor used by derived classes. More...
 
 SocketBase (Address::Family, Descriptor s, const Accepted &)
 Constructor used by StreamSocket::accept() to create a socket object from a newly accept()ed socket handle. More...
 
 SocketBase (const Raw &, int domain, int type, int protocol)
 Constructor for a raw socket. More...
 
void clearReason ()
 Clears the saved errno. More...
 
void saveReason ()
 Saves the current errno following error()/sizeError(). More...
 
void saveReason () const
 Saves the current errno following error()/sizeError(). More...
 
bool isFamily (Address::Family) const
 Returns true if the socket family is as given. More...
 

Static Protected Member Functions

static Address getLocalAddress (Descriptor)
 
- Static Protected Member Functions inherited from GNet::SocketBase
static bool error (int rc)
 Returns true if the given return code indicates an error. More...
 
static bool sizeError (ssize_type size)
 Returns true if the given write() return value indicates an error. More...
 

Additional Inherited Members

- Public Types inherited from GNet::SocketBase
using size_type = G::ReadWrite::size_type
 
using ssize_type = G::ReadWrite::ssize_type
 
- Public Types inherited from G::ReadWrite
using size_type = std::size_t
 
using ssize_type = ssize_t
 

Detailed Description

An internet-protocol socket class.

Provides bind(), listen(), and connect(); the base classes provide write(); and derived classes provide accept() and read().

Definition at line 226 of file gsocket.h.

Constructor & Destructor Documentation

◆ Socket() [1/3]

GNet::Socket::Socket ( Address::Family  af,
int  type,
int  protocol,
const Config config 
)
protected

Definition at line 230 of file gsocket.cpp.

◆ Socket() [2/3]

GNet::Socket::Socket ( Address::Family  af,
Descriptor  s,
const Accepted a,
const Config config 
)
protected

Definition at line 236 of file gsocket.cpp.

◆ Socket() [3/3]

GNet::Socket::Socket ( Address::Family  af,
Descriptor  s,
const Adopted ,
const Config config 
)
protected

Definition at line 242 of file gsocket.cpp.

Member Function Documentation

◆ bind() [1/2]

void GNet::Socket::bind ( const Address local_address)

Binds the socket with the given address.

Definition at line 248 of file gsocket.cpp.

◆ bind() [2/2]

bool GNet::Socket::bind ( const Address local_address,
std::nothrow_t   
)

No-throw overload. Returns false on error.

Definition at line 266 of file gsocket.cpp.

◆ canBindHint()

std::string GNet::Socket::canBindHint ( const Address address,
bool  stream_socket,
const Config config 
)
static

Returns the empty string if a socket could probably be bound with the given address or a failure reason.

Some implementations will always return the empty string.

Definition at line 160 of file gsocket_unix.cpp.

◆ connect()

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 289 of file gsocket.cpp.

◆ getBoundScopeId()

unsigned long GNet::Socket::getBoundScopeId ( ) const

Returns the scope-id of the address last successfully bind()ed.

Note that getLocalAddress() has a zero scope-id even after bind()ing an address with a non-zero scope-id.

Definition at line 284 of file gsocket.cpp.

◆ getLocalAddress() [1/2]

GNet::Address GNet::Socket::getLocalAddress ( ) const

Retrieves the local address of the socket.

Definition at line 343 of file gsocket.cpp.

◆ getLocalAddress() [2/2]

GNet::Address GNet::Socket::getLocalAddress ( Descriptor  fd)
staticprotected

Definition at line 334 of file gsocket.cpp.

◆ getPeerAddress()

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

Retrieves address of socket's peer.

Returns false in 'first' if none, ie. not yet connected.

Definition at line 355 of file gsocket.cpp.

◆ listen()

void GNet::Socket::listen ( )

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

Definition at line 320 of file gsocket.cpp.

◆ setOptionFreeBind()

void GNet::Socket::setOptionFreeBind ( )
protected

Definition at line 404 of file gsocket.cpp.

◆ setOptionKeepAlive()

void GNet::Socket::setOptionKeepAlive ( )
protected

Definition at line 399 of file gsocket.cpp.

◆ setOptionLinger()

void GNet::Socket::setOptionLinger ( int  onoff,
int  time 
)
protected

Definition at line 410 of file gsocket.cpp.

◆ shutdown()

void GNet::Socket::shutdown ( int  how = 1)

Modifies the local socket state 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. See also RFC-793 3.5.

Errors are ignored.

Definition at line 369 of file gsocket.cpp.


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