You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2021/07/22 06:43:22 UTC

[incubator-nuttx] 02/02: local/stream: remove preamble header in stream mode

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

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

commit a48513ad65bed560ec0fc9141a541eabe3216417
Author: chao.an <an...@xiaomi.com>
AuthorDate: Wed Jul 21 15:41:49 2021 +0800

    local/stream: remove preamble header in stream mode
    
    Preable sync header is no necessary in stream mode
    
    Signed-off-by: chao.an <an...@xiaomi.com>
---
 net/local/local.h            | 14 +++++-------
 net/local/local_recvmsg.c    | 41 +++++-----------------------------
 net/local/local_recvutils.c  | 15 +++++++++----
 net/local/local_sendmsg.c    |  4 ++--
 net/local/local_sendpacket.c | 52 +++++++++++++++++++++++---------------------
 5 files changed, 51 insertions(+), 75 deletions(-)

diff --git a/net/local/local.h b/net/local/local.h
index 9451b70..38a52d4 100644
--- a/net/local/local.h
+++ b/net/local/local.h
@@ -178,13 +178,6 @@ struct local_conn_s
     {
       volatile int lc_result;  /* Result of the connection operation (client) */
     } client;
-
-    /* Fields common to connected peers (connected or accepted) */
-
-    struct
-    {
-      uint16_t lc_remaining;   /* Bytes remaining in the incoming stream */
-    } peer;
   } u;
 #endif /* CONFIG_NET_LOCAL_STREAM */
 };
@@ -401,6 +394,7 @@ ssize_t local_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
  *   filep    File structure of write-only FIFO.
  *   buf      Data to send
  *   len      Length of data to send
+ *   preamble Flag to indicate the preamble sync header assembly
  *
  * Returned Value:
  *   Zero is returned on success; a negated errno value is returned on any
@@ -409,7 +403,7 @@ ssize_t local_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
  ****************************************************************************/
 
 int local_send_packet(FAR struct file *filep, FAR const struct iovec *buf,
-                      size_t len);
+                      size_t len, bool preamble);
 
 /****************************************************************************
  * Name: local_recvmsg
@@ -450,6 +444,7 @@ ssize_t local_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
  *   buf   - Local to store the received data
  *   len   - Length of data to receive [in]
  *           Length of data actually received [out]
+ *   once  - Flag to indicate the buf may only be read once
  *
  * Returned Value:
  *   Zero is returned on success; a negated errno value is returned on any
@@ -459,7 +454,8 @@ ssize_t local_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
  *
  ****************************************************************************/
 
-int local_fifo_read(FAR struct file *filep, FAR uint8_t *buf, size_t *len);
+int local_fifo_read(FAR struct file *filep, FAR uint8_t *buf,
+                    size_t *len, bool once);
 
 /****************************************************************************
  * Name: local_getaddr
diff --git a/net/local/local_recvmsg.c b/net/local/local_recvmsg.c
index e5e48bc..ccfa03b 100644
--- a/net/local/local_recvmsg.c
+++ b/net/local/local_recvmsg.c
@@ -60,12 +60,12 @@
  ****************************************************************************/
 
 static int psock_fifo_read(FAR struct socket *psock, FAR void *buf,
-                           FAR size_t *readlen)
+                           FAR size_t *readlen, bool once)
 {
   FAR struct local_conn_s *conn = (FAR struct local_conn_s *)psock->s_conn;
   int ret;
 
-  ret = local_fifo_read(&conn->lc_infile, buf, readlen);
+  ret = local_fifo_read(&conn->lc_infile, buf, readlen, once);
   if (ret < 0)
     {
       /* -ECONNRESET is a special case.  We may or not have received
@@ -134,7 +134,7 @@ psock_stream_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
                       FAR socklen_t *fromlen)
 {
   FAR struct local_conn_s *conn = (FAR struct local_conn_s *)psock->s_conn;
-  size_t readlen;
+  size_t readlen = len;
   int ret;
 
   /* Verify that this is a connected peer socket */
@@ -149,43 +149,14 @@ psock_stream_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
 
   DEBUGASSERT(conn->lc_infile.f_inode != NULL);
 
-  /* Are there still bytes in the FIFO from the last packet? */
-
-  if (conn->u.peer.lc_remaining == 0)
-    {
-      /* No.. Sync to the start of the next packet in the stream and get
-       * the size of the next packet.
-       */
-
-      ret = local_sync(&conn->lc_infile);
-      if (ret < 0)
-        {
-          nerr("ERROR: Failed to get packet length: %d\n", ret);
-          return ret;
-        }
-      else if (ret > UINT16_MAX)
-        {
-          nerr("ERROR: Packet is too big: %d\n", ret);
-          return -E2BIG;
-        }
-
-      conn->u.peer.lc_remaining = (uint16_t)ret;
-    }
-
   /* Read the packet */
 
-  readlen = MIN(conn->u.peer.lc_remaining, len);
-  ret     = psock_fifo_read(psock, buf, &readlen);
+  ret = psock_fifo_read(psock, buf, &readlen, true);
   if (ret < 0)
     {
       return ret;
     }
 
-  /* Adjust the number of bytes remaining to be read from the packet */
-
-  DEBUGASSERT(readlen <= conn->u.peer.lc_remaining);
-  conn->u.peer.lc_remaining -= readlen;
-
   /* Return the address family */
 
   if (from)
@@ -296,7 +267,7 @@ psock_dgram_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
   /* Read the packet */
 
   readlen = MIN(pktlen, len);
-  ret     = psock_fifo_read(psock, buf, &readlen);
+  ret     = psock_fifo_read(psock, buf, &readlen, false);
   if (ret < 0)
     {
       goto errout_with_infd;
@@ -319,7 +290,7 @@ psock_dgram_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
           /* Read 32 bytes into the bit bucket */
 
           readlen = MIN(remaining, 32);
-          ret     = psock_fifo_read(psock, bitbucket, &tmplen);
+          ret     = psock_fifo_read(psock, bitbucket, &tmplen, false);
           if (ret < 0)
             {
               goto errout_with_infd;
diff --git a/net/local/local_recvutils.c b/net/local/local_recvutils.c
index 18341f1..de5170c 100644
--- a/net/local/local_recvutils.c
+++ b/net/local/local_recvutils.c
@@ -53,6 +53,7 @@
  *   buf   - Local to store the received data
  *   len   - Length of data to receive [in]
  *           Length of data actually received [out]
+ *   once  - Flag to indicate the buf may only be read once
  *
  * Returned Value:
  *   Zero is returned on success; a negated errno value is returned on any
@@ -62,7 +63,8 @@
  *
  ****************************************************************************/
 
-int local_fifo_read(FAR struct file *filep, FAR uint8_t *buf, size_t *len)
+int local_fifo_read(FAR struct file *filep, FAR uint8_t *buf,
+                    size_t *len, bool once)
 {
   ssize_t remaining;
   ssize_t nread;
@@ -99,6 +101,11 @@ int local_fifo_read(FAR struct file *filep, FAR uint8_t *buf, size_t *len)
           DEBUGASSERT(nread <= remaining);
           remaining -= nread;
           buf       += nread;
+
+          if (once)
+            {
+              break;
+            }
         }
     }
 
@@ -142,7 +149,7 @@ int local_sync(FAR struct file *filep)
       do
         {
           readlen = sizeof(uint8_t);
-          ret     = local_fifo_read(filep, &sync, &readlen);
+          ret     = local_fifo_read(filep, &sync, &readlen, false);
           if (ret < 0)
             {
               nerr("ERROR: Failed to read sync bytes: %d\n", ret);
@@ -156,7 +163,7 @@ int local_sync(FAR struct file *filep)
       do
         {
           readlen = sizeof(uint8_t);
-          ret     = local_fifo_read(filep, &sync, &readlen);
+          ret     = local_fifo_read(filep, &sync, &readlen, false);
           if (ret < 0)
             {
               nerr("ERROR: Failed to read sync bytes: %d\n", ret);
@@ -170,7 +177,7 @@ int local_sync(FAR struct file *filep)
   /* Then read the packet length */
 
   readlen = sizeof(uint16_t);
-  ret     = local_fifo_read(filep, (FAR uint8_t *)&pktlen, &readlen);
+  ret     = local_fifo_read(filep, (FAR uint8_t *)&pktlen, &readlen, false);
   return ret < 0 ? ret : pktlen;
 }
 
diff --git a/net/local/local_sendmsg.c b/net/local/local_sendmsg.c
index 7b71941..934abc4 100644
--- a/net/local/local_sendmsg.c
+++ b/net/local/local_sendmsg.c
@@ -91,7 +91,7 @@ static ssize_t local_send(FAR struct socket *psock,
 
           /* Send the packet */
 
-          ret = local_send_packet(&peer->lc_outfile, buf, len);
+          ret = local_send_packet(&peer->lc_outfile, buf, len, false);
         }
         break;
 #endif /* CONFIG_NET_LOCAL_STREAM */
@@ -231,7 +231,7 @@ static ssize_t local_sendto(FAR struct socket *psock,
 
   /* Send the packet */
 
-  ret = local_send_packet(&conn->lc_outfile, buf, len);
+  ret = local_send_packet(&conn->lc_outfile, buf, len, true);
   if (ret < 0)
     {
       nerr("ERROR: Failed to send the packet: %zd\n", ret);
diff --git a/net/local/local_sendpacket.c b/net/local/local_sendpacket.c
index 8dd2f14..96253b4 100644
--- a/net/local/local_sendpacket.c
+++ b/net/local/local_sendpacket.c
@@ -118,6 +118,7 @@ static int local_fifo_write(FAR struct file *filep, FAR const uint8_t *buf,
  *   filep    File structure of write-only FIFO.
  *   buf      Data to send
  *   len      Length of data to send
+ *   preamble Flag to indicate the preamble sync header assembly
  *
  * Returned Value:
  *   Packet length is returned on success; a negated errno value is returned
@@ -126,47 +127,48 @@ static int local_fifo_write(FAR struct file *filep, FAR const uint8_t *buf,
  ****************************************************************************/
 
 int local_send_packet(FAR struct file *filep, FAR const struct iovec *buf,
-                      size_t len)
+                      size_t len, bool preamble)
 {
+  FAR const struct iovec *end = buf + len;
   FAR const struct iovec *iov;
-  FAR const struct iovec *end;
+  int ret = -EINVAL;
   uint16_t len16;
-  int ret;
 
-  /* Send the packet preamble */
-
-  ret = local_fifo_write(filep, g_preamble, LOCAL_PREAMBLE_SIZE);
-  if (ret == OK)
+  if (preamble)
     {
-      /* Send the packet length */
-
-      end = buf + len;
-      for (len16 = 0, iov = buf; iov != end; iov++)
-        {
-          len16 += iov->iov_len;
-        }
+      /* Send the packet preamble */
 
-      ret = local_fifo_write(filep, (FAR const uint8_t *)&len16,
-                             sizeof(uint16_t));
+      ret = local_fifo_write(filep, g_preamble, LOCAL_PREAMBLE_SIZE);
       if (ret == OK)
         {
-          /* Send the packet data */
+          /* Send the packet length */
 
           for (len16 = 0, iov = buf; iov != end; iov++)
             {
-              ret = local_fifo_write(filep, iov->iov_base, iov->iov_len);
-              if (ret < 0)
-                break;
-              else
-                len16 += iov->iov_len;
+              len16 += iov->iov_len;
+            }
+
+          ret = local_fifo_write(filep, (FAR const uint8_t *)&len16,
+                                 sizeof(uint16_t));
+          if (ret != OK)
+            {
+              return ret;
             }
+        }
+    }
 
-          if (ret == OK)
-            ret = len16;
+  for (len16 = 0, iov = buf; iov != end; iov++)
+    {
+      ret = local_fifo_write(filep, iov->iov_base, iov->iov_len);
+      if (ret < 0)
+        {
+          break;
         }
+
+      len16 += iov->iov_len;
     }
 
-  return ret;
+  return (ret == OK) ? len16 : ret;
 }
 
 #endif /* CONFIG_NET && CONFIG_NET_LOCAL */