NDEVR
API Documentation
TcpServer.h
1#include "DLLInfo.h"
2#include <NDEVR/Buffer.h>
3#include <NDEVR/String.h>
4#include <QObject>
5
6class QTcpSocket;
7class QTcpServer;
8namespace NDEVR
9{
10 class String;
11 class File;
12 class Exception;
22 class NDEVR_NETWORK_PROTOCOLS_API TcpServer : public QObject
23 {
24 public:
25 Q_OBJECT
26 public:
33 explicit TcpServer(const String& server_name, uint04 port, QObject* parent = 0);
39 explicit TcpServer(const String& server_name, QObject* parent = 0);
44 void connectToRemoteServer(const String& remote_address);
49 void connectToRemoteServer(QTcpSocket* remote_socket);
50 protected:
59 virtual bool dataReceived(const String& data_type, const String& args, const String& data, QTcpSocket* socket);
68 virtual bool fileReceived(const String& data_type, const String& args, const File& data, QTcpSocket* socket);
69 public:
77 static QByteArray CreateMessagePacket(const StringView& type, const StringView& data, const StringView& args = StringView());
84 static QByteArray CreateFilePacket(const File& file, bool send_file_contents);
93 static QByteArray CreateOpenAppPacket(const File& application, const String& args = String(), bool wait_for_completion = true, bool windowless = false);
99 static QByteArray CreateOpenFilePacket(const File& file_to_open);
106 static QByteArray CreateRequestSendFilePacket(const File& file_to_request, const String& args = String());
112 static QByteArray CreateSendErrorPacket(const Exception& e);
113 public:
120 virtual bool sendData(const StringView& data, QTcpSocket* socket = nullptr);
127 virtual bool sendFile(const File& file, QTcpSocket* socket = nullptr);
135 virtual bool sendFile(const File& file, bool send_file_contents, QTcpSocket* socket = nullptr);
141 virtual bool sendShowFileBrowser(QTcpSocket* socket = nullptr);
148 virtual bool sendSetScreenResolution(const Vector<2, uint04>& size, QTcpSocket* socket = nullptr);
158 virtual bool sendOpenApp(const File& application, const String& args, bool wait_for_completion, bool windowless, QTcpSocket* socket = nullptr);
166 virtual bool sendData(const StringView& data_type, const StringView& data, QTcpSocket* socket = nullptr);
173 virtual bool sendError(const Exception& e, QTcpSocket* socket = nullptr);
180 virtual bool sendPacket(const QByteArray& packet, QTcpSocket* socket = nullptr);
181 protected:
186 virtual void socketDisconnected(QTcpSocket* socket);
191 virtual void showError(const Exception& e);
192
193 public slots:
210 void displayErrorSlot(int socketError);
211 public:
212 static constexpr StringView generic_data_message_type = "data";
213 static constexpr StringView error_data_message = "send_error_message";
214 static constexpr StringView open_os_settings = "open_os_settings";
215 static constexpr StringView set_screen_resolution = "set_screen_resolution";
216 static constexpr StringView generic_file_message_type = "file";
217 static constexpr StringView start_application_file_message_type = "start_application";
218 static constexpr StringView open_file_message_type = "open_file";
219 static constexpr StringView request_send_file = "request_send_file";
220 private:
221 QTcpServer* server;
222 String m_server_name;
223 Buffer<QTcpSocket*> m_sockets;
224 };
225}
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 consistent interface to handle errors through the throw expression.
Definition Exception.hpp:47
Logic for reading or writing to a file as well as navigating filesystems or other common file operati...
Definition File.h:53
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
virtual void showError(const Exception &e)
Displays or logs an error from an exception; override for custom error handling.
void readSocketSlot()
Slot invoked when data is available to read from a connected socket.
void discardSocketSlot()
Slot invoked when a socket is disconnected and should be discarded.
virtual bool sendFile(const File &file, QTcpSocket *socket=nullptr)
Sends a file (metadata and contents) to the specified socket or all connected sockets.
static constexpr StringView error_data_message
Message type for error messages.
Definition TcpServer.h:213
static constexpr StringView open_file_message_type
Message type to open a file remotely.
Definition TcpServer.h:218
void connectToRemoteServer(QTcpSocket *remote_socket)
Connects to a remote TcpServer using an existing QTcpSocket.
void newConnectionSlot()
Slot invoked when a new incoming TCP connection is established.
virtual bool sendData(const StringView &data, QTcpSocket *socket=nullptr)
Sends a generic data string to the specified socket or all connected sockets.
static constexpr StringView start_application_file_message_type
Message type to start an application remotely.
Definition TcpServer.h:217
virtual bool dataReceived(const String &data_type, const String &args, const String &data, QTcpSocket *socket)
Called when a data message is received from a connected socket.
static QByteArray CreateOpenFilePacket(const File &file_to_open)
Creates a packet instructing the remote end to open a file in its default handler.
virtual bool sendError(const Exception &e, QTcpSocket *socket=nullptr)
Sends an error message to the specified socket or all connected sockets.
static constexpr StringView open_os_settings
Message type to open OS settings on the remote end.
Definition TcpServer.h:214
static constexpr StringView set_screen_resolution
Message type to set screen resolution remotely.
Definition TcpServer.h:215
virtual bool sendFile(const File &file, bool send_file_contents, QTcpSocket *socket=nullptr)
Sends a file with control over whether contents are included.
virtual bool fileReceived(const String &data_type, const String &args, const File &data, QTcpSocket *socket)
Called when a file message is received from a connected socket.
virtual bool sendSetScreenResolution(const Vector< 2, uint04 > &size, QTcpSocket *socket=nullptr)
Sends a command to set the screen resolution on the remote end.
static QByteArray CreateFilePacket(const File &file, bool send_file_contents)
Creates a file transfer packet.
static QByteArray CreateSendErrorPacket(const Exception &e)
Creates a packet containing an error message to send to the remote end.
void connectToRemoteServer(const String &remote_address)
Connects to a remote TcpServer at the given address string.
static QByteArray CreateMessagePacket(const StringView &type, const StringView &data, const StringView &args=StringView())
Creates a message packet with the given type, data, and optional arguments.
virtual void socketDisconnected(QTcpSocket *socket)
Called when a socket has been disconnected; override to handle cleanup.
virtual bool sendPacket(const QByteArray &packet, QTcpSocket *socket=nullptr)
Sends a raw pre-built packet to the specified socket or all connected sockets.
virtual bool sendOpenApp(const File &application, const String &args, bool wait_for_completion, bool windowless, QTcpSocket *socket=nullptr)
Sends a command to launch an application on the remote end.
virtual bool sendShowFileBrowser(QTcpSocket *socket=nullptr)
Sends a command to show a file browser on the remote end.
virtual bool sendData(const StringView &data_type, const StringView &data, QTcpSocket *socket=nullptr)
Sends a typed data message to the specified socket or all connected sockets.
static constexpr StringView generic_file_message_type
Message type for generic file transfers.
Definition TcpServer.h:216
TcpServer(const String &server_name, QObject *parent=0)
Constructs a TcpServer without binding to a port (client-only or deferred listen).
static QByteArray CreateOpenAppPacket(const File &application, const String &args=String(), bool wait_for_completion=true, bool windowless=false)
Creates a packet instructing the remote end to launch an application.
static constexpr StringView generic_data_message_type
Message type for generic data payloads.
Definition TcpServer.h:212
TcpServer(const String &server_name, uint04 port, QObject *parent=0)
Constructs a TcpServer that listens on the specified port.
void displayErrorSlot(int socketError)
Slot invoked when a socket error occurs.
static QByteArray CreateRequestSendFilePacket(const File &file_to_request, const String &args=String())
Creates a packet requesting the remote end to send a specific file back.
static constexpr StringView request_send_file
Message type to request a file from the remote end.
Definition TcpServer.h:219
A fixed-size array with N dimensions used as the basis for geometric and mathematical types.
Definition Vector.hpp:62
The primary namespace for the NDEVR SDK.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...