From 6746beb2a36f17a6371dcbe400afdd85448fef42 Mon Sep 17 00:00:00 2001 From: goldsimon Date: Fri, 30 Nov 2007 12:54:10 +0000 Subject: [PATCH] Compacted code: moved the code creating a netconn (without pcb) from netconn_new_with_proto_and_callback to new (synchroneous) function netconn_alloc_with_proto_and_callback and call this function from netconn_new_with_proto_and_callback and accept_function. --- src/api/api_lib.c | 58 ++++++++----------------- src/api/api_msg.c | 89 +++++++++++++++++++++++++------------- src/include/lwip/api_msg.h | 4 ++ 3 files changed, 80 insertions(+), 71 deletions(-) diff --git a/src/api/api_lib.c b/src/api/api_lib.c index 1d96fe21..9bedc1a9 100644 --- a/src/api/api_lib.c +++ b/src/api/api_lib.c @@ -64,52 +64,28 @@ * @return a newly allocated struct netconn or * NULL on memory error */ -struct -netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, - void (*callback)(struct netconn *, enum netconn_evt, u16_t len)) +struct netconn* +netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, + void (*callback)(struct netconn *, enum netconn_evt, u16_t len)) { struct netconn *conn; struct api_msg msg; - conn = memp_malloc(MEMP_NETCONN); - if (conn == NULL) { - return NULL; + conn = netconn_alloc_with_proto_and_callback(t, proto, callback); + if (conn != NULL ) { + msg.function = do_newconn; + msg.msg.msg.n.proto = proto; + msg.msg.conn = conn; + TCPIP_APIMSG(&msg); + + if (conn->err != ERR_OK) { + LWIP_ASSERT("freeing conn without freeing pcb", conn->pcb.tcp == NULL); + sys_mbox_free(conn->mbox); + sys_mbox_free(conn->recvmbox); + memp_free(MEMP_NETCONN, conn); + return NULL; + } } - - conn->err = ERR_OK; - conn->type = t; - conn->pcb.tcp = NULL; - - if ((conn->mbox = sys_mbox_new()) == SYS_MBOX_NULL) { - memp_free(MEMP_NETCONN, conn); - return NULL; - } - conn->recvmbox = SYS_MBOX_NULL; - conn->acceptmbox = SYS_MBOX_NULL; - conn->state = NETCONN_NONE; - /* initialize socket to -1 since 0 is a valid socket */ - conn->socket = -1; - conn->callback = callback; - conn->recv_avail = 0; -#if LWIP_SO_RCVTIMEO - conn->recv_timeout = 0; -#endif /* LWIP_SO_RCVTIMEO */ -#if LWIP_SO_RCVBUF - conn->recv_bufsize = INT_MAX; -#endif /* LWIP_SO_RCVBUF */ - - msg.function = do_newconn; - msg.msg.msg.n.proto = proto; - msg.msg.conn = conn; - TCPIP_APIMSG(&msg); - - if (conn->err != ERR_OK) { - LWIP_ASSERT("freeing conn without freeing pcb", conn->pcb.tcp == NULL); - sys_mbox_free(conn->mbox); - memp_free(MEMP_NETCONN, conn); - return NULL; - } - return conn; } diff --git a/src/api/api_msg.c b/src/api/api_msg.c index fd402c67..d9f9802c 100644 --- a/src/api/api_msg.c +++ b/src/api/api_msg.c @@ -115,9 +115,11 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct netbuf *buf; struct netconn *conn; - LWIP_UNUSED_ARG(pcb); - + LWIP_UNUSED_ARG(pcb); /* only used for asserts... */ + LWIP_ASSERT("recv_udp must have a pcb argument", pcb != NULL); + LWIP_ASSERT("recv_udp must have an argument", arg != NULL); conn = arg; + LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb); #if LWIP_SO_RCVBUF if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL) || @@ -161,8 +163,10 @@ recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) u16_t len; LWIP_UNUSED_ARG(pcb); - + LWIP_ASSERT("recv_tcp must have a pcb argument", pcb != NULL); + LWIP_ASSERT("recv_tcp must have an argument", arg != NULL); conn = arg; + LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb); if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL)) { pbuf_free(p); @@ -324,41 +328,17 @@ accept_function(void *arg, struct tcp_pcb *newpcb, err_t err) LWIP_ERROR("accept_function: invalid conn->acceptmbox", conn->acceptmbox != SYS_MBOX_NULL, return ERR_VAL;); - newconn = memp_malloc(MEMP_NETCONN); + /* We have to set the callback here even though + * the new socket is unknown. conn->socket is marked as -1. */ + newconn = netconn_alloc_with_proto_and_callback(conn->type, 0, conn->callback); if (newconn == NULL) { return ERR_MEM; } - newconn->recvmbox = sys_mbox_new(); - if (newconn->recvmbox == SYS_MBOX_NULL) { - memp_free(MEMP_NETCONN, newconn); - return ERR_MEM; - } - newconn->mbox = sys_mbox_new(); - if (newconn->mbox == SYS_MBOX_NULL) { - sys_mbox_free(newconn->recvmbox); - memp_free(MEMP_NETCONN, newconn); - return ERR_MEM; - } - /* Allocations were OK, setup the PCB etc */ - newconn->type = NETCONN_TCP; newconn->pcb.tcp = newpcb; setup_tcp(newconn); - newconn->state = NETCONN_NONE; - newconn->acceptmbox = SYS_MBOX_NULL; newconn->err = err; /* Register event with callback */ API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0); - /* We have to set the callback here even though - * the new socket is unknown. Mark the socket as -1. */ - newconn->callback = conn->callback; - newconn->socket = -1; - newconn->recv_avail = 0; -#if LWIP_SO_RCVTIMEO - newconn->recv_timeout = 0; -#endif /* LWIP_SO_RCVTIMEO */ -#if LWIP_SO_RCVBUF - newconn->recv_bufsize = INT_MAX; -#endif /* LWIP_SO_RCVBUF */ sys_mbox_post(conn->acceptmbox, newconn); return ERR_OK; @@ -447,6 +427,55 @@ do_newconn(struct api_msg_msg *msg) TCPIP_APIMSG_ACK(msg); } +/** + * Create a new netconn (of a specific type) that has a callback function. + * The corresponding pcb is NOT created! + * + * @param t the type of 'connection' to create (@see enum netconn_type) + * @param proto the IP protocol for RAW IP pcbs + * @param callback a function to call on status changes (RX available, TX'ed) + * @return a newly allocated struct netconn or + * NULL on memory error + */ +struct netconn* +netconn_alloc_with_proto_and_callback(enum netconn_type t, u8_t proto, + void (*callback)(struct netconn *, enum netconn_evt, u16_t len)) +{ + struct netconn *conn; + + conn = memp_malloc(MEMP_NETCONN); + if (conn == NULL) { + return NULL; + } + + conn->err = ERR_OK; + conn->type = t; + conn->pcb.tcp = NULL; + + if ((conn->mbox = sys_mbox_new()) == SYS_MBOX_NULL) { + memp_free(MEMP_NETCONN, conn); + return NULL; + } + if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) { + sys_mbox_free(conn->mbox); + memp_free(MEMP_NETCONN, conn); + return NULL; + } + conn->acceptmbox = SYS_MBOX_NULL; + conn->state = NETCONN_NONE; + /* initialize socket to -1 since 0 is a valid socket */ + conn->socket = -1; + conn->callback = callback; + conn->recv_avail = 0; +#if LWIP_SO_RCVTIMEO + conn->recv_timeout = 0; +#endif /* LWIP_SO_RCVTIMEO */ +#if LWIP_SO_RCVBUF + conn->recv_bufsize = INT_MAX; +#endif /* LWIP_SO_RCVBUF */ + return conn; +} + #if LWIP_TCP static void do_close_internal(struct netconn *conn) diff --git a/src/include/lwip/api_msg.h b/src/include/lwip/api_msg.h index ce19b40f..47af8205 100644 --- a/src/include/lwip/api_msg.h +++ b/src/include/lwip/api_msg.h @@ -142,6 +142,10 @@ void do_join_leave_group( struct api_msg_msg *msg); void do_gethostbyname(void *arg); #endif /* LWIP_DNS */ +struct netconn* netconn_alloc_with_proto_and_callback( + enum netconn_type t, u8_t proto, + void (*callback)(struct netconn *, enum netconn_evt, u16_t len)); + #ifdef __cplusplus } #endif