浏览代码

checksum is now a run-time option

eihrul 15 年之前
父节点
当前提交
e49aaf7a6f
共有 5 个文件被更改,包括 44 次插入50 次删除
  1. 3 1
      ChangeLog
  2. 0 11
      configure.ac
  3. 1 0
      host.c
  4. 31 26
      include/enet/enet.h
  5. 9 12
      protocol.c

+ 3 - 1
ChangeLog

@@ -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
 totalReceivedPackets counters inside ENetHost for getting usage
 statistics

+ 0 - 11
configure.ac

@@ -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, 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
 	libenet.pc])
 AC_OUTPUT

+ 1 - 0
host.c

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

+ 31 - 26
include/enet/enet.h

@@ -284,6 +284,9 @@ typedef struct _ENetPeer
    enet_uint32   disconnectData;
 } 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.
   *
   * No fields should be modified.
@@ -294,38 +297,40 @@ typedef struct _ENetPeer
     @sa enet_host_service()
     @sa enet_host_flush()
     @sa enet_host_broadcast()
+    @sa enet_host_checksum()
     @sa enet_host_channel_limit()
     @sa enet_host_bandwidth_limit()
     @sa enet_host_bandwidth_throttle()
   */
 typedef struct _ENetHost
 {
-   ENetSocket         socket;
-   ENetAddress        address;                     /**< Internet address of the host */
-   enet_uint32        incomingBandwidth;           /**< downstream bandwidth of the host */
-   enet_uint32        outgoingBandwidth;           /**< upstream bandwidth of the host */
-   enet_uint32        bandwidthThrottleEpoch;
-   enet_uint32        mtu;
-   int                recalculateBandwidthLimits;
-   ENetPeer *         peers;                       /**< array 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 */
-   enet_uint32        serviceTime;
-   ENetList           dispatchQueue;
-   int                continueSending;
-   size_t             packetSize;
-   enet_uint16        headerFlags;
-   ENetProtocol       commands [ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS];
-   size_t             commandCount;
-   ENetBuffer         buffers [ENET_BUFFER_MAXIMUM];
-   size_t             bufferCount;
-   ENetAddress        receivedAddress;
-   enet_uint8         receivedData [ENET_PROTOCOL_MAXIMUM_MTU];
-   size_t             receivedDataLength;
-   enet_uint32        totalSentData;               /**< total data sent, 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        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 */
+   ENetSocket           socket;
+   ENetAddress          address;                     /**< Internet address of the host */
+   enet_uint32          incomingBandwidth;           /**< downstream bandwidth of the host */
+   enet_uint32          outgoingBandwidth;           /**< upstream bandwidth of the host */
+   enet_uint32          bandwidthThrottleEpoch;
+   enet_uint32          mtu;
+   int                  recalculateBandwidthLimits;
+   ENetPeer *           peers;                       /**< array 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 */
+   enet_uint32          serviceTime;
+   ENetList             dispatchQueue;
+   int                  continueSending;
+   size_t               packetSize;
+   enet_uint16          headerFlags;
+   ENetProtocol         commands [ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS];
+   size_t               commandCount;
+   ENetBuffer           buffers [ENET_BUFFER_MAXIMUM];
+   size_t               bufferCount;
+   ENetChecksumCallback checksum;
+   ENetAddress          receivedAddress;
+   enet_uint8           receivedData [ENET_PROTOCOL_MAXIMUM_MTU];
+   size_t               receivedDataLength;
+   enet_uint32          totalSentData;               /**< total data sent, 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          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;
 
 /**

+ 9 - 12
protocol.c

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