simple-socket library 1.5.0
Loading...
Searching...
No Matches
NET::SocketHandle< Socket > Class Template Reference

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

#include <simple_socket/SocketHandle.h>

Public Types

typedef Socket socket_type
 socket type that was provided as template argument

Public Member Functions

 SocketHandle () noexcept
 constructs an invalid socket handle
 SocketHandle (const SocketHandle &)=delete
SocketHandle & operator= (const SocketHandle &)=delete
 SocketHandle (SocketHandle &&other) noexcept
 move constructor: transfers ownership from other
SocketHandle & operator= (SocketHandle &&other) noexcept
 move assignment: closes the currently owned descriptor first
 operator bool () const noexcept
 returns whether the socket handle is valid

Friends

class TCPSocket
class SCTPSocket
class UnixStreamSocket

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 is move-only. Moving transfers ownership of the socket file descriptor and leaves the source handle invalid. Copying is not allowed. Constructing a socket from a handle also transfers ownership.

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(std::move(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 38 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 46 of file SocketHandle.h.

Constructor & Destructor Documentation

◆ SocketHandle() [1/2]

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

constructs an invalid socket handle

Definition at line 49 of file SocketHandle.h.

◆ SocketHandle() [2/2]

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

move constructor: transfers ownership from other

Definition at line 55 of file SocketHandle.h.

◆ ~SocketHandle()

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

Definition at line 65 of file SocketHandle.h.

Member Function Documentation

◆ operator bool()

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

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 75 of file SocketHandle.h.

◆ operator=()

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

move assignment: closes the currently owned descriptor first

Definition at line 58 of file SocketHandle.h.

◆ SCTPSocket

template<class Socket>
friend class SCTPSocket
friend

Definition at line 42 of file SocketHandle.h.

◆ TCPSocket

template<class Socket>
friend class TCPSocket
friend

Definition at line 41 of file SocketHandle.h.

◆ UnixStreamSocket

template<class Socket>
friend class UnixStreamSocket
friend

Definition at line 43 of file SocketHandle.h.


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