diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-03-23 16:39:33 +0100 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-03-23 16:39:33 +0100 |
| commit | 61debe99a269bf7e87f6ba2f8d2a376e619fcf12 (patch) | |
| tree | 41cf942329760f8a5f16d98ea202124fcae8a0f3 /src/Xpl2Client.h | |
| parent | 46e96b3318900b561928be304f4a9e252f2785a4 (diff) | |
| download | QtXpl2-61debe99a269bf7e87f6ba2f8d2a376e619fcf12.tar.gz QtXpl2-61debe99a269bf7e87f6ba2f8d2a376e619fcf12.zip | |
Invert TCP connection model: client listens, server connects
The XPL2 protocol specifies that the JI2 controller initiates connections
to the client, not the other way around. Xpl2Client now listens on 3 ports
via QTcpServer and accepts inbound connections; MockServer connects out
with auto-retry on the 1s KA_PING tick.
QML API: startListening/stopListening, listening+connected properties,
host property removed. Mock server gains --host CLI arg.
Diffstat (limited to 'src/Xpl2Client.h')
| -rw-r--r-- | src/Xpl2Client.h | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/src/Xpl2Client.h b/src/Xpl2Client.h index 838c344..44edb65 100644 --- a/src/Xpl2Client.h +++ b/src/Xpl2Client.h @@ -11,17 +11,19 @@ #include <QHash> #include <QObject> #include <QQmlEngine> -#include <QTcpSocket> +#include <QTcpServer> #include <functional> +class QTcpSocket; + class Xpl2Client : public QObject { Q_OBJECT QML_ELEMENT QML_SINGLETON - Q_PROPERTY (QString host READ host WRITE setHost NOTIFY hostChanged) + Q_PROPERTY (bool listening READ isListening NOTIFY listeningChanged) Q_PROPERTY (bool connected READ isConnected NOTIFY connectedChanged) Q_PROPERTY (int controllerId READ controllerId NOTIFY jcVersionReceived) Q_PROPERTY ( @@ -33,9 +35,7 @@ class Xpl2Client : public QObject public: explicit Xpl2Client (QObject *parent = nullptr); - QString host () const; - void setHost (const QString &host); - + bool isListening () const; bool isConnected () const; int controllerId () const; @@ -45,8 +45,8 @@ public: static void enableWireDebug (); - Q_INVOKABLE void connectToServer (); - Q_INVOKABLE void disconnectFromServer (); + Q_INVOKABLE void startListening (); + Q_INVOKABLE void stopListening (); /** Get the just connected Jetting Controller ID and Software Version. */ Q_INVOKABLE void getJcVersion (); /** Query the specified printhead's version info. */ @@ -177,7 +177,7 @@ public: Q_INVOKABLE void imageCount (); signals: - void hostChanged (); + void listeningChanged (); void connectedChanged (); void errorOccurred (const QString &error); void statusMessage (const QString &message); @@ -286,18 +286,20 @@ signals: void imagingStopResult (bool success); private slots: - void onSocketConnected (); + void onNewConnection (); void onSocketDisconnected (); void onSocketMessageReady (); void onSocketError (QAbstractSocket::SocketError error); private: - void sendCommand (QTcpSocket &socket, const QByteArray &command, + void setupServer (QTcpServer &server, quint16 port); + QTcpSocket *&socketForServer (QTcpServer *server); + void sendCommand (QTcpSocket *socket, const QByteArray &command, const QVariantList ¶ms = {}); void dispatchCommandMessage (const Xpl2Protocol::ParsedMessage &msg); void dispatchImagingMessage (const Xpl2Protocol::ParsedMessage &msg); void dispatchStatusMessage (const Xpl2Protocol::ParsedMessage &msg); - void handleKaPing (QTcpSocket &socket); + void handleKaPing (QTcpSocket *socket); void handleGsJcVersion (const QVariantList ¶ms); void handleGsPhVersion (const QVariantList ¶ms); enum class ResponseShape @@ -335,13 +337,16 @@ private: void updateConnectedState (); QString logTag (const QTcpSocket *socket) const; - QTcpSocket m_commandSocket; - QTcpSocket m_imagingSocket; - QTcpSocket m_statusSocket; - QString m_host = QStringLiteral ("127.0.0.1"); + QTcpServer m_commandServer; + QTcpServer m_imagingServer; + QTcpServer m_statusServer; + QTcpSocket *m_commandSocket = nullptr; + QTcpSocket *m_imagingSocket = nullptr; + QTcpSocket *m_statusSocket = nullptr; quint16 m_commandPort = 9110; quint16 m_imagingPort = 9111; quint16 m_statusPort = 9112; + bool m_listening = false; bool m_connected = false; static bool s_wireDebug; int m_controllerId = 0; |
