From ba04c31c4c518476cd6fa13cae65f6084dd51dcf Mon Sep 17 00:00:00 2001 From: spacemeowx2 Date: Sat, 6 Oct 2018 20:06:16 +0800 Subject: [PATCH] fix crash --- src/lan-client.c | 30 +++++++++++++++++++++++------- src/lan-play.h | 2 -- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/lan-client.c b/src/lan-client.c index 2a4edd1..0d9af47 100644 --- a/src/lan-client.c +++ b/src/lan-client.c @@ -4,6 +4,12 @@ enum lan_client_type { LAN_CLIENT_TYPE_KEEPALIVE = 0x00, LAN_CLIENT_TYPE_IPV4 = 0x01, }; +struct ipv4_req { + uv_udp_send_t req; + uv_buf_t bufs[2]; + char type; + char *packet; +}; uint8_t BROADCAST_MAC[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; void lan_client_on_recv(uv_udp_t *handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags); @@ -131,24 +137,34 @@ void lan_client_on_recv(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, co void lan_client_on_sent(uv_udp_send_t* req, int status) { - // free(req); + if (status != 0) { + LLOG(LLOG_DEBUG, "lan_client_on_sent %d", status); + } + struct ipv4_req *ipv4_req = req->data; + free(ipv4_req->packet); + free(ipv4_req); } int lan_client_send(struct lan_play *lan_play, const uint8_t type, const void *packet, uint16_t len) { struct sockaddr *server_addr = (struct sockaddr *)&lan_play->server_addr; int ret; - uv_buf_t *bufs = lan_play->client_send_buf; - lan_play->client_send_type = type; + struct ipv4_req *req = malloc(sizeof(struct ipv4_req)); + req->type = type; + req->packet = malloc(len); + memcpy(req->packet, packet, len); + + uv_buf_t *bufs = req->bufs; int bufs_len = 1; - bufs[0] = uv_buf_init((char *)&lan_play->client_send_type, sizeof(type)); + bufs[0] = uv_buf_init(&req->type, sizeof(type)); if (packet && len > 0) { - bufs[1] = uv_buf_init((char *)packet, len); + bufs[1] = uv_buf_init(req->packet, len); bufs_len = 2; } - uv_udp_send_t *req = &lan_play->client_send_req; - ret = uv_udp_send(req, &lan_play->client, bufs, bufs_len, server_addr, lan_client_on_sent); + uv_udp_send_t *udp_req = &req->req; + udp_req->data = req; + ret = uv_udp_send(udp_req, &lan_play->client, bufs, bufs_len, server_addr, lan_client_on_sent); return ret; } diff --git a/src/lan-play.h b/src/lan-play.h index c137214..d83fd7b 100644 --- a/src/lan-play.h +++ b/src/lan-play.h @@ -46,8 +46,6 @@ struct lan_play { // lan_client uv_udp_t client; uv_timer_t client_keepalive_timer; - uv_buf_t client_send_buf[2]; - uint8_t client_send_type; struct sockaddr_in server_addr; struct gateway gateway;