diff --git a/configure.in b/configure.in index eb6a374..145e25b 100644 --- a/configure.in +++ b/configure.in @@ -1,5 +1,5 @@ -AC_INIT(libenet, 7-6-2006) -AM_INIT_AUTOMAKE(libenet.a, 7-6-2006) +AC_INIT(libenet, 10-2-2006) +AM_INIT_AUTOMAKE(libenet.a, 10-2-2006) AC_PROG_CC AC_PROG_RANLIB diff --git a/include/enet/enet.h b/include/enet/enet.h index 24f6ef9..a19640f 100644 --- a/include/enet/enet.h +++ b/include/enet/enet.h @@ -85,6 +85,9 @@ typedef enum ENET_PACKET_FLAG_NO_ALLOCATE = (1 << 2) } ENetPacketFlag; +struct _ENetPacket; +typedef void (ENET_CALLBACK * ENetPacketFreeCallback) (struct _ENetPacket *); + /** * ENet packet structure. * @@ -101,10 +104,11 @@ typedef enum */ typedef struct _ENetPacket { - size_t referenceCount; /**< internal use only */ - enet_uint32 flags; /**< bitwise or of ENetPacketFlag constants */ - enet_uint8 * data; /**< allocated data for packet */ - size_t dataLength; /**< length of data */ + size_t referenceCount; /**< internal use only */ + enet_uint32 flags; /**< bitwise or of ENetPacketFlag constants */ + enet_uint8 * data; /**< allocated data for packet */ + size_t dataLength; /**< length of data */ + ENetPacketFreeCallback freeCallback; /**< function to be called when the packet is no longer in use */ } ENetPacket; typedef struct _ENetAcknowledgement diff --git a/packet.c b/packet.c index 77b5583..db6f2d4 100644 --- a/packet.c +++ b/packet.c @@ -21,11 +21,16 @@ enet_packet_create (const void * data, size_t dataLength, enet_uint32 flags) { ENetPacket * packet = (ENetPacket *) enet_malloc (sizeof (ENetPacket)); - if(flags & ENET_PACKET_FLAG_NO_ALLOCATE) + if (flags & ENET_PACKET_FLAG_NO_ALLOCATE) packet -> data = (enet_uint8 *) data; else { packet -> data = (enet_uint8 *) enet_malloc (dataLength); + if (packet -> data == NULL) + { + enet_free (packet); + return NULL; + }; if (data != NULL) memcpy (packet -> data, data, dataLength); @@ -34,6 +39,7 @@ enet_packet_create (const void * data, size_t dataLength, enet_uint32 flags) packet -> referenceCount = 0; packet -> flags = flags; packet -> dataLength = dataLength; + packet -> freeCallback = NULL; return packet; } @@ -44,7 +50,9 @@ enet_packet_create (const void * data, size_t dataLength, enet_uint32 flags) void enet_packet_destroy (ENetPacket * packet) { - if((packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE) == 0) + if (packet -> freeCallback != NULL) + (* packet -> freeCallback) (packet); + if (! (packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE)) enet_free (packet -> data); enet_free (packet); } @@ -68,6 +76,9 @@ enet_packet_resize (ENetPacket * packet, size_t dataLength) } newData = (enet_uint8 *) enet_malloc (dataLength); + if (newData == NULL) + return -1; + memcpy (newData, packet -> data, packet -> dataLength); enet_free (packet -> data); diff --git a/protocol.c b/protocol.c index 5684dc8..14b0d43 100644 --- a/protocol.c +++ b/protocol.c @@ -362,6 +362,8 @@ enet_protocol_handle_send_reliable (ENetHost * host, ENetPeer * peer, const ENet packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendReliable), dataLength, ENET_PACKET_FLAG_RELIABLE); + if (packet == NULL) + return -1; enet_peer_queue_incoming_command (peer, command, packet, 0); return 0; @@ -404,7 +406,9 @@ enet_protocol_handle_send_unsequenced (ENetHost * host, ENetPeer * peer, const E packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendUnsequenced), dataLength, ENET_PACKET_FLAG_UNSEQUENCED); - + if (packet == NULL) + return -1; + enet_peer_queue_incoming_command (peer, command, packet, 0); return 0; } @@ -427,6 +431,8 @@ enet_protocol_handle_send_unreliable (ENetHost * host, ENetPeer * peer, const EN packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendUnreliable), dataLength, 0); + if (packet == NULL) + return -1; enet_peer_queue_incoming_command (peer, command, packet, 0); return 0; @@ -487,7 +493,10 @@ enet_protocol_handle_send_fragment (ENetHost * host, ENetPeer * peer, const ENet if (currentCommand == enet_list_end (& channel -> incomingReliableCommands)) { ENetProtocol hostCommand = * command; - + ENetPacket * packet = enet_packet_create (NULL, totalLength, ENET_PACKET_FLAG_RELIABLE); + if (packet == NULL) + return -1; + hostCommand.header.reliableSequenceNumber = startSequenceNumber; hostCommand.sendFragment.startSequenceNumber = startSequenceNumber; hostCommand.sendFragment.dataLength = fragmentLength; @@ -496,10 +505,7 @@ enet_protocol_handle_send_fragment (ENetHost * host, ENetPeer * peer, const ENet hostCommand.sendFragment.fragmentOffset = fragmentOffset; hostCommand.sendFragment.totalLength = totalLength; - startCommand = enet_peer_queue_incoming_command (peer, - & hostCommand, - enet_packet_create (NULL, totalLength, ENET_PACKET_FLAG_RELIABLE), - fragmentCount); + startCommand = enet_peer_queue_incoming_command (peer, & hostCommand, packet, fragmentCount); } else if (totalLength != startCommand -> packet -> dataLength || @@ -1278,7 +1284,7 @@ enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch #else fprintf (stderr, #endif - "peer %u: %f%%+-%f%% packet loss, %u+-%u ms round trip time, %f%% throttle, %u/%u outgoing, %u/%u incoming\n", currentPeer -> incomingPeerID, currentPeer -> packetLoss / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> packetLossVariance / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> roundTripTime, currentPeer -> roundTripTimeVariance, currentPeer -> packetThrottle / (float) ENET_PEER_PACKET_THROTTLE_SCALE, enet_list_size (& currentPeer -> outgoingReliableCommands), enet_list_size (& currentPeer -> outgoingUnreliableCommands), currentPeer -> channels != NULL ? enet_list_size (& currentPeer -> channels -> incomingReliableCommands) : 0, enet_list_size (& currentPeer -> channels -> incomingUnreliableCommands)); + "peer %u: %f%%+-%f%% packet loss, %u+-%u ms round trip time, %f%% throttle, %u/%u outgoing, %u/%u incoming\n", currentPeer -> incomingPeerID, currentPeer -> packetLoss / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> packetLossVariance / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> roundTripTime, currentPeer -> roundTripTimeVariance, currentPeer -> packetThrottle / (float) ENET_PEER_PACKET_THROTTLE_SCALE, enet_list_size (& currentPeer -> outgoingReliableCommands), enet_list_size (& currentPeer -> outgoingUnreliableCommands), currentPeer -> channels != NULL ? enet_list_size (& currentPeer -> channels -> incomingReliableCommands) : 0, currentPeer -> channels != NULL ? enet_list_size (& currentPeer -> channels -> incomingUnreliableCommands) : 0); #endif currentPeer -> packetLossVariance -= currentPeer -> packetLossVariance / 4;