tutorial.dox 12 KB

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