blocxx
BLOCXX_NAMESPACE::Socket Class Reference

#include <Socket.hpp>

Inheritance diagram for BLOCXX_NAMESPACE::Socket:
BLOCXX_NAMESPACE::SelectableIFC BLOCXX_NAMESPACE::IOIFC BLOCXX_NAMESPACE::IntrusiveCountableBase

Classes

struct  ShutDownMechanismFactory
 

Public Types

typedef UnnamedPipeRef ShutDownMechanism_t
 
- Public Types inherited from BLOCXX_NAMESPACE::IOIFC
enum  ErrorAction { E_THROW_ON_ERROR , E_RETURN_ON_ERROR }
 

Public Member Functions

 Socket ()
 Construct a non-SSL Socket.
 
 Socket (const SSLClientCtxRef &sslCtx)
 Construct a Socket.
 
 Socket (SocketFlags::ESSLFlag isSSL) BLOCXX_DEPRECATED
 Allocate a new Socket.
 
 Socket (SocketHandle_t fd, SocketAddress::AddressType addrType, SocketFlags::ESSLFlag isSSL=SocketFlags::E_NOT_SSL)
 TODO: Make a replacement for this function so it can be deprecated.
 
 Socket (const SocketAddress &addr, SocketFlags::ESSLFlag isSSL=SocketFlags::E_NOT_SSL)
 TODO: Make a replacement for this function so it can be deprecated.
 
void connect (const SocketAddress &addr)
 Connect to a peer node.
 
void disconnect ()
 Disconnect the (presumably) open connection.
 
BLOCXX_DEPRECATED void setReceiveTimeout (int seconds)
 Set the receive timeout on the socket.
 
void setReceiveTimeout (const Timeout &timeout)
 Set the receive timeout on the socket.
 
Timeout getReceiveTimeout () const
 Get the receive timeout.
 
BLOCXX_DEPRECATED void setSendTimeout (int seconds)
 Set the send timeout on the socket.
 
void setSendTimeout (const Timeout &timeout)
 Set the send timeout on the socket.
 
Timeout getSendTimeout () const
 Get the send timeout.
 
BLOCXX_DEPRECATED void setConnectTimeout (int seconds)
 Set the connect timeout on the socket.
 
void setConnectTimeout (const Timeout &timeout)
 Set the connect timeout on the socket.
 
Timeout getConnectTimeout () const
 Get the connect timeout.
 
BLOCXX_DEPRECATED void setTimeouts (int seconds)
 Set all timeouts (send, receive, connect)
 
void setTimeouts (const Timeout &timeout)
 Set all timeouts (send, receive, connect)
 
bool receiveTimeOutExpired () const
 Has the receive timeout expired?
 
int write (const void *dataOut, int dataOutLen, ErrorAction errorAsException=E_RETURN_ON_ERROR)
 Write some data to the socket.
 
int read (void *dataIn, int dataInLen, ErrorAction errorAsException=E_RETURN_ON_ERROR)
 Read from the socket.
 
BLOCXX_DEPRECATED bool waitForInput (int timeOutSecs)
 Wait for input on the socket for a specified length of time.
 
bool waitForInput (const Timeout &timeout=Timeout::infinite)
 Wait for input on the socket for a specified length of time.
 
BLOCXX_DEPRECATED bool waitForOutput (int timeOutSecs)
 Wait for output on the socket for a specified length of time.
 
bool waitForOutput (const Timeout &timeout=Timeout::infinite)
 Wait for output on the socket for a specified length of time.
 
SocketAddress getLocalAddress () const
 Get the local address associated with the socket connection.
 
SocketAddress getPeerAddress () const
 Get the peer address associated with the socket connection.
 
std::istream & getInputStream ()
 Get an istream to read from the socket.
 
std::ostream & getOutputStream ()
 Get an ostream to write to the socket.
 
BLOCXX_DEPRECATED std::iostream & getIOStream ()
 Get an iostream to read/write from/to the socket.
 
Select_t getSelectObj () const
 
SocketHandle_t getfd ()
 Get the socket handle for the socket.
 
bool isConnected () const
 Get connected state.
 
SSL * getSSL () const
 get the SSL structure associated with the socket (if it is an SSL socket)
 
bool peerCertVerified () const
 did the peer certificate pass verification?
 
- Public Member Functions inherited from BLOCXX_NAMESPACE::SelectableIFC
virtual ~SelectableIFC ()
 
- Public Member Functions inherited from BLOCXX_NAMESPACE::IOIFC
virtual ~IOIFC ()
 

Static Public Member Functions

static void createShutDownMechanism ()
 
static void shutdownAllSockets ()
 Call this to shutdown all sockets.
 
static bool gotShutDown () BLOCXX_DEPRECATED
 Have the sockets been shutdown?
 
static void deleteShutDownMechanism ()
 
static ShutDownMechanism_t getShutDownMechanism ()
 

Static Public Attributes

static const int INFINITE_TIMEOUT BLOCXX_DEPRECATED = -1
 

Private Member Functions

 Socket (SocketHandle_t fd, SocketAddress::AddressType addrType, const SSLServerCtxRef &sslCtx)
 Allocate a new Socket based on an existing handle.
 

Private Attributes

SocketBaseImplRef m_impl
 

Static Private Attributes

static LazyGlobal< Socket::ShutDownMechanism_t, int, ShutDownMechanismFactorys_shutDownMechanism = { 0, 0, {0, PTHREAD_MUTEX_INITIALIZER} }
 

Friends

class ServerSocketImpl
 

Additional Inherited Members

- Protected Member Functions inherited from BLOCXX_NAMESPACE::IntrusiveCountableBase
 IntrusiveCountableBase ()
 
 IntrusiveCountableBase (const IntrusiveCountableBase &)
 
IntrusiveCountableBaseoperator= (const IntrusiveCountableBase &)
 
virtual ~IntrusiveCountableBase ()
 

Detailed Description

Definition at line 65 of file Socket.hpp.

Member Typedef Documentation

◆ ShutDownMechanism_t

Constructor & Destructor Documentation

◆ Socket() [1/6]

BLOCXX_NAMESPACE::Socket::Socket ( )

Construct a non-SSL Socket.

Definition at line 60 of file Socket.cpp.

References m_impl.

◆ Socket() [2/6]

BLOCXX_NAMESPACE::Socket::Socket ( const SSLClientCtxRef & sslCtx)

Construct a Socket.

Parameters
sslCtxThe SSL client context. If null, the socket will not use SSL.

Definition at line 52 of file Socket_needs_SSL.cpp.

References BLOCXX_THROW, and m_impl.

◆ Socket() [3/6]

BLOCXX_NAMESPACE::Socket::Socket ( SocketFlags::ESSLFlag isSSL)

Allocate a new Socket.

Parameters
isSSLis it an ssl socket?

Definition at line 69 of file Socket_needs_SSL.cpp.

References BLOCXX_THROW, BLOCXX_NAMESPACE::SocketFlags::E_SSL, and m_impl.

◆ Socket() [4/6]

BLOCXX_NAMESPACE::Socket::Socket ( SocketHandle_t fd,
SocketAddress::AddressType addrType,
SocketFlags::ESSLFlag isSSL = SocketFlags::E_NOT_SSL )

TODO: Make a replacement for this function so it can be deprecated.

Allocate a new Socket based on an existing handle. This is used by ServerSocket::accept()

Parameters
fda handle to the existing socket
isSSLis it an SSL socket?

Definition at line 85 of file Socket_needs_SSL.cpp.

References BLOCXX_THROW, BLOCXX_NAMESPACE::SocketFlags::E_SSL, and m_impl.

◆ Socket() [5/6]

BLOCXX_NAMESPACE::Socket::Socket ( const SocketAddress & addr,
SocketFlags::ESSLFlag isSSL = SocketFlags::E_NOT_SSL )

TODO: Make a replacement for this function so it can be deprecated.

Allocate a new Socket and connect it to a peer machine

Parameters
addrthe address of the peer machine
addrTypethe address type of the socket @isSSL is it an SSL socket?
Exceptions
SocketException

Definition at line 120 of file Socket_needs_SSL.cpp.

References BLOCXX_THROW, BLOCXX_NAMESPACE::SocketFlags::E_SSL, and m_impl.

◆ Socket() [6/6]

BLOCXX_NAMESPACE::Socket::Socket ( SocketHandle_t fd,
SocketAddress::AddressType addrType,
const SSLServerCtxRef & sslCtx )
private

Allocate a new Socket based on an existing handle.

This is used by ServerSocket::accept()

Parameters
fda handle to the existing socket
addrTypethe address type of the socket
sslCtxa SSL server context reference

Definition at line 103 of file Socket_needs_SSL.cpp.

References BLOCXX_THROW, and m_impl.

Member Function Documentation

◆ connect()

void BLOCXX_NAMESPACE::Socket::connect ( const SocketAddress & addr)
inline

Connect to a peer node.

Parameters
addrThe address of the machine to connect to.
Exceptions
SocketException

Definition at line 105 of file Socket.hpp.

References m_impl.

◆ createShutDownMechanism()

◆ deleteShutDownMechanism()

void BLOCXX_NAMESPACE::Socket::deleteShutDownMechanism ( )
static

Definition at line 113 of file Socket.cpp.

References s_shutDownMechanism, and BLOCXX_NAMESPACE::shutdownMutex.

◆ disconnect()

void BLOCXX_NAMESPACE::Socket::disconnect ( )
inline

Disconnect the (presumably) open connection.

Definition at line 110 of file Socket.hpp.

References m_impl.

◆ getConnectTimeout()

Timeout BLOCXX_NAMESPACE::Socket::getConnectTimeout ( ) const
inline

Get the connect timeout.

Returns
The number of seconds of the connect timeout

Definition at line 157 of file Socket.hpp.

References m_impl.

◆ getfd()

SocketHandle_t BLOCXX_NAMESPACE::Socket::getfd ( )
inline

Get the socket handle for the socket.

Returns
the socket handle

Definition at line 269 of file Socket.hpp.

References m_impl.

◆ getInputStream()

std::istream & BLOCXX_NAMESPACE::Socket::getInputStream ( )
inline

Get an istream to read from the socket.

Returns
a istream& which can be used for socket input
Exceptions
SocketException

Definition at line 245 of file Socket.hpp.

References m_impl.

◆ getIOStream()

BLOCXX_DEPRECATED std::iostream & BLOCXX_NAMESPACE::Socket::getIOStream ( )
inline

Get an iostream to read/write from/to the socket.

Returns
an iostream& which can be used for socket input and output.
Exceptions
SocketException

Definition at line 259 of file Socket.hpp.

References BLOCXX_DEPRECATED, and m_impl.

◆ getLocalAddress()

SocketAddress BLOCXX_NAMESPACE::Socket::getLocalAddress ( ) const
inline

Get the local address associated with the socket connection.

Returns
an SocketAddress representing the local machine

Definition at line 234 of file Socket.hpp.

References m_impl.

◆ getOutputStream()

std::ostream & BLOCXX_NAMESPACE::Socket::getOutputStream ( )
inline

Get an ostream to write to the socket.

Returns
an ostream& which can be used for socket output
Exceptions
SocketException

Definition at line 252 of file Socket.hpp.

References m_impl.

◆ getPeerAddress()

SocketAddress BLOCXX_NAMESPACE::Socket::getPeerAddress ( ) const
inline

Get the peer address associated with the socket connection.

Returns
an SocketAddress representing the peer machine

Definition at line 239 of file Socket.hpp.

References m_impl.

◆ getReceiveTimeout()

Timeout BLOCXX_NAMESPACE::Socket::getReceiveTimeout ( ) const
inline

Get the receive timeout.

Returns
The number of seconds of the receive timeout

Definition at line 127 of file Socket.hpp.

References m_impl.

◆ getSelectObj()

Select_t BLOCXX_NAMESPACE::Socket::getSelectObj ( ) const
inlinevirtual
Returns
The Select_t associated with this sockect.

Implements BLOCXX_NAMESPACE::SelectableIFC.

Definition at line 264 of file Socket.hpp.

References m_impl.

◆ getSendTimeout()

Timeout BLOCXX_NAMESPACE::Socket::getSendTimeout ( ) const
inline

Get the send timeout.

Returns
The number of seconds of the send timeout

Definition at line 142 of file Socket.hpp.

References m_impl.

◆ getShutDownMechanism()

static ShutDownMechanism_t BLOCXX_NAMESPACE::Socket::getShutDownMechanism ( )
inlinestatic

◆ getSSL()

SSL * BLOCXX_NAMESPACE::Socket::getSSL ( ) const

get the SSL structure associated with the socket (if it is an SSL socket)

Returns
a pointer to the SSL structure

Definition at line 138 of file Socket_needs_SSL.cpp.

References m_impl.

◆ gotShutDown()

bool BLOCXX_NAMESPACE::Socket::gotShutDown ( )
static

Have the sockets been shutdown?

Returns
true if the sockets have been shutdown

Definition at line 127 of file Socket.cpp.

References BLOCXX_NAMESPACE::b_gotShutDown, and BLOCXX_NAMESPACE::shutdownMutex.

◆ isConnected()

bool BLOCXX_NAMESPACE::Socket::isConnected ( ) const
inline

Get connected state.

Definition at line 274 of file Socket.hpp.

References m_impl.

◆ peerCertVerified()

bool BLOCXX_NAMESPACE::Socket::peerCertVerified ( ) const

did the peer certificate pass verification?

Returns
true if peer cert verified.

Definition at line 150 of file Socket_needs_SSL.cpp.

References m_impl.

◆ read()

int BLOCXX_NAMESPACE::Socket::read ( void * dataIn,
int dataInLen,
ErrorAction errorAsException = E_RETURN_ON_ERROR )
inlinevirtual

Read from the socket.

Parameters
dataIna pointer to a buffer where data should be copied to
dataInLenthe number of bytes to read.
errorAsExceptiontrue if errors should throw exceptions.
Returns
the number of bytes read.
Exceptions
SocketException

Implements BLOCXX_NAMESPACE::IOIFC.

Definition at line 191 of file Socket.hpp.

References BLOCXX_NAMESPACE::IOIFC::E_RETURN_ON_ERROR, and m_impl.

◆ receiveTimeOutExpired()

bool BLOCXX_NAMESPACE::Socket::receiveTimeOutExpired ( ) const
inline

Has the receive timeout expired?

Returns
true if the receive timeout has expired.

Definition at line 172 of file Socket.hpp.

References m_impl.

◆ setConnectTimeout() [1/2]

void BLOCXX_NAMESPACE::Socket::setConnectTimeout ( const Timeout & timeout)
inline

Set the connect timeout on the socket.

Parameters
secondsthe number of seconds for the connect timeout

Definition at line 152 of file Socket.hpp.

References m_impl.

◆ setConnectTimeout() [2/2]

BLOCXX_DEPRECATED void BLOCXX_NAMESPACE::Socket::setConnectTimeout ( int seconds)
inline

Set the connect timeout on the socket.

Parameters
secondsthe number of seconds for the connect timeout

Definition at line 147 of file Socket.hpp.

References BLOCXX_DEPRECATED, m_impl, and BLOCXX_NAMESPACE::Timeout::relative().

◆ setReceiveTimeout() [1/2]

void BLOCXX_NAMESPACE::Socket::setReceiveTimeout ( const Timeout & timeout)
inline

Set the receive timeout on the socket.

Parameters
secondsthe number of seconds for the receive timeout

Definition at line 122 of file Socket.hpp.

References m_impl.

◆ setReceiveTimeout() [2/2]

BLOCXX_DEPRECATED void BLOCXX_NAMESPACE::Socket::setReceiveTimeout ( int seconds)
inline

Set the receive timeout on the socket.

Parameters
secondsthe number of seconds for the receive timeout

Definition at line 117 of file Socket.hpp.

References BLOCXX_DEPRECATED, m_impl, and BLOCXX_NAMESPACE::Timeout::relative().

◆ setSendTimeout() [1/2]

void BLOCXX_NAMESPACE::Socket::setSendTimeout ( const Timeout & timeout)
inline

Set the send timeout on the socket.

Parameters
secondsthe number of seconds for the send timeout

Definition at line 137 of file Socket.hpp.

References m_impl.

◆ setSendTimeout() [2/2]

BLOCXX_DEPRECATED void BLOCXX_NAMESPACE::Socket::setSendTimeout ( int seconds)
inline

Set the send timeout on the socket.

Parameters
secondsthe number of seconds for the send timeout

Definition at line 132 of file Socket.hpp.

References BLOCXX_DEPRECATED, m_impl, and BLOCXX_NAMESPACE::Timeout::relative().

◆ setTimeouts() [1/2]

void BLOCXX_NAMESPACE::Socket::setTimeouts ( const Timeout & timeout)
inline

Set all timeouts (send, receive, connect)

Parameters
secondsthe number of seconds for the timeouts

Definition at line 167 of file Socket.hpp.

References m_impl.

◆ setTimeouts() [2/2]

BLOCXX_DEPRECATED void BLOCXX_NAMESPACE::Socket::setTimeouts ( int seconds)
inline

Set all timeouts (send, receive, connect)

Parameters
secondsthe number of seconds for the timeouts

Definition at line 162 of file Socket.hpp.

References BLOCXX_DEPRECATED, m_impl, and BLOCXX_NAMESPACE::Timeout::relative().

◆ shutdownAllSockets()

void BLOCXX_NAMESPACE::Socket::shutdownAllSockets ( )
static

Call this to shutdown all sockets.

This is usefull when a server is shutting down. We want any outstanding connections to close immediately.

Definition at line 71 of file Socket.cpp.

References BLOCXX_NAMESPACE::b_gotShutDown, BLOCXX_ASSERT, BLOCXX_THROW_ERRNO_MSG, s_shutDownMechanism, and BLOCXX_NAMESPACE::shutdownMutex.

◆ waitForInput() [1/2]

bool BLOCXX_NAMESPACE::Socket::waitForInput ( const Timeout & timeout = Timeout::infinite)
inline

Wait for input on the socket for a specified length of time.

Parameters
timeOutSecsthe number of seconds to wait.
Returns
true if the timeout expired
Exceptions
SocketException

Definition at line 209 of file Socket.hpp.

References BLOCXX_NAMESPACE::Timeout::infinite, and m_impl.

◆ waitForInput() [2/2]

BLOCXX_DEPRECATED bool BLOCXX_NAMESPACE::Socket::waitForInput ( int timeOutSecs)
inline

Wait for input on the socket for a specified length of time.

Parameters
timeOutSecsthe number of seconds to wait.
Returns
true if the timeout expired
Exceptions
SocketException

Definition at line 200 of file Socket.hpp.

References BLOCXX_DEPRECATED, m_impl, and BLOCXX_NAMESPACE::Timeout::relative().

◆ waitForOutput() [1/2]

bool BLOCXX_NAMESPACE::Socket::waitForOutput ( const Timeout & timeout = Timeout::infinite)
inline

Wait for output on the socket for a specified length of time.

Parameters
timeOutSecsthe number of seconds to wait.
Returns
true if the timeout expired
Exceptions
SocketException

Definition at line 227 of file Socket.hpp.

References BLOCXX_NAMESPACE::Timeout::infinite, and m_impl.

◆ waitForOutput() [2/2]

BLOCXX_DEPRECATED bool BLOCXX_NAMESPACE::Socket::waitForOutput ( int timeOutSecs)
inline

Wait for output on the socket for a specified length of time.

Parameters
timeOutSecsthe number of seconds to wait.
Returns
true if the timeout expired
Exceptions
SocketException

Definition at line 218 of file Socket.hpp.

References BLOCXX_DEPRECATED, m_impl, and BLOCXX_NAMESPACE::Timeout::relative().

◆ write()

int BLOCXX_NAMESPACE::Socket::write ( const void * dataOut,
int dataOutLen,
ErrorAction errorAsException = E_RETURN_ON_ERROR )
inlinevirtual

Write some data to the socket.

Parameters
dataOuta pointer to the memory to be written to the socket.
dataOutLenthe length of the data to be written
errorAsExceptiontrue if errors should throw exceptions.
Returns
the number of bytes written.
Exceptions
SocketException

Implements BLOCXX_NAMESPACE::IOIFC.

Definition at line 181 of file Socket.hpp.

References BLOCXX_NAMESPACE::IOIFC::E_RETURN_ON_ERROR, and m_impl.

Friends And Related Symbol Documentation

◆ ServerSocketImpl

friend class ServerSocketImpl
friend

Definition at line 347 of file Socket.hpp.

References ServerSocketImpl.

Referenced by ServerSocketImpl.

Member Data Documentation

◆ BLOCXX_DEPRECATED

const int INFINITE_TIMEOUT BLOCXX_NAMESPACE::Socket::BLOCXX_DEPRECATED = -1
static

◆ m_impl

◆ s_shutDownMechanism

LazyGlobal< Socket::ShutDownMechanism_t, int, Socket::ShutDownMechanismFactory > BLOCXX_NAMESPACE::Socket::s_shutDownMechanism = { 0, 0, {0, PTHREAD_MUTEX_INITIALIZER} }
staticprivate

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