Skip to content

Commit

Permalink
code: bring enet_packet_resize back
Browse files Browse the repository at this point in the history
  • Loading branch information
zpl-zak committed Jun 23, 2023
1 parent 0af71dd commit 42c03e1
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions include/enet.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +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_copy(ENetPacket*);
extern void enet_packet_destroy(ENetPacket*);

Expand Down Expand Up @@ -1391,6 +1392,36 @@ extern "C" {
return packet;
}

/** Attempts to resize the data in the packet to length specified in the
dataLength parameter
@param packet packet to resize
@param dataLength new size for the packet data
@returns 0 on success, < 0 on failure
*/
int enet_packet_resize(ENetPacket * packet, size_t dataLength)
{
enet_uint8 *newData = 0;

if (dataLength <= packet->dataLength || (packet->flags & ENET_PACKET_FLAG_NO_ALLOCATE))
{
packet->dataLength = dataLength;

return 0;
}

newData = (enet_uint8 *) enet_malloc(dataLength);
if (newData == NULL)
return -1;

memcpy(newData, packet->data, packet->dataLength);
enet_free(packet->data);

packet->data = newData;
packet->dataLength = dataLength;

return 0;
}

ENetPacket *enet_packet_create_offset(const void *data, size_t dataLength, size_t dataOffset, enet_uint32 flags) {
ENetPacket *packet;
if (flags & ENET_PACKET_FLAG_NO_ALLOCATE) {
Expand Down

0 comments on commit 42c03e1

Please sign in to comment.