From 2a8c095e387fa5ad4907ee2c8fa350b388fc7139 Mon Sep 17 00:00:00 2001 From: CKT Date: Sat, 3 Sep 2022 20:51:19 +0800 Subject: [PATCH] udp socket bugfix (#1640) Co-authored-by: chenkete --- lualib-src/lua-socket.c | 5 +++-- skynet-src/skynet_socket.c | 2 +- skynet-src/skynet_socket.h | 2 +- skynet-src/socket_server.c | 2 +- skynet-src/socket_server.h | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index ea97dce6..3662e7cd 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -648,12 +648,13 @@ ludp(lua_State *L) { host = address_port(L, tmp, addr, 2, &port); } - int id = skynet_socket_udp(ctx, host, port); + int id = skynet_socket_udp(ctx, host, &port); if (id < 0) { return luaL_error(L, "udp init failed"); } lua_pushinteger(L, id); - return 1; + lua_pushinteger(L, port); + return 2; } static int diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 31acafc0..00fd7778 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -175,7 +175,7 @@ skynet_socket_nodelay(struct skynet_context *ctx, int id) { } int -skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port) { +skynet_socket_udp(struct skynet_context *ctx, const char * addr, int *port) { uint32_t source = skynet_context_handle(ctx); return socket_server_udp(SOCKET_SERVER, source, addr, port); } diff --git a/skynet-src/skynet_socket.h b/skynet-src/skynet_socket.h index 59aabb86..3a6f263f 100644 --- a/skynet-src/skynet_socket.h +++ b/skynet-src/skynet_socket.h @@ -38,7 +38,7 @@ void skynet_socket_start(struct skynet_context *ctx, int id); void skynet_socket_pause(struct skynet_context *ctx, int id); void skynet_socket_nodelay(struct skynet_context *ctx, int id); -int skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port); +int skynet_socket_udp(struct skynet_context *ctx, const char * addr, int *port); int skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * addr, int port); int skynet_socket_udp_sendbuffer(struct skynet_context *ctx, const char * address, struct socket_sendbuffer *buffer); const char * skynet_socket_udp_address(struct skynet_socket_message *, int *addrsz); diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index b9da6b97..745a6335 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -2070,7 +2070,7 @@ socket_server_userobject(struct socket_server *ss, struct socket_object_interfac // UDP int -socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr, int port) { +socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr, int *port) { int fd; int family; if (port != 0 || addr != NULL) { diff --git a/skynet-src/socket_server.h b/skynet-src/socket_server.h index ae47c855..c70438c9 100644 --- a/skynet-src/socket_server.h +++ b/skynet-src/socket_server.h @@ -54,7 +54,7 @@ struct socket_udp_address; // create an udp socket handle, attach opaque with it . udp socket don't need call socket_server_start to recv message // if port != 0, bind the socket . if addr == NULL, bind ipv4 0.0.0.0 . If you want to use ipv6, addr can be "::" and port 0. -int socket_server_udp(struct socket_server *, uintptr_t opaque, const char * addr, int port); +int socket_server_udp(struct socket_server *, uintptr_t opaque, const char * addr, int *port); // set default dest address, return 0 when success int socket_server_udp_connect(struct socket_server *, int id, const char * addr, int port); // If the socket_udp_address is NULL, use last call socket_server_udp_connect address instead