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

#include <TCPSocket.h>

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

Public Member Functions

 TCPSocket (SOCKET_ADDRESS_FAMILY domain=AF_INET, int32_t type=SOCK_STREAM, int32_t protocol=0)
 
 ~TCPSocket () override
 
void setProxy (std::shared_ptr< Proxy > proxy)
 
void close () noexcept override
 
virtual bool accept (SOCKET &clientSocketFD, struct sockaddr_in &clientInfo, std::chrono::milliseconds timeout)
 
size_t socketBytes () override
 
bool readyToRead (std::chrono::milliseconds timeout) override
 
size_t readLine (char *buffer, size_t size, char delimiter='\n')
 
size_t readLine (Buffer &buffer, char delimiter='\n')
 
size_t readLine (String &s, char delimiter='\n')
 
size_t read (uint8_t *buffer, size_t size, sockaddr_in *from=nullptr) override
 
size_t read (Buffer &buffer, size_t size, sockaddr_in *from=nullptr) override
 
size_t read (String &buffer, size_t size, sockaddr_in *from=nullptr) override
 
template<typename T >
size_t read (T &value, sockaddr_in *from=nullptr)
 
- Public Member Functions inherited from sptk::BaseSocket
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

TCPSocketReaderreader ()
 
void _open (const Host &host, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override
 
void _open (const struct sockaddr_in &address, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override
 
Proxyproxy ()
 
- Protected Member Functions inherited from sptk::BaseSocket
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)
 

Additional Inherited Members

- Public Types inherited from sptk::BaseSocket
enum class  OpenMode : uint8_t { CREATE , CONNECT , BIND }
 

Detailed Description

Generic TCP socket.

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

Constructor & Destructor Documentation

◆ TCPSocket()

sptk::TCPSocket::TCPSocket ( 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

◆ ~TCPSocket()

sptk::TCPSocket::~TCPSocket ( )
override

Destructor

Member Function Documentation

◆ _open() [1/2]

void sptk::TCPSocket::_open ( const Host host,
OpenMode  openMode,
bool  blockingMode,
std::chrono::milliseconds  timeout 
)
overrideprotectedvirtual

Opens the client socket connection by host and port

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

Reimplemented from sptk::BaseSocket.

◆ _open() [2/2]

void sptk::TCPSocket::_open ( const struct sockaddr_in &  address,
OpenMode  openMode,
bool  blockingMode,
std::chrono::milliseconds  timeout 
)
overrideprotectedvirtual

Opens the client socket connection by host and port

Parameters
addressAddress and port
openModeSocket open mode
blockingModeSocket blocking (true) on non-blocking (false) mode
timeoutConnection timeout. The default is 0 (wait forever)

Reimplemented from sptk::BaseSocket.

◆ accept()

virtual bool sptk::TCPSocket::accept ( SOCKET &  clientSocketFD,
struct sockaddr_in &  clientInfo,
std::chrono::milliseconds  timeout 
)
virtual

In server mode, waits for the incoming connection.

When incoming connection is made, exits returning the connection info

Parameters
clientSocketFDConnected client socket FD
clientInfoConnected client info
timeoutAccept operation timeout
Returns
True if accepted a connection

◆ close()

void sptk::TCPSocket::close ( )
overridevirtualnoexcept

Close socket connection

Reimplemented from sptk::BaseSocket.

Referenced by sptk::TCPServerConnection::terminate(), and sptk::WSProtocol::~WSProtocol().

◆ proxy()

Proxy * sptk::TCPSocket::proxy ( )
inlineprotected

Get proxy information

Returns

◆ read() [1/3]

size_t sptk::TCPSocket::read ( Buffer buffer,
size_t  size,
sockaddr_in *  from = nullptr 
)
overridevirtual

Reads data from the socket into memory buffer

Buffer bytes() is set to number of bytes read

Parameters
bufferThe memory buffer
sizeNumber of bytes to read from socket
fromAn optional structure for source address
Returns
the number of bytes read from the socket

Reimplemented from sptk::BaseSocket.

◆ read() [2/3]

size_t sptk::TCPSocket::read ( String buffer,
size_t  size,
sockaddr_in *  from = nullptr 
)
overridevirtual

Reads data from the socket into memory buffer

Buffer bytes() is set to number of bytes read

Parameters
bufferThe memory buffer
sizeNumber of bytes to read from socket
fromAn optional structure for source address
Returns
the number of bytes read from the socket

Reimplemented from sptk::BaseSocket.

◆ read() [3/3]

size_t sptk::TCPSocket::read ( uint8_t *  buffer,
size_t  size,
sockaddr_in *  from = nullptr 
)
overridevirtual

Reads data from the socket

Parameters
bufferThe memory buffer
sizeThe number of bytes to read
fromAn optional structure for source address
Returns
the number of bytes read from the socket

Reimplemented from sptk::BaseSocket.

◆ reader()

TCPSocketReader & sptk::TCPSocket::reader ( )
inlineprotected

Access to internal socket reader for derived classes

Returns
internal socket reader

◆ readLine() [1/3]

size_t sptk::TCPSocket::readLine ( Buffer buffer,
char  delimiter = '\n' 
)

Reads one line (terminated with CRLF) from the socket into existing memory buffer

The memory buffer is extended automatically to fit the string.

Parameters
bufferThe destination buffer
delimiterLine delimiter
Returns
the number of bytes read from the socket

◆ readLine() [2/3]

size_t sptk::TCPSocket::readLine ( char *  buffer,
size_t  size,
char  delimiter = '\n' 
)

Reads one line from the socket into existing memory buffer

The output string should fit the buffer or it will be returned incomplete.

Parameters
bufferThe destination buffer
sizeThe destination buffer size
delimiterLine delimiter
Returns
the number of bytes read from the socket

◆ readLine() [3/3]

size_t sptk::TCPSocket::readLine ( String s,
char  delimiter = '\n' 
)

Reads one line (terminated with CRLF) from the socket into string

Parameters
sThe destination string
delimiterLine delimiter
Returns
the number of bytes read from the socket

◆ readyToRead()

bool sptk::TCPSocket::readyToRead ( std::chrono::milliseconds  timeout)
overridevirtual

Reports true if socket is ready for reading from it

Parameters
timeoutRead timeout

Reimplemented from sptk::BaseSocket.

◆ setProxy()

void sptk::TCPSocket::setProxy ( std::shared_ptr< Proxy proxy)

Set proxy

Parameters
proxyProxy.

◆ socketBytes()

size_t sptk::TCPSocket::socketBytes ( )
overridevirtual

Returns number of bytes available in socket

Reimplemented from sptk::BaseSocket.


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

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