NDEVR
API Documentation
SocketConnection.h
1#pragma once
2#include "DLLInfo.h"
3#include <NDEVR/Connection.h>
4#include <NDEVR/TranslatedString.h>
5#include <QHostAddress>
6class QAbstractSocket;
7class QTcpSocket;
8class QUdpSocket;
9namespace NDEVR
10{
16 class NDEVR_NETWORK_PROTOCOLS_API SocketConnection : public ConnectionBackend
17 {
18 public:
30 virtual uint04 peek(char*, uint04 size) const final override;
36 virtual uint04 rx(char*, uint04 size = Constant<uint04>::Max) final override;
41 virtual void tx(const StringView& command) final override;
46 void txInThread(const StringView& data);
52 virtual bool open(const ConnectionInfo& request) final override;
56 virtual void close() final override;
61 virtual bool isOpen() const final override;
66 virtual uint04 bytesAvailable() const override;
71 virtual TranslatedString lastError() const final override;
78 virtual bool setTCPPort(QTcpSocket* tcp_port, ConnectionOpenType connect_mode);
85 virtual bool setUDPPort(QUdpSocket* udp_port, ConnectionOpenType connect_mode);
91 virtual bool waitForReadyRead(const TimeSpan& span) final override;
98 virtual bool waitForConnection(QAbstractSocket* socket, const ConnectionInfo& request);
103 void setSocket(QAbstractSocket* socket);
109 bool openUDP(const ConnectionInfo& request);
115 bool openUDPServer(const ConnectionInfo& request);
121 bool openTCP(const ConnectionInfo& request);
122 protected slots:
127 void handleSocketErrorSlot(int error);
128 protected:
129 QAbstractSocket* m_socket = nullptr;
131 QHostAddress m_host_address;
133 };
134
140 {
141 public:
147 virtual bool handlesType(const StringView& request) const override;
153 virtual ConnectionBackend* createConnection(const ConnectionInfo& request) override;
165 };
166}
Defines cross-platform DLL export/import macros for the NDEVRNetworkProtocols module.
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
Provides the unseen backend logic for setting up, tearing down, and sending data over a specific type...
Definition Connection.h:169
ConnectionBackend(LogPtr log)
Constructs a ConnectionBackend with the given log.
Stores factories that can be registered for creating Connections of various types by instantiating a ...
Definition Connection.h:250
A light-weight wrapper that will be a no-op if there is not a valid log reference,...
A ConnectionFactory that enables the creation of a SocketConnection for interfacing with a QAbstractS...
virtual Buffer< ConnectionInfo > availableConnections(const ConnectionSearchParameters &params) override
Enumerates available network connections matching the given search parameters.
virtual ConnectionBackend * createConnection(const ConnectionInfo &request) override
Creates a new SocketConnection backend for the given connection info.
virtual bool handlesType(const StringView &request) const override
Checks whether this factory can create connections for the given type string.
virtual Buffer< ConnectionCategory > availableConnectionCategories() override
Returns the categories of connections this factory can produce.
void handleSocketErrorSlot(int error)
Slot that handles socket error signals and stores the error description.
bool openTCP(const ConnectionInfo &request)
Opens a TCP connection 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 bool waitForConnection(QAbstractSocket *socket, const ConnectionInfo &request)
Blocks until the socket establishes a connection or the request times out.
void setSocket(QAbstractSocket *socket)
Replaces the internal socket with the given QAbstractSocket.
QAbstractSocket * m_socket
The underlying Qt socket (TCP or UDP).
bool openUDP(const ConnectionInfo &request)
Opens a UDP client connection using the given connection info.
virtual bool open(const ConnectionInfo &request) final override
Opens a socket connection using the given connection info (TCP or UDP).
virtual bool isOpen() const final override
Checks whether the socket connection is currently open.
bool openUDPServer(const ConnectionInfo &request)
Opens a UDP server (broadcast listener) using the given connection info.
virtual void close() final override
Closes the socket connection and releases associated resources.
virtual bool setUDPPort(QUdpSocket *udp_port, ConnectionOpenType connect_mode)
Assigns an existing QUdpSocket to this connection.
virtual bool setTCPPort(QTcpSocket *tcp_port, ConnectionOpenType connect_mode)
Assigns an existing QTcpSocket to this connection.
virtual void tx(const StringView &command) final override
Transmits the given command string over the socket.
virtual TranslatedString lastError() const final override
Returns a translated description of the last socket error encountered.
virtual uint04 bytesAvailable() const override
Returns the number of bytes available for reading from the socket.
virtual uint04 rx(char *, uint04 size=Constant< uint04 >::Max) final override
Reads (receives) data from the socket.
uint02 m_broadcast_port
The UDP broadcast port, or Invalid if not broadcasting.
virtual bool waitForReadyRead(const TimeSpan &span) final override
Blocks until data is available to read or the given timeout elapses.
void txInThread(const StringView &data)
Transmits data over the socket from a worker thread context.
QHostAddress m_host_address
The resolved host address for the connection.
SocketConnection(LogPtr log)
Constructs a SocketConnection with the given log for diagnostics.
TranslatedString m_last_error
The last error message from the socket.
The core String View class for the NDEVR API.
Definition StringView.h:58
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
The primary namespace for the NDEVR SDK.
uint16_t uint02
-Defines an alias representing a 2 byte, unsigned integer -Can represent exact integer values 0 throu...
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
ConnectionOpenType
Describes how to open a connection.
Definition Connection.h:64
A structure designed to store information about a specific Connection.
Definition Connection.h:90
Describes settings for performing a Connection search.
Definition Connection.h:149
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...