NDEVR
API Documentation
SocketConnection

A core ConnectionBackend for QAbstractSocket supporting TCP and UDP connections. More...

Inheritance diagram for SocketConnection:
[legend]
Collaboration diagram for SocketConnection:
[legend]

Public Member Functions

 SocketConnection (LogPtr log)
 Constructs a SocketConnection with the given log for diagnostics.
virtual uint04 bytesAvailable () const override
 Returns the number of bytes available for reading from the socket.
virtual void close () final override
 Closes the socket connection and releases associated resources.
virtual bool isOpen () const final override
 Checks whether the socket connection is currently open.
virtual TranslatedString lastError () const final override
 Returns a translated description of the last socket error encountered.
virtual bool open (const ConnectionInfo &request) final override
 Opens a socket connection using the given connection info (TCP or UDP).
bool openTCP (const ConnectionInfo &request)
 Opens a TCP connection using the given connection info.
bool openUDP (const ConnectionInfo &request)
 Opens a UDP client connection using the given connection info.
bool openUDPServer (const ConnectionInfo &request)
 Opens a UDP server (broadcast listener) using the given connection info.
virtual uint04 peek (char *, uint04 size) const final override
 Peeks at incoming data without consuming it from the socket buffer.
virtual uint04 rx (char *, uint04 size=Constant< uint04 >::Max) final override
 Reads (receives) data from the socket.
void setSocket (QAbstractSocket *socket)
 Replaces the internal socket with the given QAbstractSocket.
virtual bool setTCPPort (QTcpSocket *tcp_port, ConnectionOpenType connect_mode)
 Assigns an existing QTcpSocket to this connection.
virtual bool setUDPPort (QUdpSocket *udp_port, ConnectionOpenType connect_mode)
 Assigns an existing QUdpSocket to this connection.
virtual void tx (const StringView &command) final override
 Transmits the given command string over the socket.
void txInThread (const StringView &data)
 Transmits data over the socket from a worker thread context.
virtual bool waitForConnection (QAbstractSocket *socket, const ConnectionInfo &request)
 Blocks until the socket establishes a connection or the request times out.
virtual bool waitForReadyRead (const TimeSpan &span) final override
 Blocks until data is available to read or the given timeout elapses.
Public Member Functions inherited from ConnectionBackend
 ConnectionBackend (LogPtr log)
 Constructs a ConnectionBackend with the given log.
void connectionStateChangedSignal (ConnectionState state)
 Emitted when the connection state changes.
void dataAvailableSignal ()
 Emitted when data is available to be read from the connection.
virtual String threadID () const
 Returns the thread ID this backend must execute on, if any.

Protected Member Functions

void handleSocketErrorSlot (int error)
 Slot that handles socket error signals and stores the error description.

Protected Attributes

uint02 m_broadcast_port = Constant<uint02>::Invalid
 The UDP broadcast port, or Invalid if not broadcasting.
QHostAddress m_host_address
 The resolved host address for the connection.
TranslatedString m_last_error
 The last error message from the socket.
QAbstractSocket * m_socket = nullptr
 The underlying Qt socket (TCP or UDP).
Protected Attributes inherited from ConnectionBackend
LogPtr m_log
 Log for diagnostic messages.

Detailed Description

A core ConnectionBackend for QAbstractSocket supporting TCP and UDP connections.


See also
Connection, WebSocketConnection, SerialPortConnection

Definition at line 16 of file SocketConnection.h.

Constructor & Destructor Documentation

◆ SocketConnection()

SocketConnection::SocketConnection ( LogPtr log)

Constructs a SocketConnection with the given log for diagnostics.

Parameters
[in]logThe log pipe for recording connection events and errors.

References SocketConnection().

Referenced by SocketConnection().

Member Function Documentation

◆ bytesAvailable()

virtual uint04 SocketConnection::bytesAvailable ( ) const
overridevirtual

Returns the number of bytes available for reading from the socket.

Returns
The number of bytes available.

Implements ConnectionBackend.

References bytesAvailable().

Referenced by bytesAvailable().

◆ handleSocketErrorSlot()

void SocketConnection::handleSocketErrorSlot ( int error)
protected

Slot that handles socket error signals and stores the error description.

Parameters
[in]errorThe socket error code.

References handleSocketErrorSlot().

Referenced by handleSocketErrorSlot().

◆ isOpen()

virtual bool SocketConnection::isOpen ( ) const
finaloverridevirtual

Checks whether the socket connection is currently open.

Returns
True if the connection is open, false otherwise.

Implements ConnectionBackend.

References isOpen().

Referenced by isOpen().

◆ lastError()

virtual TranslatedString SocketConnection::lastError ( ) const
finaloverridevirtual

Returns a translated description of the last socket error encountered.

Returns
The last error as a TranslatedString.

Implements ConnectionBackend.

References lastError().

Referenced by lastError().

◆ open()

virtual bool SocketConnection::open ( const ConnectionInfo & request)
finaloverridevirtual

Opens a socket connection using the given connection info (TCP or UDP).

Parameters
[in]requestThe connection parameters specifying address, port, and protocol.
Returns
True if the connection was opened successfully, false otherwise.

Implements ConnectionBackend.

◆ openTCP()

bool SocketConnection::openTCP ( const ConnectionInfo & request)

Opens a TCP connection using the given connection info.

Parameters
[in]requestThe connection parameters.
Returns
True if the TCP connection was opened successfully, false otherwise.

References openTCP().

Referenced by openTCP().

◆ openUDP()

bool SocketConnection::openUDP ( const ConnectionInfo & request)

Opens a UDP client connection using the given connection info.

Parameters
[in]requestThe connection parameters.
Returns
True if the UDP connection was opened successfully, false otherwise.

References openUDP().

Referenced by openUDP().

◆ openUDPServer()

bool SocketConnection::openUDPServer ( const ConnectionInfo & request)

Opens a UDP server (broadcast listener) using the given connection info.

Parameters
[in]requestThe connection parameters.
Returns
True if the UDP server was opened successfully, false otherwise.

References openUDPServer().

Referenced by openUDPServer().

◆ peek()

virtual uint04 SocketConnection::peek ( char * ,
uint04 size ) const
finaloverridevirtual

Peeks at incoming data without consuming it from the socket buffer.

Parameters
[in]sizeThe maximum number of bytes to peek.
Returns
The number of bytes actually peeked.

Implements ConnectionBackend.

◆ rx()

virtual uint04 SocketConnection::rx ( char * ,
uint04 size = Constantuint04 >::Max )
finaloverridevirtual

Reads (receives) data from the socket.

Parameters
[in]sizeThe maximum number of bytes to read.
Returns
The number of bytes actually read.

Implements ConnectionBackend.

◆ setSocket()

void SocketConnection::setSocket ( QAbstractSocket * socket)

Replaces the internal socket with the given QAbstractSocket.

Parameters
[in]socketThe new socket to use for this connection.

References setSocket().

Referenced by setSocket().

◆ setTCPPort()

virtual bool SocketConnection::setTCPPort ( QTcpSocket * tcp_port,
ConnectionOpenType connect_mode )
virtual

Assigns an existing QTcpSocket to this connection.

Parameters
[in]tcp_portThe TCP socket to use.
[in]connect_modeThe open mode (read, write, or both).
Returns
True if the socket was set successfully, false otherwise.

References setTCPPort().

Referenced by setTCPPort().

◆ setUDPPort()

virtual bool SocketConnection::setUDPPort ( QUdpSocket * udp_port,
ConnectionOpenType connect_mode )
virtual

Assigns an existing QUdpSocket to this connection.

Parameters
[in]udp_portThe UDP socket to use.
[in]connect_modeThe open mode (read, write, or both).
Returns
True if the socket was set successfully, false otherwise.

References setUDPPort().

Referenced by setUDPPort().

◆ tx()

virtual void SocketConnection::tx ( const StringView & command)
finaloverridevirtual

Transmits the given command string over the socket.

Parameters
[in]commandThe data to send.

Implements ConnectionBackend.

◆ txInThread()

void SocketConnection::txInThread ( const StringView & data)

Transmits data over the socket from a worker thread context.

Parameters
[in]dataThe data to send.

◆ waitForConnection()

virtual bool SocketConnection::waitForConnection ( QAbstractSocket * socket,
const ConnectionInfo & request )
virtual

Blocks until the socket establishes a connection or the request times out.

Parameters
[in]socketThe abstract socket to wait on.
[in]requestThe connection info containing timeout parameters.
Returns
True if the connection was established, false on failure or timeout.

References waitForConnection().

Referenced by waitForConnection().

◆ waitForReadyRead()

virtual bool SocketConnection::waitForReadyRead ( const TimeSpan & span)
finaloverridevirtual

Blocks until data is available to read or the given timeout elapses.

Parameters
[in]spanThe maximum time to wait.
Returns
True if data became available, false on timeout.

Implements ConnectionBackend.

References waitForReadyRead().

Referenced by waitForReadyRead().


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