sptk2 logo
SPTK Home Page
Public Types | Public Member Functions | Protected Member Functions | List of all members
sptk::BaseSocket Class Reference

#include <BaseSocket.h>

Inheritance diagram for sptk::BaseSocket:
sptk::TCPSocket sptk::UDPSocket sptk::ImapConnect sptk::SSLSocket sptk::SmtpConnect

Public Types

enum class  OpenMode : uint8_t { CREATE , CONNECT , BIND }
 

Public Member Functions

SOCKET fd () const
 
void open_addr (OpenMode openMode=OpenMode::CREATE, const sockaddr_in *addr=nullptr, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
 
 BaseSocket (SOCKET_ADDRESS_FAMILY domain=AF_INET, int32_t type=SOCK_STREAM, int32_t protocol=0)
 
 BaseSocket (const BaseSocket &other)=delete
 
 BaseSocket (BaseSocket &&other) noexcept=default
 
virtual ~BaseSocket ()
 Destructor.
 
BaseSocketoperator= (const BaseSocket &other)=delete
 
BaseSocketoperator= (BaseSocket &&other) noexcept=default
 
void blockingMode (bool blocking)
 
virtual size_t socketBytes ()
 
virtual void attach (SOCKET socketHandle, bool accept)
 
virtual SOCKET detach ()
 
void host (const Host &host)
 
const Hosthost () const
 
void open (const Host &host=Host(), OpenMode openMode=OpenMode::CONNECT, bool blockingMode=true, std::chrono::milliseconds timeoutMS=std::chrono::milliseconds(0))
 
void open (const struct sockaddr_in &address, OpenMode openMode=OpenMode::CONNECT, bool blockingMode=true, std::chrono::milliseconds timeoutMS=std::chrono::milliseconds(0))
 
void bind (const char *address, uint32_t portNumber)
 
void listen (uint16_t portNumber=0)
 
virtual void close () noexcept
 
bool active () const
 
int32_t control (int flag, const uint32_t *check) const
 
void setOption (int level, int option, int value) const
 
void getOption (int level, int option, int &value) const
 
virtual size_t recv (uint8_t *buffer, size_t len)
 
virtual size_t send (const uint8_t *buffer, size_t len)
 
virtual size_t read (uint8_t *buffer, size_t size, sockaddr_in *from)
 
virtual size_t read (uint8_t *buffer, size_t size)
 
virtual size_t read (Buffer &buffer, size_t size, sockaddr_in *from)
 
size_t read (Buffer &buffer, size_t size)
 
virtual size_t read (String &buffer, size_t size, sockaddr_in *from)
 
size_t read (String &buffer, size_t size)
 
virtual size_t write (const uint8_t *buffer, size_t size, const sockaddr_in *peer)
 
size_t write (const uint8_t *buffer, size_t size)
 
virtual size_t write (const Buffer &buffer, const sockaddr_in *peer)
 
size_t write (const Buffer &buffer)
 
virtual size_t write (const String &buffer, const sockaddr_in *peer)
 
size_t write (const String &buffer)
 
virtual bool readyToRead (std::chrono::milliseconds timeout)
 
virtual bool readyToWrite (std::chrono::milliseconds timeout)
 
bool blockingMode () const
 Return current blocking mode state. More...
 

Protected Member Functions

void setSocketFD (SOCKET socket)
 
int32_t domain () const
 
int32_t type () const
 
int32_t protocol () const
 
virtual void _open (const Host &host, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeoutMS)
 
virtual void _open (const struct sockaddr_in &address, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeoutMS)
 

Detailed Description

Generic socket.

Allows establishing a network connection to the host by name and port address

Member Enumeration Documentation

◆ OpenMode

enum class sptk::BaseSocket::OpenMode : uint8_t
strong

A mode to open a socket, one of

Enumerator
CREATE 

Only create (Typical UDP connectionless socket)

CONNECT 

Connect (Typical TCP connection socket)

BIND 

Bind (TCP listener)

Constructor & Destructor Documentation

◆ BaseSocket() [1/3]

sptk::BaseSocket::BaseSocket ( SOCKET_ADDRESS_FAMILY  domain = AF_INET,
int32_t  type = SOCK_STREAM,
int32_t  protocol = 0 
)
explicit

Constructor

Parameters
domainSocket domain type
typeSocket type
protocolProtocol type

◆ BaseSocket() [2/3]

sptk::BaseSocket::BaseSocket ( const BaseSocket other)
delete

Deleted copy constructor

Parameters
otherOther socket

◆ BaseSocket() [3/3]

sptk::BaseSocket::BaseSocket ( BaseSocket &&  other)
defaultnoexcept

Move constructor

Parameters
otherOther socket

Member Function Documentation

◆ _open() [1/2]

virtual void sptk::BaseSocket::_open ( const Host host,
OpenMode  openMode,
bool  blockingMode,
std::chrono::milliseconds  timeoutMS 
)
protectedvirtual

Opens the client socket connection by host and port

Parameters
hostThe host
openModeSocket open mode
blockingModeSocket blocking (true) on non-blocking (false) mode
timeoutMSConnection timeout. The default is 0 (wait forever)

Reimplemented in sptk::SSLSocket, and sptk::TCPSocket.

◆ _open() [2/2]

virtual void sptk::BaseSocket::_open ( const struct sockaddr_in &  address,
OpenMode  openMode,
bool  blockingMode,
std::chrono::milliseconds  timeoutMS 
)
inlineprotectedvirtual

Opens the client socket connection by host and port

Parameters
addressAddress and port
openModeSocket open mode
blockingModeSocket blocking (true) on non-blocking (false) mode
timeoutMSConnection timeout, std::chrono::milliseconds. The default is 0 (wait forever)

Reimplemented in sptk::SSLSocket, and sptk::TCPSocket.

◆ active()

bool sptk::BaseSocket::active ( ) const
inline

Returns the current socket state

Returns
true if socket is opened

◆ attach()

virtual void sptk::BaseSocket::attach ( SOCKET  socketHandle,
bool  accept 
)
virtual

Attaches socket handle

Parameters
socketHandleExisting socket handle

Reimplemented in sptk::SSLSocket.

Referenced by sptk::TCPServerConnection::TCPServerConnection().

◆ bind()

void sptk::BaseSocket::bind ( const char *  address,
uint32_t  portNumber 
)

Binds the socket to port

Parameters
addressLocal IP address, or NULL if any
portNumberThe port number, or 0 if any

◆ blockingMode() [1/2]

bool sptk::BaseSocket::blockingMode ( ) const
inline

Return current blocking mode state.

Returns
Current blocking mode state

◆ blockingMode() [2/2]

void sptk::BaseSocket::blockingMode ( bool  blocking)

Set blocking mode

Parameters
blockingSocket blocking mode flag

◆ close()

virtual void sptk::BaseSocket::close ( )
virtualnoexcept

Closes the socket connection

Reimplemented in sptk::SSLSocket, and sptk::TCPSocket.

◆ control()

int32_t sptk::BaseSocket::control ( int  flag,
const uint32_t *  check 
) const

Calls Unix fcntl() or Windows ioctlsocket()

◆ detach()

virtual SOCKET sptk::BaseSocket::detach ( )
virtual

Detaches socket handle, setting it to INVALID_SOCKET. Closes the socket without affecting socket handle.

Returns
Existing socket handle

◆ domain()

int32_t sptk::BaseSocket::domain ( ) const
inlineprotected

Get socket domain type

◆ fd()

SOCKET sptk::BaseSocket::fd ( ) const
inline

Get socket internal (OS) handle

◆ getOption()

void sptk::BaseSocket::getOption ( int  level,
int  option,
int &  value 
) const

Gets socket option value

Throws an error if not succeeded

◆ host() [1/2]

const Host & sptk::BaseSocket::host ( ) const
inline

Returns the host

◆ host() [2/2]

void sptk::BaseSocket::host ( const Host host)

Sets the host name

Parameters
hostThe host

Referenced by sptk::TCPServerListener::port().

◆ listen()

void sptk::BaseSocket::listen ( uint16_t  portNumber = 0)

Opens the server socket connection on port (binds/listens)

Parameters
portNumberThe port number

Referenced by sptk::TCPServerListener::listen().

◆ open() [1/2]

void sptk::BaseSocket::open ( const Host host = Host(),
OpenMode  openMode = OpenMode::CONNECT,
bool  blockingMode = true,
std::chrono::milliseconds  timeoutMS = std::chrono::milliseconds(0) 
)
inline

Opens the client socket connection by host and port

Parameters
hostThe host
openModeSocket open mode
blockingModeSocket blocking (true) on non-blocking (false) mode
timeoutMSConnection timeout. The default is 0 (wait forever)

◆ open() [2/2]

void sptk::BaseSocket::open ( const struct sockaddr_in &  address,
OpenMode  openMode = OpenMode::CONNECT,
bool  blockingMode = true,
std::chrono::milliseconds  timeoutMS = std::chrono::milliseconds(0) 
)
inline

Opens the client socket connection by host and port

Parameters
addressAddress and port
openModeSocket open mode
blockingModeSocket blocking (true) on non-blocking (false) mode
timeoutMSConnection timeout, std::chrono::milliseconds. The default is 0 (wait forever)

◆ open_addr()

void sptk::BaseSocket::open_addr ( OpenMode  openMode = OpenMode::CREATE,
const sockaddr_in *  addr = nullptr,
std::chrono::milliseconds  timeout = std::chrono::milliseconds(0) 
)

Opens the socket connection by address.

Parameters
openModeSOM_CREATE for UDP socket, SOM_BIND for the server socket, and SOM_CONNECT for the client socket
addrDefines socket address/port information
timeoutConnection timeout. If 0 then wait forever.

◆ operator=() [1/2]

BaseSocket & sptk::BaseSocket::operator= ( BaseSocket &&  other)
defaultnoexcept

Move assignment

Parameters
otherOther socket

◆ operator=() [2/2]

BaseSocket & sptk::BaseSocket::operator= ( const BaseSocket other)
delete

Deleted copy assignment

Parameters
otherOther socket

◆ protocol()

int32_t sptk::BaseSocket::protocol ( ) const
inlineprotected

Get socket protocol

◆ read() [1/6]

size_t sptk::BaseSocket::read ( Buffer buffer,
size_t  size 
)
inline

Reads data from the socket into memory buffer

Buffer bytes() is set to number of bytes read

Parameters
bufferThe output buffer
sizeThe number of bytes to read
Returns
the number of bytes read from the socket

◆ read() [2/6]

virtual size_t sptk::BaseSocket::read ( Buffer buffer,
size_t  size,
sockaddr_in *  from 
)
virtual

Reads data from the socket into memory buffer

Buffer bytes() is set to number of bytes read

Parameters
bufferThe output buffer
sizeThe number of bytes to read
fromThe source address
Returns
the number of bytes read from the socket

Reimplemented in sptk::TCPSocket, and sptk::UDPSocket.

◆ read() [3/6]

size_t sptk::BaseSocket::read ( String buffer,
size_t  size 
)
inline

Reads data from the socket into memory buffer

Buffer bytes() is set to number of bytes read

Parameters
bufferThe memory buffer
sizeThe number of bytes to read
Returns
the number of bytes read from the socket

◆ read() [4/6]

virtual size_t sptk::BaseSocket::read ( String buffer,
size_t  size,
sockaddr_in *  from 
)
virtual

Reads data from the socket into memory buffer

Buffer bytes() is set to number of bytes read

Parameters
bufferThe memory buffer
sizeThe number of bytes to read
fromThe source address
Returns
the number of bytes read from the socket

Reimplemented in sptk::TCPSocket, and sptk::UDPSocket.

◆ read() [5/6]

virtual size_t sptk::BaseSocket::read ( uint8_t *  buffer,
size_t  size 
)
inlinevirtual

Reads data from the socket

Parameters
bufferThe memory buffer
sizeThe number of bytes to read
Returns
the number of bytes read from the socket

◆ read() [6/6]

virtual size_t sptk::BaseSocket::read ( uint8_t *  buffer,
size_t  size,
sockaddr_in *  from 
)
virtual

Reads data from the socket

Parameters
bufferThe memory buffer
sizeThe number of bytes to read
fromThe source address
Returns
the number of bytes read from the socket

Reimplemented in sptk::TCPSocket, and sptk::UDPSocket.

◆ readyToRead()

virtual bool sptk::BaseSocket::readyToRead ( std::chrono::milliseconds  timeout)
virtual

Reports true if socket is ready for reading from it

Parameters
timeoutRead timeout

Reimplemented in sptk::TCPSocket.

◆ readyToWrite()

virtual bool sptk::BaseSocket::readyToWrite ( std::chrono::milliseconds  timeout)
virtual

Reports true if socket is ready for writing to it

Parameters
timeoutWrite timeout

◆ recv()

virtual size_t sptk::BaseSocket::recv ( uint8_t *  buffer,
size_t  len 
)
virtual

Reads data from the socket in regular or SSL mode

Parameters
bufferThe destination buffer
lenThe destination buffer size
Returns
the number of bytes read from the socket

Reimplemented in sptk::SSLSocket.

◆ send()

virtual size_t sptk::BaseSocket::send ( const uint8_t *  buffer,
size_t  len 
)
virtual

Reads data from the socket in regular or TLS mode

Parameters
bufferThe send buffer
lenThe send data length
Returns
the number of bytes sent the socket

Reimplemented in sptk::SSLSocket.

◆ setOption()

void sptk::BaseSocket::setOption ( int  level,
int  option,
int  value 
) const

Sets socket option value Throws an error if not succeeded

◆ setSocketFD()

void sptk::BaseSocket::setSocketFD ( SOCKET  socket)
inlineprotected

Set socket internal (OS) handle

◆ socketBytes()

virtual size_t sptk::BaseSocket::socketBytes ( )
virtual

Returns number of bytes available in socket

Reimplemented in sptk::SSLSocket, and sptk::TCPSocket.

◆ type()

int32_t sptk::BaseSocket::type ( ) const
inlineprotected

Get socket type

◆ write() [1/6]

size_t sptk::BaseSocket::write ( const Buffer buffer)
inline

Writes data to the socket

Parameters
bufferThe memory buffer
Returns
the number of bytes written to the socket

◆ write() [2/6]

virtual size_t sptk::BaseSocket::write ( const Buffer buffer,
const sockaddr_in *  peer 
)
virtual

Writes data to the socket

Parameters
bufferThe memory buffer
peerThe peer information
Returns
the number of bytes written to the socket

◆ write() [3/6]

size_t sptk::BaseSocket::write ( const String buffer)
inline

Writes data to the socket

Parameters
bufferThe memory buffer
Returns
the number of bytes written to the socket

◆ write() [4/6]

virtual size_t sptk::BaseSocket::write ( const String buffer,
const sockaddr_in *  peer 
)
virtual

Writes data to the socket

Parameters
bufferThe memory buffer
peerThe peer information
Returns
the number of bytes written to the socket

◆ write() [5/6]

size_t sptk::BaseSocket::write ( const uint8_t *  buffer,
size_t  size 
)
inline

Writes data to the socket

If size is omited then buffer is treated as zero-terminated string

Parameters
bufferThe memory buffer
sizeThe memory buffer size
Returns
the number of bytes written to the socket

◆ write() [6/6]

virtual size_t sptk::BaseSocket::write ( const uint8_t *  buffer,
size_t  size,
const sockaddr_in *  peer 
)
virtual

Writes data to the socket

If size is omited then buffer is treated as zero-terminated string

Parameters
bufferThe memory buffer
sizeThe memory buffer size
peerThe peer information
Returns
the number of bytes written to the socket

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

Fri Oct 14 2022 09:58:32: SPTK 5.4.1