From 2de918b86666cd284f4c3d625ece837c00500eca Mon Sep 17 00:00:00 2001 From: oylbin Date: Fri, 1 Mar 2024 16:44:18 +0800 Subject: [PATCH 1/2] fix enet_packet_resize --- include/enet.h | 13 +++++++------ misc/docs/tutorial.dox | 2 +- test/resize.c | 5 ++++- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/include/enet.h b/include/enet.h index 2e63951..b3d1b1b 100644 --- a/include/enet.h +++ b/include/enet.h @@ -254,7 +254,7 @@ extern "C" { extern void *enet_malloc(size_t); extern void enet_free(void *); extern ENetPacket* enet_packet_create(const void*,size_t,enet_uint32); - extern int enet_packet_resize(ENetPacket*, size_t); + extern ENetPacket* enet_packet_resize(ENetPacket*, size_t); extern ENetPacket* enet_packet_copy(ENetPacket*); extern void enet_packet_destroy(ENetPacket*); @@ -1396,9 +1396,9 @@ extern "C" { dataLength parameter @param packet packet to resize @param dataLength new size for the packet data - @returns 0 on success, < 0 on failure + @returns new packet pointer on success, NULL on failure */ - int enet_packet_resize(ENetPacket * packet, size_t dataLength) + ENetPacket* enet_packet_resize(ENetPacket * packet, size_t dataLength) { ENetPacket *newPacket = NULL; @@ -1406,18 +1406,19 @@ extern "C" { { packet->dataLength = dataLength; - return 0; + return packet; } newPacket = (ENetPacket *)enet_malloc(sizeof (ENetPacket) + dataLength); if (newPacket == NULL) - return -1; + return NULL; memcpy(newPacket, packet, sizeof(ENetPacket) + packet->dataLength); newPacket->data = (enet_uint8 *)newPacket + sizeof(ENetPacket); + newPacket->dataLength = dataLength; enet_free(packet); - return 0; + return newPacket; } ENetPacket *enet_packet_create_offset(const void *data, size_t dataLength, size_t dataOffset, enet_uint32 flags) { diff --git a/misc/docs/tutorial.dox b/misc/docs/tutorial.dox index e91eae8..3f45c56 100644 --- a/misc/docs/tutorial.dox +++ b/misc/docs/tutorial.dox @@ -247,7 +247,7 @@ dispatching any events. /* Extend the packet so and append the string "foo", so it now */ /* contains "packetfoo\0" */ - enet_packet_resize (packet, strlen ("packetfoo") + 1); + packet = enet_packet_resize (packet, strlen ("packetfoo") + 1); strcpy (& packet -> data [strlen ("packet")], "foo"); /* Send the packet to the peer over channel id 0. */ diff --git a/test/resize.c b/test/resize.c index 80b99be..16150ec 100644 --- a/test/resize.c +++ b/test/resize.c @@ -4,5 +4,8 @@ int main() { ENetPacket *packet = enet_packet_create("packet", strlen("packet") + 1, ENET_PACKET_FLAG_RELIABLE); - enet_packet_resize(packet, strlen("packetfoo") + 1); + printf("length: %d, data: %.*s\n", packet->dataLength, packet->dataLength, (char*)packet->data); + packet = enet_packet_resize(packet, strlen("packetfoo") + 1); + strcpy (& packet -> data [strlen ("packet")], "foo"); + printf("length: %d, data: %.*s\n", packet->dataLength, packet->dataLength, (char*)packet->data); } From 319d0e5b0db8504ef105a939ce068a59bded3e62 Mon Sep 17 00:00:00 2001 From: oylbin Date: Fri, 1 Mar 2024 16:52:26 +0800 Subject: [PATCH 2/2] update test to make length more clear --- test/resize.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/resize.c b/test/resize.c index 16150ec..4565503 100644 --- a/test/resize.c +++ b/test/resize.c @@ -2,10 +2,15 @@ #include "enet.h" int main() { ENetPacket *packet = enet_packet_create("packet", - strlen("packet") + 1, + strlen("packet"), ENET_PACKET_FLAG_RELIABLE); printf("length: %d, data: %.*s\n", packet->dataLength, packet->dataLength, (char*)packet->data); - packet = enet_packet_resize(packet, strlen("packetfoo") + 1); + packet = enet_packet_resize(packet, strlen("packetfoo")); strcpy (& packet -> data [strlen ("packet")], "foo"); printf("length: %d, data: %.*s\n", packet->dataLength, packet->dataLength, (char*)packet->data); + packet = enet_packet_resize(packet, strlen("packet")); + printf("length: %d, data: %.*s\n", packet->dataLength, packet->dataLength, (char*)packet->data); + packet = enet_packet_resize(packet, strlen("packetfoobar")); + strcpy (& packet -> data [strlen ("packet")], "foobar"); + printf("length: %d, data: %.*s\n", packet->dataLength, packet->dataLength, (char*)packet->data); }