From 61debe99a269bf7e87f6ba2f8d2a376e619fcf12 Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Mon, 23 Mar 2026 16:39:33 +0100 Subject: 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. --- src/Xpl2Client.h | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'src/Xpl2Client.h') 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 #include #include -#include +#include #include +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; -- cgit v1.2.3