فهرست منبع

TCP receive read & printHex called improvements

Benjamin Harris 1 ماه پیش
والد
کامیت
ae58b9f476
1فایلهای تغییر یافته به همراه13 افزوده شده و 7 حذف شده
  1. 13 7
      ModulosDSP_101.ino

+ 13 - 7
ModulosDSP_101.ino

@@ -83,6 +83,7 @@ static uint8_t dataBuffer[50 * 1024];
 #define CMD_READ  0x0A
 
 #define TCP_IDLE_TIMEOUT_MS 30000  // drop session if silent for 30 s
+#define TCP_DEBUG 0                // set to 1 to log raw RX bytes to Serial
 
 constexpr int WRITE_HDR_LEN = 10;
 constexpr int READ_HDR_LEN  = 8;
@@ -827,16 +828,19 @@ static void handleTcpBridgeClient(WiFiClient& client)
     // the TCP receive window open. If we only drain when we feel like it,
     // the window goes to zero and SigmaStudio stops sending (ZeroWindow).
     // ------------------------------------------------------------------
-    while (client.available()) {
-      if (writeIndex >= (int)sizeof(dataBuffer)) {
+    int avail = client.available();
+    if (avail > 0) {
+      int space = (int)sizeof(dataBuffer) - writeIndex;
+      if (avail > space) {
         Serial.println("TCP RX overflow");
         ledErrorFlash(); client.stop(); return;
       }
-      int b = client.read();
-      if (b < 0) break;
-      dataBuffer[writeIndex++] = (uint8_t)b;
-      receivedByteCount++;
-      lastActivityMs = millis();
+      int got = client.read(&dataBuffer[writeIndex], avail);
+      if (got > 0) {
+        writeIndex += got;
+        receivedByteCount += got;
+        lastActivityMs = millis();
+      }
     }
 
     // ------------------------------------------------------------------
@@ -856,7 +860,9 @@ static void handleTcpBridgeClient(WiFiClient& client)
           ledOff();
           break; // nothing to process, go back to receive loop
         }
+#if TCP_DEBUG
         printHex("TCP RX: ", &dataBuffer[readIndex], (uint16_t)(receivedByteCount - readIndex));
+#endif
         uint8_t op = dataBuffer[readIndex];
         if      (op == CMD_WRITE) { currentState = STATE_WRITE_CMD; processedSomething = true; }
         else if (op == CMD_READ)  { currentState = STATE_READ_CMD;  processedSomething = true; }