checksum is now a run-time option

This commit is contained in:
eihrul 2010-05-20 16:55:27 +00:00
parent 72525fbca1
commit e49aaf7a6f
5 changed files with 44 additions and 50 deletions

View file

@ -1,5 +1,7 @@
ENet 1.2.2 (May 13, 2010): ENet 1.2.2 (May 20, 2010):
* checksum functionality is now enabled by setting a checksum callback
inside ENetHost instead of compile time option
* added totalSentData, totalSentPackets, totalReceivedData, and * added totalSentData, totalSentPackets, totalReceivedData, and
totalReceivedPackets counters inside ENetHost for getting usage totalReceivedPackets counters inside ENetHost for getting usage
statistics statistics

View file

@ -24,17 +24,6 @@ AC_CHECK_TYPE(socklen_t, [AC_DEFINE(HAS_SOCKLEN_T)], ,
AC_EGREP_HEADER(MSG_MAXIOVLEN, /usr/include/sys/socket.h, AC_DEFINE(ENET_BUFFER_MAXIMUM, [MSG_MAXIOVLEN])) AC_EGREP_HEADER(MSG_MAXIOVLEN, /usr/include/sys/socket.h, AC_DEFINE(ENET_BUFFER_MAXIMUM, [MSG_MAXIOVLEN]))
AC_EGREP_HEADER(MSG_MAXIOVLEN, socket.h, AC_DEFINE(ENET_BUFFER_MAXIMUM, [MSG_MAXIOVLEN])) AC_EGREP_HEADER(MSG_MAXIOVLEN, socket.h, AC_DEFINE(ENET_BUFFER_MAXIMUM, [MSG_MAXIOVLEN]))
AC_MSG_CHECKING(whether to use CRC32)
AC_ARG_ENABLE(crc32,
[ --enable-crc32 enable CRC32 packet verification ],
[if test "$enableval" = yes; then
AC_MSG_RESULT(yes)
AC_DEFINE(USE_CRC32)
else
AC_MSG_RESULT(no)
fi],
[AC_MSG_RESULT(no)])
AC_CONFIG_FILES([Makefile AC_CONFIG_FILES([Makefile
libenet.pc]) libenet.pc])
AC_OUTPUT AC_OUTPUT

1
host.c
View file

@ -75,6 +75,7 @@ enet_host_create (const ENetAddress * address, size_t peerCount, enet_uint32 inc
host -> peerCount = peerCount; host -> peerCount = peerCount;
host -> commandCount = 0; host -> commandCount = 0;
host -> bufferCount = 0; host -> bufferCount = 0;
host -> checksum = NULL;
host -> receivedAddress.host = ENET_HOST_ANY; host -> receivedAddress.host = ENET_HOST_ANY;
host -> receivedAddress.port = 0; host -> receivedAddress.port = 0;
host -> receivedDataLength = 0; host -> receivedDataLength = 0;

View file

@ -284,6 +284,9 @@ typedef struct _ENetPeer
enet_uint32 disconnectData; enet_uint32 disconnectData;
} ENetPeer; } ENetPeer;
/** Callback that computes the checksum of the data held in buffers [0..bufferCount-1] */
typedef enet_uint32 (ENET_CALLBACK * ENetChecksumCallback) (const ENetBuffer * buffers, size_t bufferCount);
/** An ENet host for communicating with peers. /** An ENet host for communicating with peers.
* *
* No fields should be modified. * No fields should be modified.
@ -294,38 +297,40 @@ typedef struct _ENetPeer
@sa enet_host_service() @sa enet_host_service()
@sa enet_host_flush() @sa enet_host_flush()
@sa enet_host_broadcast() @sa enet_host_broadcast()
@sa enet_host_checksum()
@sa enet_host_channel_limit() @sa enet_host_channel_limit()
@sa enet_host_bandwidth_limit() @sa enet_host_bandwidth_limit()
@sa enet_host_bandwidth_throttle() @sa enet_host_bandwidth_throttle()
*/ */
typedef struct _ENetHost typedef struct _ENetHost
{ {
ENetSocket socket; ENetSocket socket;
ENetAddress address; /**< Internet address of the host */ ENetAddress address; /**< Internet address of the host */
enet_uint32 incomingBandwidth; /**< downstream bandwidth of the host */ enet_uint32 incomingBandwidth; /**< downstream bandwidth of the host */
enet_uint32 outgoingBandwidth; /**< upstream bandwidth of the host */ enet_uint32 outgoingBandwidth; /**< upstream bandwidth of the host */
enet_uint32 bandwidthThrottleEpoch; enet_uint32 bandwidthThrottleEpoch;
enet_uint32 mtu; enet_uint32 mtu;
int recalculateBandwidthLimits; int recalculateBandwidthLimits;
ENetPeer * peers; /**< array of peers allocated for this host */ ENetPeer * peers; /**< array of peers allocated for this host */
size_t peerCount; /**< number of peers allocated for this host */ size_t peerCount; /**< number of peers allocated for this host */
size_t channelLimit; /**< maximum number of channels allowed for connected peers */ size_t channelLimit; /**< maximum number of channels allowed for connected peers */
enet_uint32 serviceTime; enet_uint32 serviceTime;
ENetList dispatchQueue; ENetList dispatchQueue;
int continueSending; int continueSending;
size_t packetSize; size_t packetSize;
enet_uint16 headerFlags; enet_uint16 headerFlags;
ENetProtocol commands [ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS]; ENetProtocol commands [ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS];
size_t commandCount; size_t commandCount;
ENetBuffer buffers [ENET_BUFFER_MAXIMUM]; ENetBuffer buffers [ENET_BUFFER_MAXIMUM];
size_t bufferCount; size_t bufferCount;
ENetAddress receivedAddress; ENetChecksumCallback checksum;
enet_uint8 receivedData [ENET_PROTOCOL_MAXIMUM_MTU]; ENetAddress receivedAddress;
size_t receivedDataLength; enet_uint8 receivedData [ENET_PROTOCOL_MAXIMUM_MTU];
enet_uint32 totalSentData; /**< total data sent, user should reset to 0 as needed to prevent overflow */ size_t receivedDataLength;
enet_uint32 totalSentPackets; /**< total UDP packets sent, user should reset to 0 as needed to prevent overflow */ enet_uint32 totalSentData; /**< total data sent, user should reset to 0 as needed to prevent overflow */
enet_uint32 totalReceivedData; /**< total data received, user should reset to 0 as needed to prevent overflow */ enet_uint32 totalSentPackets; /**< total UDP packets sent, user should reset to 0 as needed to prevent overflow */
enet_uint32 totalReceivedPackets; /**< total UDP packets received, user should reset to 0 as needed to prevent overflow */ enet_uint32 totalReceivedData; /**< total data received, user should reset to 0 as needed to prevent overflow */
enet_uint32 totalReceivedPackets; /**< total UDP packets received, user should reset to 0 as needed to prevent overflow */
} ENetHost; } ENetHost;
/** /**

View file

@ -245,9 +245,9 @@ enet_protocol_handle_connect (ENetHost * host, ENetProtocolHeader * header, ENet
ENetPeer * currentPeer; ENetPeer * currentPeer;
ENetProtocol verifyCommand; ENetProtocol verifyCommand;
#ifdef USE_CRC32 if (host -> checksum != NULL)
{ {
enet_uint32 crc = header -> checksum; enet_uint32 checksum = header -> checksum;
ENetBuffer buffer; ENetBuffer buffer;
command -> header.reliableSequenceNumber = ENET_HOST_TO_NET_16 (command -> header.reliableSequenceNumber); command -> header.reliableSequenceNumber = ENET_HOST_TO_NET_16 (command -> header.reliableSequenceNumber);
@ -257,12 +257,11 @@ enet_protocol_handle_connect (ENetHost * host, ENetProtocolHeader * header, ENet
buffer.data = host -> receivedData; buffer.data = host -> receivedData;
buffer.dataLength = host -> receivedDataLength; buffer.dataLength = host -> receivedDataLength;
if (enet_crc32 (& buffer, 1) != crc) if (host -> checksum (& buffer, 1) != checksum)
return NULL; return NULL;
command -> header.reliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> header.reliableSequenceNumber); command -> header.reliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> header.reliableSequenceNumber);
} }
#endif
channelCount = ENET_NET_TO_HOST_32 (command -> connect.channelCount); channelCount = ENET_NET_TO_HOST_32 (command -> connect.channelCount);
@ -834,9 +833,9 @@ enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
peer -> address.host != ENET_HOST_BROADCAST)) peer -> address.host != ENET_HOST_BROADCAST))
return 0; return 0;
#ifdef USE_CRC32 if (host -> checksum != NULL)
{ {
enet_uint32 crc = header -> checksum; enet_uint32 checksum = header -> checksum;
ENetBuffer buffer; ENetBuffer buffer;
header -> checksum = peer -> sessionID; header -> checksum = peer -> sessionID;
@ -844,13 +843,12 @@ enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
buffer.data = host -> receivedData; buffer.data = host -> receivedData;
buffer.dataLength = host -> receivedDataLength; buffer.dataLength = host -> receivedDataLength;
if (enet_crc32 (& buffer, 1) != crc) if (host -> checksum (& buffer, 1) != checksum)
return 0; return 0;
} }
#else else
if (header -> checksum != peer -> sessionID) if (header -> checksum != peer -> sessionID)
return 0; return 0;
#endif
peer -> address.host = host -> receivedAddress.host; peer -> address.host = host -> receivedAddress.host;
peer -> address.port = host -> receivedAddress.port; peer -> address.port = host -> receivedAddress.port;
@ -1415,9 +1413,8 @@ enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch
else else
host -> buffers -> dataLength = (size_t) & ((ENetProtocolHeader *) 0) -> sentTime; host -> buffers -> dataLength = (size_t) & ((ENetProtocolHeader *) 0) -> sentTime;
#ifdef USE_CRC32 if (host -> checksum != NULL)
header.checksum = enet_crc32 (host -> buffers, host -> bufferCount); header.checksum = host -> checksum (host -> buffers, host -> bufferCount);
#endif
currentPeer -> lastSendTime = host -> serviceTime; currentPeer -> lastSendTime = host -> serviceTime;