diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-06-05 23:29:38 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-06-05 23:29:38 +0000 |
commit | a5955f1c84d42d78af27a298f3cdb54e9cd9bff0 (patch) | |
tree | 1c2ea4136091e836c9c285c7ec83b0e1da1b9adc /nuttx/net | |
parent | 260b4435a85b9f4ae5d70830c1259af7afee256f (diff) | |
download | px4-nuttx-a5955f1c84d42d78af27a298f3cdb54e9cd9bff0.tar.gz px4-nuttx-a5955f1c84d42d78af27a298f3cdb54e9cd9bff0.tar.bz2 px4-nuttx-a5955f1c84d42d78af27a298f3cdb54e9cd9bff0.zip |
Minor improvement in recvfrom()
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3673 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/net')
-rw-r--r-- | nuttx/net/recvfrom.c | 62 |
1 files changed, 32 insertions, 30 deletions
diff --git a/nuttx/net/recvfrom.c b/nuttx/net/recvfrom.c index d974b85cf..d9039b91b 100644 --- a/nuttx/net/recvfrom.c +++ b/nuttx/net/recvfrom.c @@ -861,7 +861,7 @@ static ssize_t tcp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, { struct recvfrom_s state; uip_lock_t save; - int ret = OK; + int ret; /* Initialize the state structure. This is done with interrupts * 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 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 - /* 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)) { @@ -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 (state.rf_recvlen <= 0) + if (ret <= 0) +#endif { - /* Nothing was received. The SOCK_STREAM must be re-connected in - * order to receive an additional data. + /* Nothing was previously received from the readahead buffers. + * The SOCK_STREAM must be (re-)connected in order to receive any + * additional data. */ 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 * 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. */ + else #if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0 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 */ 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 - * 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 + if (state.rf_buflen > 0) { 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); recvfrom_uninit(&state); - return ret; + return (ssize_t)ret; } #endif /* CONFIG_NET_TCP */ |