Skip to content

Commit

Permalink
local/stream: remove preamble header in stream mode
Browse files Browse the repository at this point in the history
Preable sync header is no necessary in stream mode

Signed-off-by: chao.an <anchao@xiaomi.com>
  • Loading branch information
anchao committed Jul 21, 2021
1 parent dcd79ba commit 28e2464
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 75 deletions.
14 changes: 5 additions & 9 deletions net/local/local.h
Expand Up @@ -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 */
};
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
41 changes: 6 additions & 35 deletions net/local/local_recvmsg.c
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
15 changes: 11 additions & 4 deletions net/local/local_recvutils.c
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions net/local/local_sendmsg.c
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down
52 changes: 27 additions & 25 deletions net/local/local_sendpacket.c
Expand Up @@ -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
Expand All @@ -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 */

0 comments on commit 28e2464

Please sign in to comment.