Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Revision:
90:955f4c6e18a9
Parent:
89:9b765a67699b
Child:
93:580fc113d9e9
--- a/tcp/tcpsend.c	Wed Nov 21 17:13:04 2018 +0000
+++ b/tcp/tcpsend.c	Thu Nov 22 17:35:59 2018 +0000
@@ -1,5 +1,6 @@
 #include <stdint.h>
 #include <stdbool.h>
+#include <stdarg.h>
 
 #include    "log.h"
 #include    "net.h"
@@ -17,6 +18,21 @@
 #define MAX_RETRANSMISSIONS   10
 #define TIMEOUT_BROKEN_LINK  600
 
+static void log(uint16_t remPort, char* fmt, ...)
+{
+    if (TcpTrace)
+    {
+        if (NetTraceNewLine) Log("\r\n");
+        LogTimeF("TCP port %hu - ", remPort);
+        va_list argptr;
+        va_start(argptr, fmt);
+        LogV(fmt, argptr);
+        va_end(argptr);
+        Log("\r\n");
+    }
+}
+
+
 static bool doTrace(uint16_t port)
 {
     switch (port)
@@ -208,7 +224,7 @@
     //Check and make available the remote AR index
     if (pTcb->remArIndex < 0)
     {
-        if (TcpTrace) LogTimeF("TCP - Reaping TCB port %hu - missing remote AR index\r\n", pTcb->remPort);
+        log(pTcb->remPort, "missing remote AR index -> reaping TCB");
         pTcb->state = TCB_EMPTY;
         return DO_NOTHING;
     }
@@ -220,7 +236,7 @@
     //Reap old ones
     if (TcbElapsed >= pTcb->timeLastRcvd + TIMEOUT_BROKEN_LINK)
     {
-        if (TcpTrace) LogTimeF("TCP - Reaping TCB port %hu - broken link\r\n", pTcb->remPort);
+        log(pTcb->remPort, "broken link -> reaping TCB");
         pTcb->state = TCB_EMPTY;
         return DO_NOTHING;
     }
@@ -238,24 +254,21 @@
         pTcb->countSendsNotAcked++;
         if (pTcb->countSendsNotAcked > MAX_RETRANSMISSIONS)
         {
-            if (TcpTrace)
-            {
-                if (NetTraceNewLine) Log("\r\n");
-                LogTimeF("TCP - Resending seq %lu on port %hu reached maximum retransmissions\r\n", pTcb->bytesAckdByRem, pTcb->remPort);
-                pTcb->state = TCB_EMPTY;
-                return TcpSendReset(pSize, pPacket, pTcb);
-            }
+            log(pTcb->remPort, "reached maximum retransmissions -> sending reset");
+            pTcb->state = TCB_EMPTY;
+            return TcpSendReset(pSize, pPacket, pTcb);
         }
-        if (TcpTrace)
+        else
         {
-            if (NetTraceNewLine) Log("\r\n");
-            LogTimeF("TCP - Resending seq %lu on port %hu waiting for ack of %lu bytes\r\n", pTcb->bytesAckdByRem, pTcb->remPort, pTcb->bytesSentToRem);
+            log(pTcb->remPort, "only had ack of %lu while sent %lu -> resending", pTcb->bytesAckdByRem, pTcb->bytesSentToRem);
+            pTcb->timeSendsBeingAcked = TcbElapsed;
+            return TcpResendLastUnAcked(pSize, pPacket, pTcb);
         }
-        pTcb->timeSendsBeingAcked = TcbElapsed;
-        return TcpResendLastUnAcked(pSize, pPacket, pTcb);
     }
-    
-    //If haven't had to do anything else then do a normal send
-    return TcpSend(pSize, pPacket, pTcb);
+    else
+    {
+        //If haven't had to do anything else then do a normal send
+        return TcpSend(pSize, pPacket, pTcb);
+    }
 }