simple-socket library 1.1.5
Public Types | Public Member Functions | Friends

NET::SocketHandle< Socket > Class Template Reference

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

#include <SocketHandle.h>

List of all members.

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)
 SocketHandle (SocketHandle &other)
 copy constructor using move semantics
 SocketHandle ()
 constructs an invalid socket handle
 ~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.

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
Examples:

TCP_Server.cpp.

Definition at line 43 of file SocketHandle.h.


Member Typedef Documentation

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

socket type that was provided as template argument

Definition at line 50 of file SocketHandle.h.


Constructor & Destructor Documentation

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

constructs an invalid socket handle

Definition at line 53 of file SocketHandle.h.

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

copy constructor using move semantics

Definition at line 56 of file SocketHandle.h.

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

Definition at line 84 of file SocketHandle.h.


Member Function Documentation

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

assignment operator using move semantics

Definition at line 69 of file SocketHandle.h.

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.

Definition at line 93 of file SocketHandle.h.


Friends And Related Function Documentation

template<class Socket >
friend class TCPSocket [friend]

Definition at line 46 of file SocketHandle.h.

template<class Socket >
friend class SCTPSocket [friend]

Definition at line 47 of file SocketHandle.h.


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