simple-socket library 1.2.0
NET::SocketHandle< Socket > Class Template Reference

A simple class to provide strict ownership of socket handles. More...

#include <SocketHandle.h>

Public Types

typedef Socket socket_type
 socket type that was provided as template argument
 

Public Member Functions

 operator bool () const
 returns whether the socket handle is valid
 
SocketHandleoperator= (SocketHandle &other)
 assignment operator using move semantics
 
 SocketHandle ()
 constructs an invalid socket handle
 
 SocketHandle (SocketHandle &other)
 copy constructor using move semantics
 
 ~SocketHandle ()
 

Friends

class SCTPSocket
 
class TCPSocket
 

Detailed Description

template<class Socket>
class NET::SocketHandle< Socket >

A simple class to provide strict ownership of socket handles.

SocketHandle is used to return accepted connections to the user, and not expose the socket file descriptor. It also ensures all an accepted socket is closed in the case no new socket is created with this handle.

SocketHandle implements move semantics, so only one SocketHandle can be active for the same connection at any given moment. If SocketHandle is copied/passed to a function, etc. the source/passed in SocketHandle is made invalid. The SocketHandle also gets invalid if an socket is constructed from the handle.

The move implementation does not need C++11, and is implemented without the need for r-value references. This also means that moving is not done like in C++11 (using std::move and std::forward) it is instead done silently and implicitly when copying the SocketHandle object.

After a SocketHandle gets returned from an accept() call, the SocketHandle has to be checked wheter it is valid or not.

Usage example:

// socket is a TCPSocket listening for connections
TCPSocket::Handle handle = socket.timedAccept(1000);
if(!handle) {
std::cout << "no one tried to connect in time" << std::endl;
return;
}
TCPSocket acceptedSocket(handle);
// use the socket
TCP socket class.
Definition TCPSocket.h:11
SocketHandle< TCPSocket > Handle
Handle for a new socket returned by accept.
Definition TCPSocket.h:14

Definition at line 48 of file SocketHandle.h.

Member Typedef Documentation

◆ socket_type

template<class Socket>
typedef Socket NET::SocketHandle< Socket >::socket_type

socket type that was provided as template argument

Definition at line 55 of file SocketHandle.h.

Constructor & Destructor Documentation

◆ SocketHandle() [1/2]

template<class Socket>
NET::SocketHandle< Socket >::SocketHandle ( )
inline

constructs an invalid socket handle

Definition at line 58 of file SocketHandle.h.

◆ SocketHandle() [2/2]

template<class Socket>
NET::SocketHandle< Socket >::SocketHandle ( SocketHandle< Socket > & other)
inline

copy constructor using move semantics

Definition at line 61 of file SocketHandle.h.

◆ ~SocketHandle()

template<class Socket>
NET::SocketHandle< Socket >::~SocketHandle ( )
inline

Definition at line 89 of file SocketHandle.h.

Member Function Documentation

◆ operator=()

template<class Socket>
SocketHandle & NET::SocketHandle< Socket >::operator= ( SocketHandle< Socket > & other)
inline

assignment operator using move semantics

Definition at line 74 of file SocketHandle.h.

◆ operator bool()

template<class Socket>
NET::SocketHandle< Socket >::operator bool ( ) const
inline

returns whether the socket handle is valid

Checks whether a socket can be constructed from the handle or not. If accept() returned an invalid handle (e.g. timed out timedAccept() call) this should be used to verify the handle. Trying to construct a socket from an invalid handle will raise a SocketException.

Exceptions
SocketExceptionif trying to use an invalid SocketHandle to create socket

Definition at line 99 of file SocketHandle.h.

Friends And Related Symbol Documentation

◆ TCPSocket

template<class Socket>
friend class TCPSocket
friend

Definition at line 51 of file SocketHandle.h.

◆ SCTPSocket

template<class Socket>
friend class SCTPSocket
friend

Definition at line 52 of file SocketHandle.h.


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