tutorial.dox 12 KB

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