Minor improvement in recvfrom()

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3673 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2011-06-05 23:29:38 +00:00
parent 6fa6c78928
commit 13067a31d6

View File

@ -861,7 +861,7 @@ static ssize_t tcp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
{ {
struct recvfrom_s state; struct recvfrom_s state;
uip_lock_t save; uip_lock_t save;
int ret = OK; int ret;
/* Initialize the state structure. This is done with interrupts /* Initialize the state structure. This is done with interrupts
* disabled because we don't want anything to happen until we * disabled because we don't want anything to happen until we
@ -878,9 +878,24 @@ static ssize_t tcp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
#if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0 #if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0
recvfrom_readahead(&state); recvfrom_readahead(&state);
/* The default return value is the number of bytes that we just copied into
* the user buffer. We will return this if the socket has become disconnected
* or if the user request was completely satisfied with data from the readahead
* buffers.
*/
ret = state.rf_recvlen;
#else
/* Otherwise, the default return value of zero is used (only for the case
* where len == state.rf_buflen is zero).
*/
ret = 0;
#endif #endif
/* Verify that the SOCK_STREAM has been or still is connected */ /* Verify that the SOCK_STREAM has been and still is connected */
if (!_SS_ISCONNECTED(psock->s_flags)) if (!_SS_ISCONNECTED(psock->s_flags))
{ {
@ -889,29 +904,17 @@ static ssize_t tcp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
*/ */
#if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0 #if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0
if (state.rf_recvlen <= 0) if (ret <= 0)
#endif
{ {
/* Nothing was received. The SOCK_STREAM must be re-connected in /* Nothing was previously received from the readahead buffers.
* order to receive an additional data. * The SOCK_STREAM must be (re-)connected in order to receive any
* additional data.
*/ */
ret = -ENOTCONN; ret = -ENOTCONN;
} }
else
{
/* The socket is disconnected, but there is data in the read-ahead
* buffer. The return value is the number of bytes read from the
* read-ahead buffer */
ret = state.rf_recvlen;
}
#else
/* The SOCK_STREAM must be connected inorder to receive data. */
ret = -ENOTCONN;
#endif
} }
else
/* In general, this uIP-based implementation will not support non-blocking /* In general, this uIP-based implementation will not support non-blocking
* socket operations... except in a few cases: Here for TCP receive with read-ahead * socket operations... except in a few cases: Here for TCP receive with read-ahead
@ -919,31 +922,30 @@ static ssize_t tcp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
* if no data was obtained from the read-ahead buffers. * if no data was obtained from the read-ahead buffers.
*/ */
else
#if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0 #if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0
if (_SS_ISNONBLOCK(psock->s_flags)) if (_SS_ISNONBLOCK(psock->s_flags))
{ {
/* Return OK if something was received; EGAIN if not */ /* Return the number of bytes read from the read-ahead buffer if
* something was received (already in 'ret'); EGAIN if not.
*/
if (state.rf_recvlen <= 0) if (ret <= 0)
{ {
/* Nothing was received */ /* Nothing was received */
ret = -EAGAIN; ret = -EAGAIN;
} }
else
{
/* The return value is the number of bytes read from the read-ahead buffer */
ret = state.rf_recvlen;
}
} }
/* It is okay to block if we need to. If there is space to receive anything /* It is okay to block if we need to. If there is space to receive anything
* more, then we will wait to receive the data. * more, then we will wait to receive the data. Otherwise return the number
* of bytes read from the read-ahead buffer (already in 'ret').
*/ */
else if (state.rf_buflen > 0) else
#endif #endif
if (state.rf_buflen > 0)
{ {
struct uip_conn *conn = (struct uip_conn *)psock->s_conn; struct uip_conn *conn = (struct uip_conn *)psock->s_conn;
@ -977,7 +979,7 @@ static ssize_t tcp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
uip_unlock(save); uip_unlock(save);
recvfrom_uninit(&state); recvfrom_uninit(&state);
return ret; return (ssize_t)ret;
} }
#endif /* CONFIG_NET_TCP */ #endif /* CONFIG_NET_TCP */