Permalink
Browse files

[LIBS] netstack: Add support for receive timeout in netstack_socket_r…

…ecv()

Signed-off-by: Anup Patel <anup@brainfault.org>
  • Loading branch information...
1 parent fd6e84c commit b77813123fca34cefba12b2385d5261d0c14a5a9 @avpatel avpatel committed Jan 22, 2013
Showing with 22 additions and 5 deletions.
  1. +1 −1 daemons/telnetd.c
  2. +4 −1 libs/include/libs/netstack.h
  3. +17 −3 libs/netstack/lwip/port/netstack.c
View
@@ -236,7 +236,7 @@ static void telnetd_fill_rx_buffer(void)
vmm_spin_unlock_irqrestore(&tdctrl.lock, flags);
/* Recieve netstack socket buffer */
- rc = netstack_socket_recv(tdctrl.active_sk, &buf);
+ rc = netstack_socket_recv(tdctrl.active_sk, &buf, -1);
if (rc) {
telnetd_set_disconnected();
TELNETD_DPRINTF("%s: Socket read failed\n", __func__);
@@ -237,13 +237,15 @@ int netstack_socket_close(struct netstack_socket *sk);
*
* @sk - pointer to socket
* @buf - pointer to socket buffer
+ * @timeout - timeout in milliseconds (<=0 means wait forever)
*
* returns
* VMM_OK - success
* VMM_Exxxx - failure
*/
int netstack_socket_recv(struct netstack_socket *sk,
- struct netstack_socket_buf *buf);
+ struct netstack_socket_buf *buf,
+ int timeout);
/**
* Traverse chain of socket buffer.
@@ -252,6 +254,7 @@ int netstack_socket_recv(struct netstack_socket *sk,
*
* returns
* VMM_OK - success
+ * VMM_ETIMEDOUT - upon receive timeout
* VMM_Exxxx - failure (this is no futher buffers)
*/
int netstack_socket_nextbuf(struct netstack_socket_buf *buf);
@@ -585,17 +585,31 @@ int netstack_socket_close(struct netstack_socket *sk)
VMM_EXPORT_SYMBOL(netstack_socket_close);
int netstack_socket_recv(struct netstack_socket *sk,
- struct netstack_socket_buf *buf)
+ struct netstack_socket_buf *buf,
+ int timeout)
{
err_t err;
struct netbuf *nb;
+ struct netconn *conn;
if (!sk || !sk->priv || !buf) {
return VMM_EINVALID;
}
+ conn = sk->priv;
- err = netconn_recv(sk->priv, &nb);
- if (err != ERR_OK) {
+ if (0 < timeout) {
+ netconn_set_recvtimeout(conn, timeout);
+ } else {
+ netconn_set_recvtimeout(conn, 0);
+ }
+
+ buf->data = NULL;
+ buf->len = 0;
+
+ err = netconn_recv(conn, &nb);
+ if (err == ERR_TIMEOUT) {
+ return VMM_ETIMEDOUT;
+ } else if (err != ERR_OK) {
return VMM_EFAIL;
}

0 comments on commit b778131

Please sign in to comment.