|
6 | 6 |
|
7 | 7 | #include <QMetaEnum> |
8 | 8 |
|
| 9 | +#include "settingnames.h" |
| 10 | + |
9 | 11 | OutgoingTcpThread::OutgoingTcpThread(QSslSocket* socket, const Packet& packetToSend, QObject* parent) |
10 | 12 | :BaseTcpThread(socket, parent), sendPacket(packetToSend) |
11 | 13 | { |
@@ -113,14 +115,18 @@ Packet OutgoingTcpThread::buildReplyPacket(const Packet& receivedPacket, |
113 | 115 |
|
114 | 116 | reply.fromPort = getSocket() ? getSocket()->localPort() : 0; |
115 | 117 |
|
116 | | - reply.tcpOrUdp = sendPacket.tcpOrUdp; |
| 118 | + reply.tcpOrUdp = receivedPacket.tcpOrUdp; |
117 | 119 | if (isSocketEncrypted()) { |
118 | 120 | reply.tcpOrUdp = "SSL"; |
119 | 121 | } |
120 | 122 |
|
121 | 123 | // Response content |
122 | 124 | if (!responseData.isEmpty()) { |
123 | 125 | reply.hexString = Packet::byteArrayToHex(responseData); |
| 126 | + } else |
| 127 | + { |
| 128 | + const QSettings& settings = getSettings(); |
| 129 | + reply.hexString = settings.value(RESPONSE_HEX).toString(); |
124 | 130 | } |
125 | 131 |
|
126 | 132 | // Macro expansion |
@@ -270,15 +276,57 @@ void OutgoingTcpThread::waitForAndProcessIncomingData() |
270 | 276 | // Optionally: auto-send a response here if configured |
271 | 277 | } |
272 | 278 |
|
273 | | -void OutgoingTcpThread::persistentConnectionLoop() |
| 279 | +bool OutgoingTcpThread::shouldSendReply() const |
274 | 280 | { |
275 | | - QDEBUG() << "Entering persistent connection loop for" << sendPacket.toIP << ":" << sendPacket.port; |
| 281 | + const QSettings &settings = getSettings(); |
276 | 282 |
|
277 | | - if (shouldStopPersistentConnectionLoop()) { |
278 | | - qDebug() << "Early exit from persistent loop due to close request"; |
| 283 | + bool basicResponseEnabled = settings.value(SEND_RESPONSE, false).toBool(); |
| 284 | + bool smartResponseEnabled = settings.value(SMART_RESPONSES_ENABLED, false).toBool(); |
| 285 | + bool hasCommandLineReply = !commandLineReplyPacket.hexString.isEmpty(); |
| 286 | + |
| 287 | + if (consoleMode) |
| 288 | + { |
| 289 | + return hasCommandLineReply; |
| 290 | + } |
| 291 | + |
| 292 | + // If either basic response or smart responses are enabled |
| 293 | + return basicResponseEnabled || smartResponseEnabled || hasCommandLineReply; |
| 294 | +} |
| 295 | + |
| 296 | +void OutgoingTcpThread::sendReplyIfNeeded(const Packet& receivedPacket) |
| 297 | +{ |
| 298 | + if (!shouldSendReply()) { |
| 299 | + QDEBUG() << "No reply configured - skipping"; |
279 | 300 | return; |
280 | 301 | } |
281 | 302 |
|
| 303 | + QDEBUG() << "shouldSendReply() == true → building reply"; |
| 304 | + |
| 305 | + // Smart response data will go here in the future |
| 306 | + QByteArray responseData; |
| 307 | + |
| 308 | + Packet reply = buildReplyPacket(receivedPacket, responseData); |
| 309 | + |
| 310 | + // Command-line reply has highest priority |
| 311 | + if (!commandLineReplyPacket.hexString.isEmpty()) { |
| 312 | + reply = commandLineReplyPacket; |
| 313 | + QDEBUG() << "Using command-line reply packet instead"; |
| 314 | + } |
| 315 | + |
| 316 | + // Don't send empty replies (this was in the old writeResponse) |
| 317 | + if (reply.hexString.isEmpty()) { |
| 318 | + QDEBUG() << "Reply has no data - skipping send"; |
| 319 | + return; |
| 320 | + } |
| 321 | + |
| 322 | + // === Actual send via Base class === |
| 323 | + BaseTcpThread::sendOutgoingPacket(reply); |
| 324 | +} |
| 325 | + |
| 326 | +void OutgoingTcpThread::persistentConnectionLoop() |
| 327 | +{ |
| 328 | + QDEBUG() << "Entering persistent connection loop for" << sendPacket.toIP << ":" << sendPacket.port; |
| 329 | + |
282 | 330 | while (shouldContinuePersistentLoop()) { |
283 | 331 | insidePersistent = true; // keeping for now, we can remove later if unused |
284 | 332 |
|
|
0 commit comments