tutorial.dox 12 KB

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