simple-socket library 1.5.0
Loading...
Searching...
No Matches
NET::UnixStreamSocket Class Reference

Unix domain stream socket class. More...

#include <simple_socket/UnixStreamSocket.h>

Inheritance diagram for NET::UnixStreamSocket:
Collaboration diagram for NET::UnixStreamSocket:

Public Types

typedef SocketHandle< UnixStreamSocket > Handle
 Handle for a new socket returned by accept().
enum  ShutdownDirection { STOP_SEND = SHUT_WR , STOP_RECEIVE = SHUT_RD , STOP_BOTH = SHUT_RDWR }

Public Member Functions

 UnixStreamSocket ()
 UnixStreamSocket (Handle &&handle)
Handle accept () const
Handle timedAccept (int timeout) const
void listen (int backlog=5)
 Listen for incoming connections.
int sendAll (const void *buffer, size_t len)
 Send the complete buffer.
int receiveAll (void *buffer, size_t len)
 Receive the complete requested buffer.
void connect (std::string_view foreignPath)
void bind (std::string_view localPath)
std::string getLocalPath () const
std::string getForeignPath () const
int nativeHandle () noexcept
 return the native handle of the open socket
int send (const void *buffer, size_t len)
 send data through a connected socket
int receive (void *buffer, size_t len)
 receive data from a bound socket
int timedReceive (void *buffer, size_t len, int timeout)
 receive data from a bound socket, return after the given timespan
void disconnect ()
 Disconnect and unset any foreign addresses.
void shutdown (ShutdownDirection type)
 shutdown the connection in the specified direction
bool peerDisconnected () const noexcept
 returns whether a currently associated peer disconnected

Protected Attributes

std::string m_boundPath
int m_socket
bool m_peerDisconnected
bool m_connectionOriented

Detailed Description

Unix domain stream socket class.

Definition at line 10 of file UnixStreamSocket.h.

Member Typedef Documentation

◆ Handle

Handle for a new socket returned by accept().

Definition at line 14 of file UnixStreamSocket.h.

Member Enumeration Documentation

◆ ShutdownDirection

Enumerator
STOP_SEND 

disable all variants of send() on the socket, receive calls still work

STOP_RECEIVE 

disable all variants of receive() on the socket, send calls still work

STOP_BOTH 

disable all send() and receive() calls on the socket

Definition at line 57 of file SimpleSocket.h.

Constructor & Destructor Documentation

◆ UnixStreamSocket() [1/2]

UnixStreamSocket::UnixStreamSocket ( )

Construct a Unix domain stream socket.

Exceptions
SocketExceptionthrown if unable to create the socket

Definition at line 8 of file UnixStreamSocket.cpp.

◆ UnixStreamSocket() [2/2]

UnixStreamSocket::UnixStreamSocket ( Handle && handle)
explicit

Construct a socket from a Handle returned by accept(). Ownership of the descriptor is transferred from the handle. The accepted socket does not own the listening socket's pathname.

Exceptions
SocketExceptionthrown if handle is invalid

Definition at line 12 of file UnixStreamSocket.cpp.

Member Function Documentation

◆ accept()

UnixStreamSocket::Handle UnixStreamSocket::accept ( ) const

Wait for and accept an incoming connection.

Returns
handle owning the accepted socket
Exceptions
SocketExceptionif accept fails

Definition at line 16 of file UnixStreamSocket.cpp.

◆ bind()

void UnixSocket::bind ( std::string_view localPath)
inherited

Set the local path to the specified path

Parameters
localPathspecifies where the socket should be bound
Exceptions
SocketExceptionthrown if setting local path fails

Definition at line 33 of file UnixSocket.cpp.

◆ connect()

void UnixSocket::connect ( std::string_view foreignPath)
inherited

Establish a socket connection with the given socket

Parameters
foreignPaththe path of the remote socket
Exceptions
SocketExceptionthrown if unable to establish connection

Definition at line 23 of file UnixSocket.cpp.

◆ disconnect()

void SimpleSocket::disconnect ( )
inherited

Disconnect and unset any foreign addresses.

This will ungracefully abort the connection by sending an RST frame. disconnect() should not be used under normal circumstances, as it will terminate the connection OOB, preventing the other end of the communication from retrieving data that would still be available in the OS buffer.

For normal termination of a connection use shutdown().

Exceptions
SocketExceptionthrown if unable to disconnect

Definition at line 112 of file SimpleSocket.cpp.

◆ getForeignPath()

std::string UnixSocket::getForeignPath ( ) const
inherited

Get the foreign path. Call connect() before using this function.

Returns
foreign path
Exceptions
SocketExceptionthrown if fetch fails

Definition at line 53 of file UnixSocket.cpp.

◆ getLocalPath()

std::string UnixSocket::getLocalPath ( ) const
inherited

Get the local path (after binding the socket)

Returns
local path of socket
Exceptions
SocketExceptionthrown if fetch fails

Definition at line 43 of file UnixSocket.cpp.

◆ listen()

void SimpleSocket::listen ( int backlog = 5)

Listen for incoming connections.

Definition at line 211 of file SimpleSocket.cpp.

◆ nativeHandle()

int NET::SimpleSocket::nativeHandle ( )
inlinenoexceptinherited

return the native handle of the open socket

Definition at line 68 of file SimpleSocket.h.

◆ peerDisconnected()

bool SimpleSocket::peerDisconnected ( ) const
noexceptinherited

returns whether a currently associated peer disconnected

For stream sockets this is set when the peer closes or resets the connection. Use this function after a call to receive, returned 0 received bytes.

For connected datagram sockets it may also be set when the network reports that the configured peer is unreachable.

The flag is cleared by a successful connect().

Returns
true if a peer disconnected
Examples
TCP_Client.cpp.

Definition at line 132 of file SimpleSocket.cpp.

◆ receive()

int SimpleSocket::receive ( void * buffer,
size_t len )
inherited

receive data from a bound socket

receive() can only be used on a socket that called bind() or connect() before. If you try to use receive() on a not bound socket, SocketException will be thrown.

If using a stream oriented Socket, receive can return a part of a received messge, e.g. if you send 100 bytes, it's possible you will receive 50 bytes two times in a row. However, the order of the sent data will be preserved.

If you are using a datagram oriented sockets, you will only receive whole datagrams. But beware of using a too small buffer. If the receive buffer is too small for the received datagram, the data you didn't read in the receive call will be discared.

If the remote host has closed the connection (on a connection based socket like TCP or SCTP) receive() will return 0. If you are using a connectionless protocol (like UDP) there is no way to determine wheter the connection has been closed by the remote host or not.

Parameters
bufferthe buffer the received data will be written to
lenlength of the provided buffer, receive will not read more than that
Returns
number of bytes received
Exceptions
SocketExceptionin case an error occured

Definition at line 70 of file SimpleSocket.cpp.

◆ receiveAll()

int SimpleSocket::receiveAll ( void * buffer,
size_t len )

Receive the complete requested buffer.

Definition at line 248 of file SimpleSocket.cpp.

◆ send()

int SimpleSocket::send ( const void * buffer,
size_t len )
inherited

send data through a connected socket

send() can only be used on a socket that called connect() before. If you try to use send() on a not connected socket, SocketException will be thrown.

If you are using a stream oriented socket (like TCPSocket), the operating system is allowed to send only a part of the packet you told it to send, so send() will return the number of bytes actually sent. It is your responsibility to resend the data not sent by send()

If you are using a datagram oriented socket (like UDPSocket or SCTPSocket) the operating system will only send and receive complete datagrams, but send() will fail if you are trying to send too much data. In that case SocketException will be thrown.

Parameters
bufferdata to be send
lenlength of the data to be sent
Returns
number of bytes sent
Exceptions
SocketExceptionif sending went wrong

Definition at line 50 of file SimpleSocket.cpp.

◆ sendAll()

int SimpleSocket::sendAll ( const void * buffer,
size_t len )

Send the complete buffer.

Definition at line 231 of file SimpleSocket.cpp.

◆ shutdown()

void SimpleSocket::shutdown ( ShutdownDirection type)
inherited

shutdown the connection in the specified direction

This will gracefully end a connection by sending a FIN frame. Use this to end a normal communication cycle. To abort a connection see disconnect().

Depending on the specified ShutdownDirection, calls for that direction will stop working. Use this function if you want to have more control than just destroing the socket. It allows you to cut the connection in one direction, or both.

If you use shutdown() on an unconnected socket, the corresponding calls will simply stop working.

Parameters
typethe ShutdownDirection that be used
Exceptions
SocketExceptionin case an error occured
Examples
TCP_Server.cpp.

Definition at line 126 of file SimpleSocket.cpp.

◆ timedAccept()

UnixStreamSocket::Handle UnixStreamSocket::timedAccept ( int timeout) const

Wait up to timeout milliseconds for an incoming connection.

Returns
valid handle on success, invalid handle on timeout
Exceptions
SocketExceptionon errors

Definition at line 22 of file UnixStreamSocket.cpp.

◆ timedReceive()

int SimpleSocket::timedReceive ( void * buffer,
size_t len,
int timeout )
inherited

receive data from a bound socket, return after the given timespan

timedReceive() can only be used on a socket that called bind() or connect before. If you try to use receive() on a not bound socket, SocketException will be thrown.

If using a stream oriented Socket, receive can return a part of a received messge, e.g. if you send 100 bytes, it's possible you will receive 50 bytes two times in a row. However, the order of the sent data will be preserved.

If you are using a datagram oriented sockets, you will only receive whole datagrams. But beware of using a too small buffer. If the receive buffer is too small for the received datagram, the data you didn't read in the receive call will be discared.

If the remote host has closed the connection (on a connection based socket like TCP or SCTP) receive() will return 0. If you are using a connectionless protocol (like UDP) there is no way to determine wheter the connection has been closed by the remote host or not.

Parameters
bufferthe buffer the received data will be written to
lenlength of the provided buffer, receive will not read more than that
timeoutthe timeout in ms after which receive will give up and return
Returns
number of bytes received, 0 on timeout
Exceptions
SocketExceptionin case an error occured
Examples
TCP_Client.cpp.

Definition at line 89 of file SimpleSocket.cpp.

Member Data Documentation

◆ m_boundPath

std::string NET::UnixSocket::m_boundPath
protectedinherited

Definition at line 53 of file UnixSocket.h.

◆ m_connectionOriented

bool NET::SimpleSocket::m_connectionOriented
protectedinherited

Definition at line 268 of file SimpleSocket.h.

◆ m_peerDisconnected

bool NET::SimpleSocket::m_peerDisconnected
protectedinherited

Definition at line 267 of file SimpleSocket.h.

◆ m_socket

int NET::SimpleSocket::m_socket
protectedinherited

Definition at line 266 of file SimpleSocket.h.


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