tutorial.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. * Using ENet
  2. Before using ENet, you must call enet_initialize() to initialize the
  3. library. Upon program exit, you should call enet_deinitialize() so that
  4. the library may clean up any used resources.
  5. i.e.
  6. int
  7. main (int argc, char ** argv)
  8. {
  9. if (enet_initialize () != 0)
  10. {
  11. fprintf (stderror, "An error occurred while initializing ENet.\n");
  12. return EXIT_FAILURE;
  13. }
  14. atexit (enet_deinitialize);
  15. ...
  16. ...
  17. ...
  18. }
  19. * Creating an ENet server
  20. Servers in ENet are constructed with enet_host_create(). You must specify
  21. an address on which to receive data and new connections, as well as the maximum
  22. allowable numbers of connected peers. You may optionally specify the incoming
  23. and outgoing bandwidth of the server in bytes per second so that ENet may try
  24. to statically manage bandwidth resources among connected peers in addition to
  25. its dynamic throttling algorithm; specifying 0 for these two options will cause
  26. ENet to rely entirely upon its dynamic throttling algorithm to manage
  27. bandwidth.
  28. When done with a host, the host may be destroyed with enet_host_destroy().
  29. All connected peers to the host will be reset, and the resources used by
  30. the host will be freed.
  31. i.e.
  32. ENetAddress address;
  33. ENetHost * server;
  34. /* Bind the server to the default localhost.
  35. * A specific host address can be specified by
  36. * enet_address_set_host (& address, "x.x.x.x");
  37. */
  38. address.host = ENET_HOST_ANY;
  39. /* Bind the server to port 1234. */
  40. address.port = 1234;
  41. server = enet_host_create (& address /* the address to bind the server host to */,
  42. 32 /* allow up to 32 clients and/or outgoing connections */,
  43. 0 /* assume any amount of incoming bandwidth */,
  44. 0 /* assume any amount of outgoing bandwidth */);
  45. if (server == NULL)
  46. {
  47. fprintf (stderr,
  48. "An error occurred while trying to create an ENet server host.\n");
  49. exit (EXIT_FAILURE);
  50. }
  51. ...
  52. ...
  53. ...
  54. enet_host_destroy(server);
  55. * Creating an ENet client
  56. Clients in ENet are similarly constructed with enet_host_create() when no
  57. address is specified to bind the host to. Bandwidth may be specified for the
  58. client host as in the above example. The peer count controls the maximum number
  59. of connections to other server hosts that may be simultaneously open.
  60. i.e.
  61. ENetHost * client;
  62. clienet = enet_host_create (NULL /* create a client host */,
  63. 1 /* only allow 1 outgoing connection */,
  64. 57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
  65. 14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);
  66. if (client == NULL)
  67. {
  68. fprintf (stderr,
  69. "An error occurred while trying to create an ENet client host.\n");
  70. exit (EXIT_FAILURE);
  71. }
  72. ...
  73. ...
  74. ...
  75. enet_host_destroy(client);
  76. * Managing an ENet host
  77. ENet uses a polled event model to notify the programmer of significant
  78. events. ENet hosts are polled for events with enet_host_service(), where an
  79. optional timeout value in milliseconds may be specified to control how long
  80. ENet will poll; if a timeout of 0 is specified, enet_host_service() will
  81. return immediately if there are no events to dispatch. enet_host_service()
  82. will return 1 if an event was dispatched within the specified timeout.
  83. Currently there are only four types of significant events in ENet:
  84. An event of type ENET_EVENT_TYPE_NONE is returned if no event occurred
  85. within the specified time limit. enet_host_service() will return 0
  86. with this event.
  87. An event of type ENET_EVENT_TYPE_CONNECT is returned when either a new client
  88. host has connected to the server host or when an attempt to establish a
  89. connection with a foreign host has succeeded. Only the "peer" field of the
  90. event structure is valid for this event and contains the newly connected peer.
  91. An event of type ENET_EVENT_TYPE_RECEIVE is returned when a packet is received
  92. from a connected peer. The "peer" field contains the peer the packet was
  93. received from, "channelID" is the channel on which the packet was sent, and
  94. "packet" is the packet that was sent. The packet contained in the "packet"
  95. field must be destroyed with enet_packet_destroy() when you are done
  96. inspecting its contents.
  97. An event of type ENET_EVENT_TYPE_DISCONNECT is returned when a connected peer
  98. has either explicitly disconnected or timed out. Only the "peer" field of the
  99. event structure is valid for this event and contains the peer that
  100. disconnected. Only the "data" field of the peer is still valid on a
  101. disconnect event and must be explicitly reset.
  102. i.e.
  103. ENetEvent event;
  104. /* Wait up to 1000 milliseconds for an event. */
  105. while (enet_host_service (client, & event, 1000) > 0)
  106. {
  107. switch (event.type)
  108. {
  109. case ENET_EVENT_TYPE_CONNECT:
  110. printf ("A new client connected from %x:%u.\n",
  111. event.peer -> address.host,
  112. event.peer -> address.port);
  113. /* Store any relevant client information here. */
  114. event.peer -> data = "Client information";
  115. break;
  116. case ENET_EVENT_TYPE_RECEIVE:
  117. printf ("A packet of length %u containing %s was received from %s on channel %u.\n",
  118. event.packet -> dataLength,
  119. event.packet -> data,
  120. event.peer -> data,
  121. event.channelID);
  122. /* Clean up the packet now that we're done using it. */
  123. enet_packet_destroy (event.packet);
  124. break;
  125. case ENET_EVENT_TYPE_DISCONNECT:
  126. printf ("%s disconected.\n", event.peer -> data);
  127. /* Reset the peer's client information. */
  128. event.peer -> data = NULL;
  129. }
  130. }
  131. ...
  132. ...
  133. ...
  134. * Sending a packet to an ENet peer
  135. Packets in ENet are created with enet_packet_create(), where the size of
  136. the packet must be specified. Optionally, initial data may be specified to
  137. copy into the packet.
  138. Certain flags may also be supplied to enet_packet_create() to control
  139. various packet features:
  140. ENET_PACKET_FLAG_RELIABLE specifies that the packet must use reliable delivery.
  141. A reliable packet is guarenteed to be delivered, and a number of retry attempts
  142. will be made until an acknowledgement is received from the foreign host the
  143. packet is sent to. If a certain number of retry attempts is reached without
  144. any acknowledgement, ENet will assume the peer has disconnected and forcefully
  145. reset the connection. If this flag is not specified, the packet is assumed
  146. an unreliable packet, and no retry attempts will be made nor acknowledgements
  147. generated.
  148. A packet may be resized (extended or truncated) with enet_packet_resize().
  149. A packet is sent to a foreign host with enet_peer_send(). enet_peer_send()
  150. accepts a channel id over which to send the packet to a given peer. Once the
  151. packet is handed over to ENet with enet_peer_send(), ENet will handle its
  152. deallocation and enet_packet_destroy() should not be used upon it.
  153. One may also use enet_host_broadcast() to send a packet to all connected
  154. peers on a given host over a specified channel id, as with enet_peer_send().
  155. Queued packets will be sent on a call to enet_host_service().
  156. Alternatively, enet_host_flush() will send out queued packets without
  157. dispatching any events.
  158. i.e.
  159. /* Create a reliable packet of size 7 containing "packet\0" */
  160. ENetPacket * packet = enet_packet_create ("packet",
  161. strlen ("packet") + 1,
  162. ENET_PACKET_FLAG_RELIABLE);
  163. /* Extend the packet so and append the string "foo", so it now
  164. * contains "packetfoo\0"
  165. *
  166. enet_packet_resize (packet, strlen ("packetfoo") + 1);
  167. strcpy (& packet -> data [strlen ("packet")], "foo");
  168. /* Send the packet to the peer over channel id 3.
  169. * One could also broadcast the packet by
  170. * enet_host_broadcast (host, 3, packet);
  171. */
  172. enet_peer_send (peer, 3, packet);
  173. ...
  174. ...
  175. ...
  176. /* One could just use enet_host_service() instead. */
  177. enet_host_flush (host);
  178. * Disconnecting an ENet peer
  179. Peers may be gently disconnected with enet_peer_disconnect(). A disconnect
  180. request will be sent to the foreign host, and ENet will wait for an
  181. acknowledgement from the foreign host before finally disconnecting. An
  182. event of type ENET_EVENT_TYPE_DISCONNECT will be generated once the
  183. disconnection succeeds. Normally timeouts apply to the disconnect
  184. acknowledgement, and so if no acknowledgement is received after a length
  185. of time the peer will be forcefully disconnected.
  186. enet_peer_reset() will forcefully disconnect a peer. The foreign host
  187. will get no notification of a disconnect and will time out on the foreign
  188. host. No event is generated.
  189. i.e.
  190. ENetEvent event;
  191. enet_peer_disconnect (& client -> peers [0]);
  192. /* Allow up to 3 seconds for the disconnect to succeed
  193. * and drop any packets received packets.
  194. */
  195. while (enet_host_service (client, & event, 3000) > 0)
  196. {
  197. switch (event.type)
  198. {
  199. case ENET_EVENT_TYPE_RECEIVE:
  200. enet_packet_destroy (event.packet);
  201. break;
  202. case ENET_EVENT_TYPE_DISCONNECT:
  203. puts ("Disconnection succeeded.");
  204. return;
  205. ...
  206. ...
  207. ...
  208. }
  209. }
  210. /* We've arrived here, so the disconnect attempt didn't succeed yet.
  211. * Force the connection down.
  212. */
  213. enet_peer_reset (& client -> peers [0]);
  214. ...
  215. ...
  216. ...
  217. * Connecting to an ENet host
  218. A connection to a foregin host is initiated with enet_host_connect().
  219. It accepts the address of a foreign host to connect to, and the number of
  220. channels that should be allocated for communication. If N channels are
  221. allocated for use, their channel ids will be numbered 0 through N-1.
  222. A peer representing the connection attempt is returned, or NULL if there
  223. were no available peers over which to initiate the connection. When the
  224. connection attempt succeeds, an event of type ENET_EVENT_TYPE_CONNECT will
  225. be generated. If the connection attempt times out or otherwise fails, an
  226. event of type ENET_EVENT_TYPE_DISCONNECT will be generated.
  227. i.e.
  228. ENetAddress address;
  229. ENetEvent event;
  230. ENetPeer *peer;
  231. /* Connect to some.server.net:1234. */
  232. enet_address_set_host (& address, "some.server.net");
  233. address.port = 1234;
  234. /* Initiate the connection, allocating the two channels 0 and 1. */
  235. peer = enet_host_connect (client, & address, 2);
  236. if (peer == NULL)
  237. {
  238. fprintf (stderr,
  239. "No available peers for initiating an ENet connection.\n");
  240. exit (EXIT_FAILURE);
  241. }
  242. /* Wait up to 5 seconds for the connection attempt to succeed.
  243. if (enet_host_service (client, & event, 5000) > 0 &&
  244. event.type == ENET_EVENT_TYPE_CONNECT)
  245. {
  246. puts ("Connection to some.server.net:1234 succeeded.");
  247. ...
  248. ...
  249. ...
  250. }
  251. else
  252. {
  253. /* Either the 5 seconds are up or a disconnect event was
  254. * received. Reset the peer in the event the 5 seconds
  255. * had run out without any significant event.
  256. */
  257. enet_peer_reset (peer);
  258. puts ("Connection to some.server.net:1234 failed.");
  259. }
  260. ...
  261. ...
  262. ...