1/**
2 @file host.c
3 @brief ENet host management functions
4*/
5#define ENET_BUILDING_LIB 1
6#include <string.h>
7#include "enet/enet.h"
8
9/** @defgroup host ENet host functions
10 @{
11*/
12
13/** Creates a host for communicating to peers.
14
15 @param address the address at which other peers may connect to this host. If NULL, then no peers may connect to the host.
16 @param peerCount the maximum number of peers that should be allocated for the host.
17 @param channelLimit the maximum number of channels allowed; if 0, then this is equivalent to ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT
18 @param incomingBandwidth downstream bandwidth of the host in bytes/second; if 0, ENet will assume unlimited bandwidth.
19 @param outgoingBandwidth upstream bandwidth of the host in bytes/second; if 0, ENet will assume unlimited bandwidth.
20
21 @returns the host on success and NULL on failure
22
23 @remarks ENet will strategically drop packets on specific sides of a connection between hosts
24 to ensure the host's bandwidth is not overwhelmed. The bandwidth parameters also determine
25 the window size of a connection which limits the amount of reliable packets that may be in transit
26 at any given time.
27*/
28ENetHost *
29enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelLimit, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth)
30{
31 ENetHost * host;
32 ENetPeer * currentPeer;
33
34 if (peerCount > ENET_PROTOCOL_MAXIMUM_PEER_ID)
35 return NULL;
36
37 host = (ENetHost *) enet_malloc (sizeof (ENetHost));
38 if (host == NULL)
39 return NULL;
40 memset (host, 0, sizeof (ENetHost));
41
42 host -> peers = (ENetPeer *) enet_malloc (peerCount * sizeof (ENetPeer));
43 if (host -> peers == NULL)
44 {
45 enet_free (host);
46
47 return NULL;
48 }
49 memset (host -> peers, 0, peerCount * sizeof (ENetPeer));
50
51 host -> socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM);
52 if (host -> socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket, address) < 0))
53 {
54 if (host -> socket != ENET_SOCKET_NULL)
55 enet_socket_destroy (host -> socket);
56
57 enet_free (host -> peers);
58 enet_free (host);
59
60 return NULL;
61 }
62
63 enet_socket_set_option (host -> socket, ENET_SOCKOPT_NONBLOCK, 1);
64 enet_socket_set_option (host -> socket, ENET_SOCKOPT_BROADCAST, 1);
65 enet_socket_set_option (host -> socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE);
66 enet_socket_set_option (host -> socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE);
67
68 if (address != NULL && enet_socket_get_address (host -> socket, & host -> address) < 0)
69 host -> address = * address;
70
71 if (! channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
72 channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
73 else
74 if (channelLimit < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT)
75 channelLimit = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT;
76
77 host -> randomSeed = (enet_uint32) (size_t) host;
78 host -> randomSeed += enet_host_random_seed ();
79 host -> randomSeed = (host -> randomSeed << 16) | (host -> randomSeed >> 16);
80 host -> channelLimit = channelLimit;
81 host -> incomingBandwidth = incomingBandwidth;
82 host -> outgoingBandwidth = outgoingBandwidth;
83 host -> bandwidthThrottleEpoch = 0;
84 host -> recalculateBandwidthLimits = 0;
85 host -> mtu = ENET_HOST_DEFAULT_MTU;
86 host -> peerCount = peerCount;
87 host -> commandCount = 0;
88 host -> bufferCount = 0;
89 host -> checksum = NULL;
90 memset(host -> receivedAddress.host, 0, 16);
91 host -> receivedAddress.port = 0;
92 host -> receivedData = NULL;
93 host -> receivedDataLength = 0;
94
95 host -> totalSentData = 0;
96 host -> totalSentPackets = 0;
97 host -> totalReceivedData = 0;
98 host -> totalReceivedPackets = 0;
99 host -> totalQueued = 0;
100
101 host -> connectedPeers = 0;
102 host -> bandwidthLimitedPeers = 0;
103 host -> duplicatePeers = ENET_PROTOCOL_MAXIMUM_PEER_ID;
104 host -> maximumPacketSize = ENET_HOST_DEFAULT_MAXIMUM_PACKET_SIZE;
105 host -> maximumWaitingData = ENET_HOST_DEFAULT_MAXIMUM_WAITING_DATA;
106
107 host -> compressor.context = NULL;
108 host -> compressor.compress = NULL;
109 host -> compressor.decompress = NULL;
110 host -> compressor.destroy = NULL;
111
112 host -> intercept = NULL;
113
114 enet_list_clear (& host -> dispatchQueue);
115
116 for (currentPeer = host -> peers;
117 currentPeer < & host -> peers [host -> peerCount];
118 ++ currentPeer)
119 {
120 currentPeer -> host = host;
121 currentPeer -> incomingPeerID = currentPeer - host -> peers;
122 currentPeer -> outgoingSessionID = currentPeer -> incomingSessionID = 0xFF;
123 currentPeer -> data = NULL;
124
125 enet_list_clear (& currentPeer -> acknowledgements);
126 enet_list_clear (& currentPeer -> sentReliableCommands);
127 enet_list_clear (& currentPeer -> outgoingCommands);
128 enet_list_clear (& currentPeer -> outgoingSendReliableCommands);
129 enet_list_clear (& currentPeer -> dispatchedCommands);
130
131 enet_peer_reset (currentPeer);
132 }
133
134 return host;
135}
136
137/** Destroys the host and all resources associated with it.
138 @param host pointer to the host to destroy
139*/
140void
141enet_host_destroy (ENetHost * host)
142{
143 ENetPeer * currentPeer;
144
145 if (host == NULL)
146 return;
147
148 enet_socket_destroy (host -> socket);
149
150 for (currentPeer = host -> peers;
151 currentPeer < & host -> peers [host -> peerCount];
152 ++ currentPeer)
153 {
154 enet_peer_reset (currentPeer);
155 }
156
157 if (host -> compressor.context != NULL && host -> compressor.destroy)
158 (* host -> compressor.destroy) (host -> compressor.context);
159
160 enet_free (host -> peers);
161 enet_free (host);
162}
163
164enet_uint32
165enet_host_random (ENetHost * host)
166{
167 /* Mulberry32 by Tommy Ettinger */
168 enet_uint32 n = (host -> randomSeed += 0x6D2B79F5U);
169 n = (n ^ (n >> 15)) * (n | 1U);
170 n ^= n + (n ^ (n >> 7)) * (n | 61U);
171 return n ^ (n >> 14);
172}
173
174/** Initiates a connection to a foreign host.
175 @param host host seeking the connection
176 @param address destination for the connection
177 @param channelCount number of channels to allocate
178 @param data user data supplied to the receiving host
179 @returns a peer representing the foreign host on success, NULL on failure
180 @remarks The peer returned will have not completed the connection until enet_host_service()
181 notifies of an ENET_EVENT_TYPE_CONNECT event for the peer.
182*/
183ENetPeer *
184enet_host_connect (ENetHost * host, const ENetAddress * address, size_t channelCount, enet_uint32 data)
185{
186 ENetPeer * currentPeer;
187 ENetChannel * channel;
188 ENetProtocol command;
189
190 if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT)
191 channelCount = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT;
192 else
193 if (channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
194 channelCount = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
195
196 for (currentPeer = host -> peers;
197 currentPeer < & host -> peers [host -> peerCount];
198 ++ currentPeer)
199 {
200 if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED)
201 break;
202 }
203
204 if (currentPeer >= & host -> peers [host -> peerCount])
205 return NULL;
206
207 currentPeer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel));
208 if (currentPeer -> channels == NULL)
209 return NULL;
210 currentPeer -> channelCount = channelCount;
211 currentPeer -> state = ENET_PEER_STATE_CONNECTING;
212 currentPeer -> address = * address;
213 currentPeer -> connectID = enet_host_random (host);
214 currentPeer -> mtu = host -> mtu;
215
216 if (host -> outgoingBandwidth == 0)
217 currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
218 else
219 currentPeer -> windowSize = (host -> outgoingBandwidth /
220 ENET_PEER_WINDOW_SIZE_SCALE) *
221 ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
222
223 if (currentPeer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
224 currentPeer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
225 else
226 if (currentPeer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
227 currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
228
229 for (channel = currentPeer -> channels;
230 channel < & currentPeer -> channels [channelCount];
231 ++ channel)
232 {
233 channel -> outgoingReliableSequenceNumber = 0;
234 channel -> outgoingUnreliableSequenceNumber = 0;
235 channel -> incomingReliableSequenceNumber = 0;
236 channel -> incomingUnreliableSequenceNumber = 0;
237
238 enet_list_clear (& channel -> incomingReliableCommands);
239 enet_list_clear (& channel -> incomingUnreliableCommands);
240
241 channel -> usedReliableWindows = 0;
242 memset (channel -> reliableWindows, 0, sizeof (channel -> reliableWindows));
243 }
244
245 command.header.command = ENET_PROTOCOL_COMMAND_CONNECT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
246 command.header.channelID = 0xFF;
247 command.connect.outgoingPeerID = ENET_HOST_TO_NET_16 (currentPeer -> incomingPeerID);
248 command.connect.incomingSessionID = currentPeer -> incomingSessionID;
249 command.connect.outgoingSessionID = currentPeer -> outgoingSessionID;
250 command.connect.mtu = ENET_HOST_TO_NET_32 (currentPeer -> mtu);
251 command.connect.windowSize = ENET_HOST_TO_NET_32 (currentPeer -> windowSize);
252 command.connect.channelCount = ENET_HOST_TO_NET_32 (channelCount);
253 command.connect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth);
254 command.connect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
255 command.connect.packetThrottleInterval = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleInterval);
256 command.connect.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleAcceleration);
257 command.connect.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration);
258 command.connect.connectID = currentPeer -> connectID;
259 command.connect.data = ENET_HOST_TO_NET_32 (data);
260
261 enet_peer_queue_outgoing_command (currentPeer, & command, NULL, 0, 0);
262
263 return currentPeer;
264}
265
266/** Queues a packet to be sent to all peers associated with the host.
267 @param host host on which to broadcast the packet
268 @param channelID channel on which to broadcast
269 @param packet packet to broadcast
270*/
271void
272enet_host_broadcast (ENetHost * host, enet_uint8 channelID, ENetPacket * packet)
273{
274 ENetPeer * currentPeer;
275
276 for (currentPeer = host -> peers;
277 currentPeer < & host -> peers [host -> peerCount];
278 ++ currentPeer)
279 {
280 if (currentPeer -> state != ENET_PEER_STATE_CONNECTED)
281 continue;
282
283 enet_peer_send (currentPeer, channelID, packet);
284 }
285
286 if (packet -> referenceCount == 0)
287 enet_packet_destroy (packet);
288}
289
290/** Sets the packet compressor the host should use to compress and decompress packets.
291 @param host host to enable or disable compression for
292 @param compressor callbacks for for the packet compressor; if NULL, then compression is disabled
293*/
294void
295enet_host_compress (ENetHost * host, const ENetCompressor * compressor)
296{
297 if (host -> compressor.context != NULL && host -> compressor.destroy)
298 (* host -> compressor.destroy) (host -> compressor.context);
299
300 if (compressor)
301 host -> compressor = * compressor;
302 else
303 host -> compressor.context = NULL;
304}
305
306/** Limits the maximum allowed channels of future incoming connections.
307 @param host host to limit
308 @param channelLimit the maximum number of channels allowed; if 0, then this is equivalent to ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT
309*/
310void
311enet_host_channel_limit (ENetHost * host, size_t channelLimit)
312{
313 if (! channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
314 channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
315 else
316 if (channelLimit < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT)
317 channelLimit = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT;
318
319 host -> channelLimit = channelLimit;
320}
321
322
323/** Adjusts the bandwidth limits of a host.
324 @param host host to adjust
325 @param incomingBandwidth new incoming bandwidth
326 @param outgoingBandwidth new outgoing bandwidth
327 @remarks the incoming and outgoing bandwidth parameters are identical in function to those
328 specified in enet_host_create().
329*/
330void
331enet_host_bandwidth_limit (ENetHost * host, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth)
332{
333 host -> incomingBandwidth = incomingBandwidth;
334 host -> outgoingBandwidth = outgoingBandwidth;
335 host -> recalculateBandwidthLimits = 1;
336}
337
338void
339enet_host_bandwidth_throttle (ENetHost * host)
340{
341 enet_uint32 timeCurrent = enet_time_get (),
342 elapsedTime = timeCurrent - host -> bandwidthThrottleEpoch,
343 peersRemaining = (enet_uint32) host -> connectedPeers,
344 dataTotal = ~0,
345 bandwidth = ~0,
346 throttle = 0,
347 bandwidthLimit = 0;
348 int needsAdjustment = host -> bandwidthLimitedPeers > 0 ? 1 : 0;
349 ENetPeer * peer;
350 ENetProtocol command;
351
352 if (elapsedTime < ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
353 return;
354
355 host -> bandwidthThrottleEpoch = timeCurrent;
356
357 if (peersRemaining == 0)
358 return;
359
360 if (host -> outgoingBandwidth != 0)
361 {
362 dataTotal = 0;
363 bandwidth = (host -> outgoingBandwidth * elapsedTime) / 1000;
364
365 for (peer = host -> peers;
366 peer < & host -> peers [host -> peerCount];
367 ++ peer)
368 {
369 if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
370 continue;
371
372 dataTotal += peer -> outgoingDataTotal;
373 }
374 }
375
376 while (peersRemaining > 0 && needsAdjustment != 0)
377 {
378 needsAdjustment = 0;
379
380 if (dataTotal <= bandwidth)
381 throttle = ENET_PEER_PACKET_THROTTLE_SCALE;
382 else
383 throttle = (bandwidth * ENET_PEER_PACKET_THROTTLE_SCALE) / dataTotal;
384
385 for (peer = host -> peers;
386 peer < & host -> peers [host -> peerCount];
387 ++ peer)
388 {
389 enet_uint32 peerBandwidth;
390
391 if ((peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER) ||
392 peer -> incomingBandwidth == 0 ||
393 peer -> outgoingBandwidthThrottleEpoch == timeCurrent)
394 continue;
395
396 peerBandwidth = (peer -> incomingBandwidth * elapsedTime) / 1000;
397 if ((throttle * peer -> outgoingDataTotal) / ENET_PEER_PACKET_THROTTLE_SCALE <= peerBandwidth)
398 continue;
399
400 peer -> packetThrottleLimit = (peerBandwidth *
401 ENET_PEER_PACKET_THROTTLE_SCALE) / peer -> outgoingDataTotal;
402
403 if (peer -> packetThrottleLimit == 0)
404 peer -> packetThrottleLimit = 1;
405
406 if (peer -> packetThrottle > peer -> packetThrottleLimit)
407 peer -> packetThrottle = peer -> packetThrottleLimit;
408
409 peer -> outgoingBandwidthThrottleEpoch = timeCurrent;
410
411 peer -> incomingDataTotal = 0;
412 peer -> outgoingDataTotal = 0;
413
414 needsAdjustment = 1;
415 -- peersRemaining;
416 bandwidth -= peerBandwidth;
417 dataTotal -= peerBandwidth;
418 }
419 }
420
421 if (peersRemaining > 0)
422 {
423 if (dataTotal <= bandwidth)
424 throttle = ENET_PEER_PACKET_THROTTLE_SCALE;
425 else
426 throttle = (bandwidth * ENET_PEER_PACKET_THROTTLE_SCALE) / dataTotal;
427
428 for (peer = host -> peers;
429 peer < & host -> peers [host -> peerCount];
430 ++ peer)
431 {
432 if ((peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER) ||
433 peer -> outgoingBandwidthThrottleEpoch == timeCurrent)
434 continue;
435
436 peer -> packetThrottleLimit = throttle;
437
438 if (peer -> packetThrottle > peer -> packetThrottleLimit)
439 peer -> packetThrottle = peer -> packetThrottleLimit;
440
441 peer -> incomingDataTotal = 0;
442 peer -> outgoingDataTotal = 0;
443 }
444 }
445
446 if (host -> recalculateBandwidthLimits)
447 {
448 host -> recalculateBandwidthLimits = 0;
449
450 peersRemaining = (enet_uint32) host -> connectedPeers;
451 bandwidth = host -> incomingBandwidth;
452 needsAdjustment = 1;
453
454 if (bandwidth == 0)
455 bandwidthLimit = 0;
456 else
457 while (peersRemaining > 0 && needsAdjustment != 0)
458 {
459 needsAdjustment = 0;
460 bandwidthLimit = bandwidth / peersRemaining;
461
462 for (peer = host -> peers;
463 peer < & host -> peers [host -> peerCount];
464 ++ peer)
465 {
466 if ((peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER) ||
467 peer -> incomingBandwidthThrottleEpoch == timeCurrent)
468 continue;
469
470 if (peer -> outgoingBandwidth > 0 &&
471 peer -> outgoingBandwidth >= bandwidthLimit)
472 continue;
473
474 peer -> incomingBandwidthThrottleEpoch = timeCurrent;
475
476 needsAdjustment = 1;
477 -- peersRemaining;
478 bandwidth -= peer -> outgoingBandwidth;
479 }
480 }
481
482 for (peer = host -> peers;
483 peer < & host -> peers [host -> peerCount];
484 ++ peer)
485 {
486 if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
487 continue;
488
489 command.header.command = ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
490 command.header.channelID = 0xFF;
491 command.bandwidthLimit.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
492
493 if (peer -> incomingBandwidthThrottleEpoch == timeCurrent)
494 command.bandwidthLimit.incomingBandwidth = ENET_HOST_TO_NET_32 (peer -> outgoingBandwidth);
495 else
496 command.bandwidthLimit.incomingBandwidth = ENET_HOST_TO_NET_32 (bandwidthLimit);
497
498 enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
499 }
500 }
501}
502
503/** @} */
504