From 3eb38d7611e35957e5e0e06d01eef76472f506dc Mon Sep 17 00:00:00 2001 From: fbernon Date: Wed, 21 Mar 2007 16:38:58 +0000 Subject: [PATCH] api_lib.c, api_msg.c, tcpip.c: integrate sys_mbox_fetch(conn->mbox, NULL) calls from api_lib.c to tcpip.c's tcpip_apimsg(). Now, use a local variable and not a dynamic one from memp to send tcpip_msg to tcpip_thread in a synchrone call. Free tcpip_msg from tcpip_apimsg is not done in tcpip_thread. This give a faster and more reliable communication between api_lib and tcpip. --- CHANGELOG | 8 +++ src/api/api_lib.c | 51 +++++----------- src/api/api_msg.c | 152 +++++++++++++++++++++++----------------------- src/api/tcpip.c | 18 +++--- 4 files changed, 108 insertions(+), 121 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index c588c936..d9757bae 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -70,6 +70,14 @@ HISTORY * api_lib.c: Use memcpy in netbuf_copy_partial. ++ Bug fixes: + + 2007-03-21 Frédéric Bernon + * api_lib.c, api_msg.c, tcpip.c: integrate sys_mbox_fetch(conn->mbox, NULL) calls from + api_lib.c to tcpip.c's tcpip_apimsg(). Now, use a local variable and not a + dynamic one from memp to send tcpip_msg to tcpip_thread in a synchrone call. + Free tcpip_msg from tcpip_apimsg is not done in tcpip_thread. This give a + faster and more reliable communication between api_lib and tcpip. + 2007-03-21 Frédéric Bernon * opt.h: Add LWIP_NETIF_CALLBACK (to avoid compiler warning) and set it to 0. diff --git a/src/api/api_lib.c b/src/api/api_lib.c index d784306d..6922192a 100644 --- a/src/api/api_lib.c +++ b/src/api/api_lib.c @@ -226,17 +226,16 @@ netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u16_t proto, } conn->state = NETCONN_NONE; conn->socket = 0; + conn->callback = callback; + conn->recv_avail = 0; #if LWIP_SO_RCVTIMEO conn->recv_timeout = 0; #endif /* LWIP_SO_RCVTIMEO */ - conn->callback = callback; - conn->recv_avail = 0; msg.type = API_MSG_NEWCONN; msg.msg.msg.bc.port = proto; /* misusing the port field */ msg.msg.conn = conn; api_msg_post(&msg); - sys_mbox_fetch(conn->mbox, NULL); if ( conn->err != ERR_OK ) { memp_free(MEMP_NETCONN, conn); @@ -274,7 +273,6 @@ netconn_delete(struct netconn *conn) msg.type = API_MSG_DELCONN; msg.msg.conn = conn; api_msg_post(&msg); - sys_mbox_fetch(conn->mbox, NULL); /* Drain the recvmbox. */ if (conn->recvmbox != SYS_MBOX_NULL) { @@ -296,17 +294,18 @@ netconn_delete(struct netconn *conn) while (sys_arch_mbox_fetch(conn->acceptmbox, &mem, 1) != SYS_ARCH_TIMEOUT) { netconn_delete((struct netconn *)mem); } - sys_mbox_free(conn->acceptmbox); conn->acceptmbox = SYS_MBOX_NULL; } sys_mbox_free(conn->mbox); conn->mbox = SYS_MBOX_NULL; + if (conn->sem != SYS_SEM_NULL) { sys_sem_free(conn->sem); + /* conn->sem = SYS_SEM_NULL; */ } - /* conn->sem = SYS_SEM_NULL;*/ + memp_free(MEMP_NETCONN, conn); return ERR_OK; } @@ -389,7 +388,6 @@ netconn_bind(struct netconn *conn, struct ip_addr *addr, msg.msg.msg.bc.ipaddr = addr; msg.msg.msg.bc.port = port; api_msg_post(&msg); - sys_mbox_fetch(conn->mbox, NULL); return conn->err; } @@ -404,7 +402,6 @@ netconn_connect(struct netconn *conn, struct ip_addr *addr, return ERR_VAL; } - if (conn->recvmbox == SYS_MBOX_NULL) { if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) { return ERR_MEM; @@ -416,7 +413,6 @@ netconn_connect(struct netconn *conn, struct ip_addr *addr, msg.msg.msg.bc.ipaddr = addr; msg.msg.msg.bc.port = port; api_msg_post(&msg); - sys_mbox_fetch(conn->mbox, NULL); return conn->err; } @@ -432,7 +428,6 @@ netconn_disconnect(struct netconn *conn) msg.type = API_MSG_DISCONNECT; msg.msg.conn = conn; api_msg_post(&msg); - sys_mbox_fetch(conn->mbox, NULL); return conn->err; } @@ -456,7 +451,6 @@ netconn_listen(struct netconn *conn) msg.type = API_MSG_LISTEN; msg.msg.conn = conn; api_msg_post(&msg); - sys_mbox_fetch(conn->mbox, NULL); return conn->err; } @@ -504,7 +498,6 @@ netconn_recv(struct netconn *conn) return NULL; } - buf = memp_malloc(MEMP_NETBUF); if (buf == NULL) { @@ -549,8 +542,6 @@ netconn_recv(struct netconn *conn) msg.msg.msg.len = 1; } api_msg_post(&msg); - - sys_mbox_fetch(conn->mbox, NULL); } else { #if LWIP_SO_RCVTIMEO sys_mbox_fetch_timeout(conn->recvmbox, (void *)&buf, conn->recv_timeout); @@ -564,13 +555,9 @@ netconn_recv(struct netconn *conn) (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, buf->p->tot_len); } } - - - LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv: received %p (err %d)\n", (void *)buf, conn->err)); - return buf; } @@ -592,8 +579,6 @@ netconn_send(struct netconn *conn, struct netbuf *buf) msg.msg.conn = conn; msg.msg.msg.p = buf->p; api_msg_post(&msg); - - sys_mbox_fetch(conn->mbox, NULL); return conn->err; } @@ -613,7 +598,6 @@ netconn_write(struct netconn *conn, const void *dataptr, u16_t size, u8_t copy) msg.type = API_MSG_WRITE; msg.msg.conn = conn; - conn->state = NETCONN_WRITE; while (conn->err == ERR_OK && size > 0) { @@ -622,17 +606,17 @@ netconn_write(struct netconn *conn, const void *dataptr, u16_t size, u8_t copy) if (conn->type == NETCONN_TCP) { if (tcp_sndbuf(conn->pcb.tcp) == 0) { - sys_sem_wait(conn->sem); - if (conn->err != ERR_OK) { - goto ret; - } + sys_sem_wait(conn->sem); + if (conn->err != ERR_OK) { + goto ret; + } } if (size > tcp_sndbuf(conn->pcb.tcp)) { - /* We cannot send more than one send buffer's worth of data at a - time. */ - len = tcp_sndbuf(conn->pcb.tcp); + /* We cannot send more than one send buffer's worth of data at a + time. */ + len = tcp_sndbuf(conn->pcb.tcp); } else { - len = size; + len = size; } } else { len = size; @@ -641,7 +625,6 @@ netconn_write(struct netconn *conn, const void *dataptr, u16_t size, u8_t copy) LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_write: writing %d bytes (%d)\n", len, copy)); msg.msg.msg.w.len = len; api_msg_post(&msg); - sys_mbox_fetch(conn->mbox, NULL); if (conn->err == ERR_OK) { dataptr = (void *)((u8_t *)dataptr + len); size -= len; @@ -672,7 +655,6 @@ netconn_close(struct netconn *conn) msg.type = API_MSG_CLOSE; msg.msg.conn = conn; api_msg_post(&msg); - sys_mbox_fetch(conn->mbox, NULL); if (conn->err == ERR_MEM && conn->sem != SYS_SEM_NULL) { sys_sem_wait(conn->sem); @@ -700,15 +682,14 @@ netconn_join_leave_group (struct netconn *conn, return conn->err; } - msg.type = API_MSG_JOIN_LEAVE; - msg.msg.conn = conn; ipaddr[0] = multiaddr; ipaddr[1] = interface; + + msg.type = API_MSG_JOIN_LEAVE; + msg.msg.conn = conn; msg.msg.msg.bc.ipaddr = (struct ip_addr *)ipaddr; msg.msg.msg.bc.port = join_or_leave; api_msg_post(&msg); - - sys_mbox_fetch(conn->mbox, NULL); return conn->err; } #endif /* LWIP_IGMP */ diff --git a/src/api/api_msg.c b/src/api/api_msg.c index 74e61050..39519223 100644 --- a/src/api/api_msg.c +++ b/src/api/api_msg.c @@ -62,7 +62,7 @@ recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p, conn->recv_avail += p->tot_len; /* Register event with callback */ if (conn->callback) - (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, p->tot_len); + (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, p->tot_len); sys_mbox_post(conn->recvmbox, buf); } @@ -97,10 +97,10 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p, buf->fromport = port; } - conn->recv_avail += p->tot_len; + conn->recv_avail += p->tot_len; /* Register event with callback */ if (conn->callback) - (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, p->tot_len); + (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, p->tot_len); sys_mbox_post(conn->recvmbox, buf); } } @@ -126,14 +126,14 @@ recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) conn->err = err; if (p != NULL) { - len = p->tot_len; - conn->recv_avail += len; + len = p->tot_len; + conn->recv_avail += len; + } else { + len = 0; } - else - len = 0; /* Register event with callback */ if (conn->callback) - (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, len); + (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, len); sys_mbox_post(conn->recvmbox, p); } return ERR_OK; @@ -171,8 +171,8 @@ sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len) } if (conn && conn->callback) - if (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) - (*conn->callback)(conn, NETCONN_EVT_SENDPLUS, len); + if (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) + (*conn->callback)(conn, NETCONN_EVT_SENDPLUS, len); return ERR_OK; } @@ -264,8 +264,7 @@ accept_function(void *arg, struct tcp_pcb *newpcb, err_t err) newconn->acceptmbox = SYS_MBOX_NULL; newconn->err = err; /* Register event with callback */ - if (conn->callback) - { + if (conn->callback) { (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, 0); } /* We have to set the callback here even though @@ -304,8 +303,8 @@ do_newconn(struct api_msg_msg *msg) case NETCONN_UDPLITE: msg->conn->pcb.udp = udp_new(); if(msg->conn->pcb.udp == NULL) { - msg->conn->err = ERR_MEM; - break; + msg->conn->err = ERR_MEM; + break; } udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE); udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn); @@ -313,8 +312,8 @@ do_newconn(struct api_msg_msg *msg) case NETCONN_UDPNOCHKSUM: msg->conn->pcb.udp = udp_new(); if(msg->conn->pcb.udp == NULL) { - msg->conn->err = ERR_MEM; - break; + msg->conn->err = ERR_MEM; + break; } udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM); udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn); @@ -322,8 +321,8 @@ do_newconn(struct api_msg_msg *msg) case NETCONN_UDP: msg->conn->pcb.udp = udp_new(); if(msg->conn->pcb.udp == NULL) { - msg->conn->err = ERR_MEM; - break; + msg->conn->err = ERR_MEM; + break; } udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn); break; @@ -332,8 +331,8 @@ do_newconn(struct api_msg_msg *msg) case NETCONN_TCP: msg->conn->pcb.tcp = tcp_new(); if(msg->conn->pcb.tcp == NULL) { - msg->conn->err = ERR_MEM; - break; + msg->conn->err = ERR_MEM; + break; } setup_tcp(msg->conn); break; @@ -368,27 +367,26 @@ do_delconn(struct api_msg_msg *msg) #if LWIP_TCP case NETCONN_TCP: if (msg->conn->pcb.tcp->state == LISTEN) { - tcp_arg(msg->conn->pcb.tcp, NULL); - tcp_accept(msg->conn->pcb.tcp, NULL); - tcp_close(msg->conn->pcb.tcp); + tcp_arg(msg->conn->pcb.tcp, NULL); + tcp_accept(msg->conn->pcb.tcp, NULL); + tcp_close(msg->conn->pcb.tcp); } else { - tcp_arg(msg->conn->pcb.tcp, NULL); - tcp_sent(msg->conn->pcb.tcp, NULL); - tcp_recv(msg->conn->pcb.tcp, NULL); - tcp_poll(msg->conn->pcb.tcp, NULL, 0); - tcp_err(msg->conn->pcb.tcp, NULL); - if (tcp_close(msg->conn->pcb.tcp) != ERR_OK) { - tcp_abort(msg->conn->pcb.tcp); - } + tcp_arg(msg->conn->pcb.tcp, NULL); + tcp_sent(msg->conn->pcb.tcp, NULL); + tcp_recv(msg->conn->pcb.tcp, NULL); + tcp_poll(msg->conn->pcb.tcp, NULL, 0); + tcp_err(msg->conn->pcb.tcp, NULL); + if (tcp_close(msg->conn->pcb.tcp) != ERR_OK) { + tcp_abort(msg->conn->pcb.tcp); + } } #endif - default: + default: break; } } /* Trigger select() in socket layer */ - if (msg->conn->callback) - { + if (msg->conn->callback) { (*msg->conn->callback)(msg->conn, NETCONN_EVT_RCVPLUS, 0); (*msg->conn->callback)(msg->conn, NETCONN_EVT_SENDPLUS, 0); } @@ -451,8 +449,7 @@ do_bind(struct api_msg_msg *msg) #endif /* LWIP_UDP */ #if LWIP_TCP case NETCONN_TCP: - msg->conn->err = tcp_bind(msg->conn->pcb.tcp, - msg->msg.bc.ipaddr, msg->msg.bc.port); + msg->conn->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port); #endif /* LWIP_TCP */ default: break; @@ -477,7 +474,7 @@ do_connected(void *arg, struct tcp_pcb *pcb, err_t err) conn->err = err; if (conn->type == NETCONN_TCP && err == ERR_OK) { setup_tcp(conn); - } + } sys_mbox_post(conn->mbox, NULL); return ERR_OK; } @@ -498,9 +495,9 @@ do_connect(struct api_msg_msg *msg) case NETCONN_UDPLITE: msg->conn->pcb.udp = udp_new(); if (msg->conn->pcb.udp == NULL) { - msg->conn->err = ERR_MEM; - sys_mbox_post(msg->conn->mbox, NULL); - return; + msg->conn->err = ERR_MEM; + sys_mbox_post(msg->conn->mbox, NULL); + return; } udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE); udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn); @@ -508,9 +505,9 @@ do_connect(struct api_msg_msg *msg) case NETCONN_UDPNOCHKSUM: msg->conn->pcb.udp = udp_new(); if (msg->conn->pcb.udp == NULL) { - msg->conn->err = ERR_MEM; - sys_mbox_post(msg->conn->mbox, NULL); - return; + msg->conn->err = ERR_MEM; + sys_mbox_post(msg->conn->mbox, NULL); + return; } udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM); udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn); @@ -518,20 +515,20 @@ do_connect(struct api_msg_msg *msg) case NETCONN_UDP: msg->conn->pcb.udp = udp_new(); if (msg->conn->pcb.udp == NULL) { - msg->conn->err = ERR_MEM; - sys_mbox_post(msg->conn->mbox, NULL); - return; + msg->conn->err = ERR_MEM; + sys_mbox_post(msg->conn->mbox, NULL); + return; } udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn); break; #endif /* LWIP_UDP */ -#if LWIP_TCP +#if LWIP_TCP case NETCONN_TCP: - msg->conn->pcb.tcp = tcp_new(); + msg->conn->pcb.tcp = tcp_new(); if (msg->conn->pcb.tcp == NULL) { - msg->conn->err = ERR_MEM; - sys_mbox_post(msg->conn->mbox, NULL); - return; + msg->conn->err = ERR_MEM; + sys_mbox_post(msg->conn->mbox, NULL); + return; } #endif default: @@ -618,17 +615,17 @@ do_listen(struct api_msg_msg *msg) case NETCONN_TCP: msg->conn->pcb.tcp = tcp_listen(msg->conn->pcb.tcp); if (msg->conn->pcb.tcp == NULL) { - msg->conn->err = ERR_MEM; + msg->conn->err = ERR_MEM; } else { - if (msg->conn->acceptmbox == SYS_MBOX_NULL) { - msg->conn->acceptmbox = sys_mbox_new(); - if (msg->conn->acceptmbox == SYS_MBOX_NULL) { - msg->conn->err = ERR_MEM; - break; - } - } - tcp_arg(msg->conn->pcb.tcp, msg->conn); - tcp_accept(msg->conn->pcb.tcp, accept_function); + if (msg->conn->acceptmbox == SYS_MBOX_NULL) { + msg->conn->acceptmbox = sys_mbox_new(); + if (msg->conn->acceptmbox == SYS_MBOX_NULL) { + msg->conn->err = ERR_MEM; + break; + } + } + tcp_arg(msg->conn->pcb.tcp, msg->conn); + tcp_accept(msg->conn->pcb.tcp, accept_function); } #endif default: @@ -698,7 +695,7 @@ do_recv(struct api_msg_msg *msg) tcp_recved(msg->conn->pcb.tcp, msg->msg.len); } } -#endif +#endif sys_mbox_post(msg->conn->mbox, NULL); } @@ -725,13 +722,13 @@ do_write(struct api_msg_msg *msg) break; #endif /* LWIP_UDP */ #if LWIP_TCP - case NETCONN_TCP: + case NETCONN_TCP: err = tcp_write(msg->conn->pcb.tcp, msg->msg.w.dataptr, msg->msg.w.len, msg->msg.w.copy); /* This is the Nagle algorithm: inhibit the sending of new TCP - segments when new outgoing data arrives from the user if any - previously transmitted data on the connection remains - unacknowledged. */ + segments when new outgoing data arrives from the user if any + previously transmitted data on the connection remains + unacknowledged. */ if(err == ERR_OK && (msg->conn->pcb.tcp->unacked == NULL || (msg->conn->pcb.tcp->flags & TF_NODELAY) || (msg->conn->pcb.tcp->snd_queuelen) > 1)) { @@ -739,11 +736,10 @@ do_write(struct api_msg_msg *msg) } msg->conn->err = err; if (msg->conn->callback) - if (err == ERR_OK) - { - if (tcp_sndbuf(msg->conn->pcb.tcp) <= TCP_SNDLOWAT) - (*msg->conn->callback)(msg->conn, NETCONN_EVT_SENDMINUS, msg->msg.w.len); - } + if (err == ERR_OK) { + if (tcp_sndbuf(msg->conn->pcb.tcp) <= TCP_SNDLOWAT) + (*msg->conn->callback)(msg->conn, NETCONN_EVT_SENDMINUS, msg->msg.w.len); + } #endif default: break; @@ -781,9 +777,9 @@ do_close(struct api_msg_msg *msg) else if (msg->conn->pcb.tcp->state == CLOSE_WAIT) { err = tcp_output(msg->conn->pcb.tcp); } - msg->conn->err = err; + msg->conn->err = err; #endif - default: + default: break; } } @@ -816,11 +812,11 @@ do_join_leave_group(struct api_msg_msg *msg) case NETCONN_TCP: break; #endif - default: + default: break; } } - msg->conn->err = err; + msg->conn->err = err; sys_mbox_post(msg->conn->mbox, NULL); } #endif /* LWIP_IGMP */ @@ -842,10 +838,12 @@ static api_msg_decode decode[API_MSG_MAX] = { do_join_leave_group #endif /* LWIP_IGMP */ }; + void api_msg_input(struct api_msg *msg) -{ - decode[msg->type](&(msg->msg)); +{ struct api_msg_msg msg_copy; + msg_copy=msg->msg; + decode[msg->type](&msg_copy); } err_t diff --git a/src/api/tcpip.c b/src/api/tcpip.c index 5d742909..7ebe05eb 100644 --- a/src/api/tcpip.c +++ b/src/api/tcpip.c @@ -211,7 +211,10 @@ tcpip_thread(void *arg) default: break; } - memp_free(MEMP_TCPIP_MSG, msg); + + if (msg->type!=TCPIP_MSG_API) { + memp_free(MEMP_TCPIP_MSG, msg); + } } } @@ -284,16 +287,13 @@ tcpip_callback(void (*f)(void *ctx), void *ctx) err_t tcpip_apimsg(struct api_msg *apimsg) { - struct tcpip_msg *msg; + struct tcpip_msg msg; if (mbox != SYS_MBOX_NULL) { - msg = memp_malloc(MEMP_TCPIP_MSG); - if (msg == NULL) { - return ERR_MEM; - } - msg->type = TCPIP_MSG_API; - msg->msg.apimsg = apimsg; - sys_mbox_post(mbox, msg); + msg.type = TCPIP_MSG_API; + msg.msg.apimsg = apimsg; + sys_mbox_post(mbox, &msg); + sys_mbox_fetch(apimsg->msg.conn->mbox, NULL); return ERR_OK; } return ERR_VAL;