先上结论,通过tcp_arg()
函数为tcp_connect()
中的callback
传递参数。
/* Demo */
···
void client_build()
{
struct tcp_pcb *tcp_client_pcb = NULL;
ip_addr_t ipaddr;
IP4_ADDR(&ipaddr, 127, 0, 0, 1);
tcp_client_pcb = tcp_new();
tcp_bind(tcp_client_pcb, IP_ADDR_ANY, server_port);
err_t err;
char* str = "this is a test message";
tcp_arg(tcp_client_pcb , test_arg );
tcp_connect(tcp_client_pcb , client_addr, server_port, connectsuccess);
ip_set_option(looper->client_pcb, SOF_KEEPALIVE);
tcp_err(looper->client_pcb, client_connecterror);
}
void connectsuccess(void *arg, struct tcp_pcb *tpcb, err_t err)
{
printf(" client connect successful, %s \r\n", arg);
}
tcp_arg()
原型如下, 将参数交给client
的pcb
的callback_arg
void
tcp_arg(struct tcp_pcb *pcb, void *arg)
{
LWIP_ASSERT_CORE_LOCKED();
/* This function is allowed to be called for both listen pcbs and
connection pcbs. */
if (pcb != NULL) {
pcb->callback_arg = arg;
}
}
tcp_connect
的调用过程
err_t
tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port,
tcp_connected_fn connected)
{
···
pcb->connected = connected;
···
}
#define TCP_EVENT_CONNECTED(pcb,err,ret) \
do { \
if((pcb)->connected != NULL) \
(ret) = (pcb)->connected((pcb)->callback_arg,(pcb),(err)); \
else (ret) = ERR_OK; \
} while (0)
static err_t
tcp_process(struct tcp_pcb *pcb)
{
···
switch (pcb->state) {
case SYN_SENT:
···
if ((flags & TCP_ACK) && (flags & TCP_SYN)
&& (ackno == pcb->lastack + 1)) {
···
TCP_EVENT_CONNECTED(pcb, ERR_OK, err);
···
}
···
}
}
···
}