tutorial.dox 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. Beware that most processing of the network with the ENet stack is done
  104. inside enet_host_service(). Both hosts that make up the sides of a connection
  105. must regularly call this function to ensure packets are actually sent and
  106. received. A common symptom of not actively calling enet_host_service()
  107. on both ends is that one side receives events while the other does not.
  108. The best way to schedule this activity to ensure adequate service is, for
  109. example, to call enet_host_service() with a 0 timeout (meaning non-blocking)
  110. at the beginning of every frame in a game loop.
  111. Currently there are only four types of significant events in ENet:
  112. An event of type ENET_EVENT_TYPE_NONE is returned if no event occurred
  113. within the specified time limit. enet_host_service() will return 0
  114. with this event.
  115. An event of type ENET_EVENT_TYPE_CONNECT is returned when either a new client
  116. host has connected to the server host or when an attempt to establish a
  117. connection with a foreign host has succeeded. Only the "peer" field of the
  118. event structure is valid for this event and contains the newly connected peer.
  119. An event of type ENET_EVENT_TYPE_RECEIVE is returned when a packet is received
  120. from a connected peer. The "peer" field contains the peer the packet was
  121. received from, "channelID" is the channel on which the packet was sent, and
  122. "packet" is the packet that was sent. The packet contained in the "packet"
  123. field must be destroyed with enet_packet_destroy() when you are done
  124. inspecting its contents.
  125. An event of type ENET_EVENT_TYPE_DISCONNECT is returned when a connected peer
  126. has either explicitly disconnected or timed out. Only the "peer" field of the
  127. event structure is valid for this event and contains the peer that
  128. disconnected. Only the "data" field of the peer is still valid on a
  129. disconnect event and must be explicitly reset.
  130. @code
  131. ENetEvent event;
  132. /* Wait up to 1000 milliseconds for an event. */
  133. while (enet_host_service (client, & event, 1000) > 0)
  134. {
  135. switch (event.type)
  136. {
  137. case ENET_EVENT_TYPE_CONNECT:
  138. printf ("A new client connected from %x:%u.\n",
  139. event.peer -> address.host,
  140. event.peer -> address.port);
  141. /* Store any relevant client information here. */
  142. event.peer -> data = "Client information";
  143. break;
  144. case ENET_EVENT_TYPE_RECEIVE:
  145. printf ("A packet of length %u containing %s was received from %s on channel %u.\n",
  146. event.packet -> dataLength,
  147. event.packet -> data,
  148. event.peer -> data,
  149. event.channelID);
  150. /* Clean up the packet now that we're done using it. */
  151. enet_packet_destroy (event.packet);
  152. break;
  153. case ENET_EVENT_TYPE_DISCONNECT:
  154. printf ("%s disconnected.\n", event.peer -> data);
  155. /* Reset the peer's client information. */
  156. event.peer -> data = NULL;
  157. }
  158. }
  159. ...
  160. ...
  161. ...
  162. @endcode
  163. @section SendingPacket Sending a packet to an ENet peer
  164. Packets in ENet are created with enet_packet_create(), where the size
  165. of the packet must be specified. Optionally, initial data may be
  166. specified to copy into the packet.
  167. Certain flags may also be supplied to enet_packet_create() to control
  168. various packet features:
  169. ENET_PACKET_FLAG_RELIABLE specifies that the packet must use reliable
  170. delivery. A reliable packet is guaranteed to be delivered, and a
  171. number of retry attempts will be made until an acknowledgement is
  172. received from the foreign host the packet is sent to. If a certain
  173. number of retry attempts is reached without any acknowledgement, ENet
  174. will assume the peer has disconnected and forcefully reset the
  175. connection. If this flag is not specified, the packet is assumed an
  176. unreliable packet, and no retry attempts will be made nor
  177. acknowledgements generated.
  178. A packet may be resized (extended or truncated) with
  179. enet_packet_resize().
  180. A packet is sent to a foreign host with
  181. enet_peer_send(). enet_peer_send() accepts a channel id over which to
  182. send the packet to a given peer. Once the packet is handed over to
  183. ENet with enet_peer_send(), ENet will handle its deallocation and
  184. enet_packet_destroy() should not be used upon it.
  185. One may also use enet_host_broadcast() to send a packet to all
  186. connected peers on a given host over a specified channel id, as with
  187. enet_peer_send().
  188. Queued packets will be sent on a call to enet_host_service().
  189. Alternatively, enet_host_flush() will send out queued packets without
  190. dispatching any events.
  191. @code
  192. /* Create a reliable packet of size 7 containing "packet\0" */
  193. ENetPacket * packet = enet_packet_create ("packet",
  194. strlen ("packet") + 1,
  195. ENET_PACKET_FLAG_RELIABLE);
  196. /* Extend the packet so and append the string "foo", so it now */
  197. /* contains "packetfoo\0" */
  198. enet_packet_resize (packet, strlen ("packetfoo") + 1);
  199. strcpy (& packet -> data [strlen ("packet")], "foo");
  200. /* Send the packet to the peer over channel id 0. */
  201. /* One could also broadcast the packet by */
  202. /* enet_host_broadcast (host, 0, packet); */
  203. enet_peer_send (peer, 0, packet);
  204. ...
  205. ...
  206. ...
  207. /* One could just use enet_host_service() instead. */
  208. enet_host_flush (host);
  209. @endcode
  210. @section Disconnecting Disconnecting an ENet peer
  211. Peers may be gently disconnected with enet_peer_disconnect(). A
  212. disconnect request will be sent to the foreign host, and ENet will
  213. wait for an acknowledgement from the foreign host before finally
  214. disconnecting. An event of type ENET_EVENT_TYPE_DISCONNECT will be
  215. generated once the disconnection succeeds. Normally timeouts apply to
  216. the disconnect acknowledgement, and so if no acknowledgement is
  217. received after a length of time the peer will be forcefully
  218. disconnected.
  219. enet_peer_reset() will forcefully disconnect a peer. The foreign host
  220. will get no notification of a disconnect and will time out on the
  221. foreign host. No event is generated.
  222. @code
  223. ENetEvent event;
  224. enet_peer_disconnect (peer, 0);
  225. /* Allow up to 3 seconds for the disconnect to succeed
  226. * and drop any packets received packets.
  227. */
  228. while (enet_host_service (client, & event, 3000) > 0)
  229. {
  230. switch (event.type)
  231. {
  232. case ENET_EVENT_TYPE_RECEIVE:
  233. enet_packet_destroy (event.packet);
  234. break;
  235. case ENET_EVENT_TYPE_DISCONNECT:
  236. puts ("Disconnection succeeded.");
  237. return;
  238. ...
  239. ...
  240. ...
  241. }
  242. }
  243. /* We've arrived here, so the disconnect attempt didn't */
  244. /* succeed yet. Force the connection down. */
  245. enet_peer_reset (peer);
  246. ...
  247. ...
  248. ...
  249. @endcode
  250. @section Connecting Connecting to an ENet host
  251. A connection to a foreign host is initiated with enet_host_connect().
  252. It accepts the address of a foreign host to connect to, and the number
  253. of channels that should be allocated for communication. If N channels
  254. are allocated for use, their channel ids will be numbered 0 through
  255. N-1. A peer representing the connection attempt is returned, or NULL
  256. if there were no available peers over which to initiate the
  257. connection. When the connection attempt succeeds, an event of type
  258. ENET_EVENT_TYPE_CONNECT will be generated. If the connection attempt
  259. times out or otherwise fails, an event of type
  260. ENET_EVENT_TYPE_DISCONNECT will be generated.
  261. @code
  262. ENetAddress address;
  263. ENetEvent event;
  264. ENetPeer *peer;
  265. /* Connect to some.server.net:1234. */
  266. enet_address_set_host (& address, "some.server.net");
  267. address.port = 1234;
  268. /* Initiate the connection, allocating the two channels 0 and 1. */
  269. peer = enet_host_connect (client, & address, 2, 0);
  270. if (peer == NULL)
  271. {
  272. fprintf (stderr,
  273. "No available peers for initiating an ENet connection.\n");
  274. exit (EXIT_FAILURE);
  275. }
  276. /* Wait up to 5 seconds for the connection attempt to succeed. */
  277. if (enet_host_service (client, & event, 5000) > 0 &&
  278. event.type == ENET_EVENT_TYPE_CONNECT)
  279. {
  280. puts ("Connection to some.server.net:1234 succeeded.");
  281. ...
  282. ...
  283. ...
  284. }
  285. else
  286. {
  287. /* Either the 5 seconds are up or a disconnect event was */
  288. /* received. Reset the peer in the event the 5 seconds */
  289. /* had run out without any significant event. */
  290. enet_peer_reset (peer);
  291. puts ("Connection to some.server.net:1234 failed.");
  292. }
  293. ...
  294. ...
  295. ...
  296. @endcode
  297. */