added enet_host_channel_limit

This commit is contained in:
eihrul 2010-05-14 18:20:20 +00:00
parent c4138503f9
commit c5189c090f
4 changed files with 31 additions and 1 deletions

View file

@ -1,5 +1,7 @@
ENet 1.2.2 (May 13, 2010): ENet 1.2.2 (May 13, 2010):
* added enet_host_channel_limit() for limiting the maximum number of
channels allowed by connected peers
* now uses dispatch queues for event dispatch rather than potentially * now uses dispatch queues for event dispatch rather than potentially
unscalable array walking unscalable array walking
* added no_memory callback that is called when a malloc attempt fails, * added no_memory callback that is called when a malloc attempt fails,

17
host.c
View file

@ -66,6 +66,7 @@ enet_host_create (const ENetAddress * address, size_t peerCount, enet_uint32 inc
if (address != NULL) if (address != NULL)
host -> address = * address; host -> address = * address;
host -> channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
host -> incomingBandwidth = incomingBandwidth; host -> incomingBandwidth = incomingBandwidth;
host -> outgoingBandwidth = outgoingBandwidth; host -> outgoingBandwidth = outgoingBandwidth;
host -> bandwidthThrottleEpoch = 0; host -> bandwidthThrottleEpoch = 0;
@ -208,6 +209,22 @@ enet_host_connect (ENetHost * host, const ENetAddress * address, size_t channelC
return currentPeer; return currentPeer;
} }
/** Limits the maximum allowed channels of future incoming connections.
@param host host to limit
@param channelLimit the maximum number of channels allowed; if 0, then this is equivalent to ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT
*/
void
enet_host_channel_limit (ENetHost * host, size_t channelLimit)
{
if (! channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
else
if (channelLimit < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT)
channelLimit = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT;
host -> channelLimit = channelLimit;
}
/** Queues a packet to be sent to all peers associated with the host. /** Queues a packet to be sent to all peers associated with the host.
@param host host on which to broadcast the packet @param host host on which to broadcast the packet
@param channelID channel on which to broadcast @param channelID channel on which to broadcast

View file

@ -294,6 +294,7 @@ 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_channel_limit()
@sa enet_host_bandwidth_limit() @sa enet_host_bandwidth_limit()
@sa enet_host_bandwidth_throttle() @sa enet_host_bandwidth_throttle()
*/ */
@ -308,6 +309,7 @@ typedef struct _ENetHost
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 */
enet_uint32 serviceTime; enet_uint32 serviceTime;
ENetList dispatchQueue; ENetList dispatchQueue;
int continueSending; int continueSending;
@ -471,6 +473,7 @@ ENET_API int enet_host_check_events (ENetHost *, ENetEvent *);
ENET_API int enet_host_service (ENetHost *, ENetEvent *, enet_uint32); ENET_API int enet_host_service (ENetHost *, ENetEvent *, enet_uint32);
ENET_API void enet_host_flush (ENetHost *); ENET_API void enet_host_flush (ENetHost *);
ENET_API void enet_host_broadcast (ENetHost *, enet_uint8, ENetPacket *); ENET_API void enet_host_broadcast (ENetHost *, enet_uint8, ENetPacket *);
ENET_API void enet_host_channel_limit (ENetHost *, size_t);
ENET_API void enet_host_bandwidth_limit (ENetHost *, enet_uint32, enet_uint32); ENET_API void enet_host_bandwidth_limit (ENetHost *, enet_uint32, enet_uint32);
extern void enet_host_bandwidth_throttle (ENetHost *); extern void enet_host_bandwidth_throttle (ENetHost *);

View file

@ -292,6 +292,8 @@ enet_protocol_handle_connect (ENetHost * host, ENetProtocolHeader * header, ENet
if (currentPeer >= & host -> peers [host -> peerCount]) if (currentPeer >= & host -> peers [host -> peerCount])
return NULL; return NULL;
if (channelCount > host -> channelLimit)
channelCount = host -> channelLimit;
currentPeer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel)); currentPeer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel));
if (currentPeer -> channels == NULL) if (currentPeer -> channels == NULL)
return NULL; return NULL;
@ -745,11 +747,14 @@ enet_protocol_handle_verify_connect (ENetHost * host, ENetEvent * event, ENetPee
{ {
enet_uint16 mtu; enet_uint16 mtu;
enet_uint32 windowSize; enet_uint32 windowSize;
size_t channelCount;
if (peer -> state != ENET_PEER_STATE_CONNECTING) if (peer -> state != ENET_PEER_STATE_CONNECTING)
return 0; return 0;
if (ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount) != peer -> channelCount || channelCount = ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount);
if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT || channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT ||
ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleInterval) != peer -> packetThrottleInterval || ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleInterval) != peer -> packetThrottleInterval ||
ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleAcceleration) != peer -> packetThrottleAcceleration || ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleAcceleration) != peer -> packetThrottleAcceleration ||
ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleDeceleration) != peer -> packetThrottleDeceleration) ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleDeceleration) != peer -> packetThrottleDeceleration)
@ -761,6 +766,9 @@ enet_protocol_handle_verify_connect (ENetHost * host, ENetEvent * event, ENetPee
enet_protocol_remove_sent_reliable_command (peer, 1, 0xFF); enet_protocol_remove_sent_reliable_command (peer, 1, 0xFF);
if (channelCount < peer -> channelCount)
peer -> channelCount = channelCount;
peer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> verifyConnect.outgoingPeerID); peer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> verifyConnect.outgoingPeerID);
mtu = ENET_NET_TO_HOST_16 (command -> verifyConnect.mtu); mtu = ENET_NET_TO_HOST_16 (command -> verifyConnect.mtu);