Skip to content

Commit

Permalink
Merge pull request #48 from oylbin/master
Browse files Browse the repository at this point in the history
fix enet_packet_resize
  • Loading branch information
zpl-zak authored Mar 1, 2024
2 parents 049a093 + 319d0e5 commit f849455
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
13 changes: 7 additions & 6 deletions include/enet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*);

Expand Down Expand Up @@ -1396,28 +1396,29 @@ 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;

if (dataLength <= packet->dataLength || (packet->flags & ENET_PACKET_FLAG_NO_ALLOCATE))
{
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) {
Expand Down
2 changes: 1 addition & 1 deletion misc/docs/tutorial.dox
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
12 changes: 10 additions & 2 deletions test/resize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
#include "enet.h"
int main() {
ENetPacket *packet = enet_packet_create("packet",
strlen("packet") + 1,
strlen("packet"),
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"));
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);
}

0 comments on commit f849455

Please sign in to comment.