This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 9ac5cf0e90c74983123a54b36e93c40a54499c13
Author: chao an <[email protected]>
AuthorDate: Tue Oct 27 14:08:07 2026 +0800

    drivers/net: Batch-copy telnet receive data
    
    Fast-copy normal receive data until the next IAC byte instead of
    running the Telnet state machine for each byte. This keeps option
    handling unchanged while reducing overhead for console streams.
    
    Signed-off-by: chao an <[email protected]>
---
 drivers/net/telnet.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/drivers/net/telnet.c b/drivers/net/telnet.c
index f0bcdd7825f..96750271741 100644
--- a/drivers/net/telnet.c
+++ b/drivers/net/telnet.c
@@ -28,6 +28,7 @@
 
 #include <assert.h>
 #include <stdio.h>
+#include <string.h>
 #include <fcntl.h>
 #include <poll.h>
 #include <errno.h>
@@ -338,14 +339,18 @@ static ssize_t telnet_receive(FAR struct telnet_dev_s 
*priv,
                               FAR const char *src, size_t srclen,
                               FAR char *dest, size_t destlen)
 {
+  FAR const char *iac;
+  size_t ncopy;
   int nread;
   uint8_t ch;
 
   ninfo("srclen: %zd destlen: %zd\n", srclen, destlen);
 
-  for (nread = 0; srclen > 0 && nread < destlen; srclen--)
+  nread = 0;
+  while (srclen > 0 && nread < destlen)
     {
       ch = *src++;
+      srclen--;
       ninfo("ch=%02x state=%d\n", ch, priv->td_state);
 
       switch (priv->td_state)
@@ -460,6 +465,26 @@ static ssize_t telnet_receive(FAR struct telnet_dev_s 
*priv,
             else
               {
                 telnet_getchar(priv, ch, dest, &nread);
+
+                ncopy = destlen - nread;
+                if (ncopy > srclen)
+                  {
+                    ncopy = srclen;
+                  }
+
+                iac = memchr(src, TELNET_IAC, ncopy);
+                if (iac != NULL)
+                  {
+                    ncopy = iac - src;
+                  }
+
+                if (ncopy > 0)
+                  {
+                    memcpy(&dest[nread], src, ncopy);
+                    nread += ncopy;
+                    src += ncopy;
+                    srclen -= ncopy;
+                  }
               }
             break;
 

Reply via email to