/src/middleware/enet/docs/Tutorial.html
https://bitbucket.org/vivkin/gam3b00bs/ · HTML · 261 lines · 234 code · 26 blank · 1 comment · 0 complexity · 5d3a2b8a91c3196ddf7a7f32f846074b MD5 · raw file
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
- <title>enet: Tutorial</title>
- <link href="tabs.css" rel="stylesheet" type="text/css"/>
- <link href="doxygen.css" rel="stylesheet" type="text/css"/>
- </head>
- <body>
- <!-- Generated by Doxygen 1.7.1 -->
- <div class="navigation" id="top">
- <div class="tabs">
- <ul class="tablist">
- <li><a href="index.html"><span>Main Page</span></a></li>
- <li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
- <li><a href="modules.html"><span>Modules</span></a></li>
- <li><a href="annotated.html"><span>Data Structures</span></a></li>
- <li><a href="files.html"><span>Files</span></a></li>
- </ul>
- </div>
- </div>
- <div class="header">
- <div class="headertitle">
- <h1>Tutorial </h1> </div>
- </div>
- <div class="contents">
- <p><a class="el" href="Tutorial.html#Initialization">Initialization</a></p>
- <p><a class="el" href="Tutorial.html#CreateServer">Creating an ENet server</a></p>
- <p><a class="el" href="Tutorial.html#CreateClient">Creating an ENet client</a></p>
- <p><a class="el" href="Tutorial.html#ManageHost">Managing an ENet host</a></p>
- <p><a class="el" href="Tutorial.html#SendingPacket">Sending a packet to an ENet peer</a></p>
- <p><a class="el" href="Tutorial.html#Disconnecting">Disconnecting an ENet peer</a></p>
- <p><a class="el" href="Tutorial.html#Connecting">Connecting to an ENet host</a></p>
- <h2><a class="anchor" id="Initialization"></a>
- Initialization</h2>
- <p>You should include the file <<a class="el" href="enet_8h.html" title="ENet public header file.">enet/enet.h</a>> when using ENet. Do not include <<a class="el" href="enet_8h.html" title="ENet public header file.">enet.h</a>> without the directory prefix, as this may cause file name conflicts on some systems.</p>
- <p>Before using ENet, you must call <a class="el" href="group__global.html#ga67fa85c46a1dc91f968f25fc0637c897" title="Initializes ENet globally.">enet_initialize()</a> to initialize the library. Upon program exit, you should call <a class="el" href="group__global.html#gad62cf417e08a2b98d48572a336f7db25" title="Shuts down ENet globally.">enet_deinitialize()</a> so that the library may clean up any used resources.</p>
- <div class="fragment"><pre class="fragment"><span class="preprocessor">#include <<a class="code" href="enet_8h.html" title="ENet public header file.">enet/enet.h</a>></span>
- <span class="keywordtype">int</span>
- main (<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> ** argv)
- {
- <span class="keywordflow">if</span> (<a class="code" href="group__global.html#ga67fa85c46a1dc91f968f25fc0637c897" title="Initializes ENet globally.">enet_initialize</a> () != 0)
- {
- fprintf (stderr, <span class="stringliteral">"An error occurred while initializing ENet.\n"</span>);
- <span class="keywordflow">return</span> EXIT_FAILURE;
- }
- atexit (<a class="code" href="group__global.html#gad62cf417e08a2b98d48572a336f7db25" title="Shuts down ENet globally.">enet_deinitialize</a>);
- ...
- ...
- ...
- }
- </pre></div><h2><a class="anchor" id="CreateServer"></a>
- Creating an ENet server</h2>
- <p>Servers in ENet are constructed with <a class="el" href="group__host.html#ga5567a95d7a45521dc9cba93a9066c940" title="Creates a host for communicating to peers.">enet_host_create()</a>. You must specify an address on which to receive data and new connections, as well as the maximum allowable numbers of connected peers. You may optionally specify the incoming and outgoing bandwidth of the server in bytes per second so that ENet may try to statically manage bandwidth resources among connected peers in addition to its dynamic throttling algorithm; specifying 0 for these two options will cause ENet to rely entirely upon its dynamic throttling algorithm to manage bandwidth.</p>
- <p>When done with a host, the host may be destroyed with <a class="el" href="group__host.html#gacec1e9a0b528c1f9ce30544d2d5b5b79" title="Destroys the host and all resources associated with it.">enet_host_destroy()</a>. All connected peers to the host will be reset, and the resources used by the host will be freed.</p>
- <div class="fragment"><pre class="fragment"> <a class="code" href="struct__ENetAddress.html" title="Portable internet address structure.">ENetAddress</a> address;
- <a class="code" href="struct__ENetHost.html" title="An ENet host for communicating with peers.">ENetHost</a> * server;
- <span class="comment">/* Bind the server to the default localhost. */</span>
- <span class="comment">/* A specific host address can be specified by */</span>
- <span class="comment">/* enet_address_set_host (& address, "x.x.x.x"); */</span>
- address.host = ENET_HOST_ANY;
- <span class="comment">/* Bind the server to port 1234. */</span>
- address.port = 1234;
- server = <a class="code" href="group__host.html#ga5567a95d7a45521dc9cba93a9066c940" title="Creates a host for communicating to peers.">enet_host_create</a> (& address <span class="comment">/* the address to bind the server host to */</span>,
- 32 <span class="comment">/* allow up to 32 clients and/or outgoing connections */</span>,
- 2 <span class="comment">/* allow up to 2 channels to be used, 0 and 1 */</span>,
- 0 <span class="comment">/* assume any amount of incoming bandwidth */</span>,
- 0 <span class="comment">/* assume any amount of outgoing bandwidth */</span>);
- <span class="keywordflow">if</span> (server == NULL)
- {
- fprintf (stderr,
- <span class="stringliteral">"An error occurred while trying to create an ENet server host.\n"</span>);
- exit (EXIT_FAILURE);
- }
- ...
- ...
- ...
- <a class="code" href="group__host.html#gacec1e9a0b528c1f9ce30544d2d5b5b79" title="Destroys the host and all resources associated with it.">enet_host_destroy</a>(server);
- </pre></div><h2><a class="anchor" id="CreateClient"></a>
- Creating an ENet client</h2>
- <p>Clients in ENet are similarly constructed with <a class="el" href="group__host.html#ga5567a95d7a45521dc9cba93a9066c940" title="Creates a host for communicating to peers.">enet_host_create()</a> when no address is specified to bind the host to. Bandwidth may be specified for the client host as in the above example. The peer count controls the maximum number of connections to other server hosts that may be simultaneously open.</p>
- <div class="fragment"><pre class="fragment"> <a class="code" href="struct__ENetHost.html" title="An ENet host for communicating with peers.">ENetHost</a> * client;
- client = <a class="code" href="group__host.html#ga5567a95d7a45521dc9cba93a9066c940" title="Creates a host for communicating to peers.">enet_host_create</a> (NULL <span class="comment">/* create a client host */</span>,
- 1 <span class="comment">/* only allow 1 outgoing connection */</span>,
- 2 <span class="comment">/* allow up 2 channels to be used, 0 and 1 */</span>,
- 57600 / 8 <span class="comment">/* 56K modem with 56 Kbps downstream bandwidth */</span>,
- 14400 / 8 <span class="comment">/* 56K modem with 14 Kbps upstream bandwidth */</span>);
- <span class="keywordflow">if</span> (client == NULL)
- {
- fprintf (stderr,
- <span class="stringliteral">"An error occurred while trying to create an ENet client host.\n"</span>);
- exit (EXIT_FAILURE);
- }
- ...
- ...
- ...
- <a class="code" href="group__host.html#gacec1e9a0b528c1f9ce30544d2d5b5b79" title="Destroys the host and all resources associated with it.">enet_host_destroy</a>(client);
- </pre></div><h2><a class="anchor" id="ManageHost"></a>
- Managing an ENet host</h2>
- <p>ENet uses a polled event model to notify the programmer of significant events. ENet hosts are polled for events with <a class="el" href="group__host.html#ga6ba501b3ee576e5578c8e6d1694ebd49" title="Waits for events on the host specified and shuttles packets between the host and its peers...">enet_host_service()</a>, where an optional timeout value in milliseconds may be specified to control how long ENet will poll; if a timeout of 0 is specified, <a class="el" href="group__host.html#ga6ba501b3ee576e5578c8e6d1694ebd49" title="Waits for events on the host specified and shuttles packets between the host and its peers...">enet_host_service()</a> will return immediately if there are no events to dispatch. <a class="el" href="group__host.html#ga6ba501b3ee576e5578c8e6d1694ebd49" title="Waits for events on the host specified and shuttles packets between the host and its peers...">enet_host_service()</a> will return 1 if an event was dispatched within the specified timeout.</p>
- <p>Currently there are only four types of significant events in ENet:</p>
- <p>An event of type ENET_EVENT_TYPE_NONE is returned if no event occurred within the specified time limit. <a class="el" href="group__host.html#ga6ba501b3ee576e5578c8e6d1694ebd49" title="Waits for events on the host specified and shuttles packets between the host and its peers...">enet_host_service()</a> will return 0 with this event.</p>
- <p>An event of type ENET_EVENT_TYPE_CONNECT is returned when either a new client host has connected to the server host or when an attempt to establish a connection with a foreign host has succeeded. Only the "peer" field of the event structure is valid for this event and contains the newly connected peer.</p>
- <p>An event of type ENET_EVENT_TYPE_RECEIVE is returned when a packet is received from a connected peer. The "peer" field contains the peer the packet was received from, "channelID" is the channel on which the packet was sent, and "packet" is the packet that was sent. The packet contained in the "packet" field must be destroyed with <a class="el" href="group__Packet.html#gab58895376ee4ade8f4e13761a44ba263" title="Destroys the packet and deallocates its data.">enet_packet_destroy()</a> when you are done inspecting its contents.</p>
- <p>An event of type ENET_EVENT_TYPE_DISCONNECT is returned when a connected peer has either explicitly disconnected or timed out. Only the "peer" field of the event structure is valid for this event and contains the peer that disconnected. Only the "data" field of the peer is still valid on a disconnect event and must be explicitly reset.</p>
- <div class="fragment"><pre class="fragment"> <a class="code" href="struct__ENetEvent.html" title="An ENet event as returned by enet_host_service().">ENetEvent</a> event;
-
- <span class="comment">/* Wait up to 1000 milliseconds for an event. */</span>
- <span class="keywordflow">while</span> (<a class="code" href="group__host.html#ga6ba501b3ee576e5578c8e6d1694ebd49" title="Waits for events on the host specified and shuttles packets between the host and its peers...">enet_host_service</a> (client, & event, 1000) > 0)
- {
- <span class="keywordflow">switch</span> (event.<a class="code" href="struct__ENetEvent.html#a53cd59a74e46de03e8f4b3fd47822d96" title="type of the event">type</a>)
- {
- <span class="keywordflow">case</span> <a class="code" href="enet_8h.html#a3cf937b5cf72510493e0a7a2b71d3755aefd9fa36297e41ca4c1cbcfdeb7e4a9d" title="a connection request initiated by enet_host_connect has completed.">ENET_EVENT_TYPE_CONNECT</a>:
- printf (<span class="stringliteral">"A new client connected from %x:%u.\n"</span>,
- event.<a class="code" href="struct__ENetEvent.html#ac991d0db800bc1c70b56ad63f1670140" title="peer that generated a connect, disconnect or receive event">peer</a> -> address.host,
- event.<a class="code" href="struct__ENetEvent.html#ac991d0db800bc1c70b56ad63f1670140" title="peer that generated a connect, disconnect or receive event">peer</a> -> address.port);
- <span class="comment">/* Store any relevant client information here. */</span>
- <span class="keyword">event</span>.peer -> data = <span class="stringliteral">"Client information"</span>;
- <span class="keywordflow">break</span>;
- <span class="keywordflow">case</span> <a class="code" href="enet_8h.html#a3cf937b5cf72510493e0a7a2b71d3755a67d928ca38b289db53ec9f56c91c5d9d" title="a packet has been received from a peer.">ENET_EVENT_TYPE_RECEIVE</a>:
- printf (<span class="stringliteral">"A packet of length %u containing %s was received from %s on channel %u.\n"</span>,
- event.<a class="code" href="struct__ENetEvent.html#afb6303a5593fce6b9671efbc2ca1b5de" title="packet associated with the event, if appropriate">packet</a> -> dataLength,
- event.<a class="code" href="struct__ENetEvent.html#afb6303a5593fce6b9671efbc2ca1b5de" title="packet associated with the event, if appropriate">packet</a> -> data,
- event.<a class="code" href="struct__ENetEvent.html#ac991d0db800bc1c70b56ad63f1670140" title="peer that generated a connect, disconnect or receive event">peer</a> -> data,
- event.<a class="code" href="struct__ENetEvent.html#a9d82e67a0f26c05de4b39bc839cb36ec" title="channel on the peer that generated the event, if appropriate">channelID</a>);
- <span class="comment">/* Clean up the packet now that we're done using it. */</span>
- <a class="code" href="group__Packet.html#gab58895376ee4ade8f4e13761a44ba263" title="Destroys the packet and deallocates its data.">enet_packet_destroy</a> (event.<a class="code" href="struct__ENetEvent.html#afb6303a5593fce6b9671efbc2ca1b5de" title="packet associated with the event, if appropriate">packet</a>);
-
- <span class="keywordflow">break</span>;
-
- <span class="keywordflow">case</span> <a class="code" href="enet_8h.html#a3cf937b5cf72510493e0a7a2b71d3755a4fa47af84cf901810510aeba077a1c2f" title="a peer has disconnected.">ENET_EVENT_TYPE_DISCONNECT</a>:
- printf (<span class="stringliteral">"%s disconected.\n"</span>, event.<a class="code" href="struct__ENetEvent.html#ac991d0db800bc1c70b56ad63f1670140" title="peer that generated a connect, disconnect or receive event">peer</a> -> data);
- <span class="comment">/* Reset the peer's client information. */</span>
- <span class="keyword">event</span>.peer -> data = NULL;
- }
- }
- ...
- ...
- ...
- </pre></div><h2><a class="anchor" id="SendingPacket"></a>
- Sending a packet to an ENet peer</h2>
- <p>Packets in ENet are created with <a class="el" href="group__Packet.html#gaac61b251aebbf9f5e5e313eca51339ea" title="Creates a packet that may be sent to a peer.">enet_packet_create()</a>, where the size of the packet must be specified. Optionally, initial data may be specified to copy into the packet.</p>
- <p>Certain flags may also be supplied to <a class="el" href="group__Packet.html#gaac61b251aebbf9f5e5e313eca51339ea" title="Creates a packet that may be sent to a peer.">enet_packet_create()</a> to control various packet features:</p>
- <p>ENET_PACKET_FLAG_RELIABLE specifies that the packet must use reliable delivery. A reliable packet is guarenteed to be delivered, and a number of retry attempts will be made until an acknowledgement is received from the foreign host the packet is sent to. If a certain number of retry attempts is reached without any acknowledgement, ENet will assume the peer has disconnected and forcefully reset the connection. If this flag is not specified, the packet is assumed an unreliable packet, and no retry attempts will be made nor acknowledgements generated.</p>
- <p>A packet may be resized (extended or truncated) with <a class="el" href="group__Packet.html#ga0aee7f8c7e2d2c4b64f6d68d930155a8" title="Attempts to resize the data in the packet to length specified in the dataLength parameter.">enet_packet_resize()</a>.</p>
- <p>A packet is sent to a foreign host with <a class="el" href="group__peer.html#gaf082a0ae58d9c435bed75c7325cf7290" title="Queues a packet to be sent.">enet_peer_send()</a>. <a class="el" href="group__peer.html#gaf082a0ae58d9c435bed75c7325cf7290" title="Queues a packet to be sent.">enet_peer_send()</a> accepts a channel id over which to send the packet to a given peer. Once the packet is handed over to ENet with <a class="el" href="group__peer.html#gaf082a0ae58d9c435bed75c7325cf7290" title="Queues a packet to be sent.">enet_peer_send()</a>, ENet will handle its deallocation and <a class="el" href="group__Packet.html#gab58895376ee4ade8f4e13761a44ba263" title="Destroys the packet and deallocates its data.">enet_packet_destroy()</a> should not be used upon it.</p>
- <p>One may also use <a class="el" href="group__host.html#ga5190a63f78eb0c15bd96cda44bf423c6" title="Queues a packet to be sent to all peers associated with the host.">enet_host_broadcast()</a> to send a packet to all connected peers on a given host over a specified channel id, as with <a class="el" href="group__peer.html#gaf082a0ae58d9c435bed75c7325cf7290" title="Queues a packet to be sent.">enet_peer_send()</a>.</p>
- <p>Queued packets will be sent on a call to <a class="el" href="group__host.html#ga6ba501b3ee576e5578c8e6d1694ebd49" title="Waits for events on the host specified and shuttles packets between the host and its peers...">enet_host_service()</a>. Alternatively, <a class="el" href="group__host.html#gac8f53bcdbd540043f87e7d59048559fa" title="Sends any queued packets on the host specified to its designated peers.">enet_host_flush()</a> will send out queued packets without dispatching any events.</p>
- <div class="fragment"><pre class="fragment"> <span class="comment">/* Create a reliable packet of size 7 containing "packet\0" */</span>
- <a class="code" href="struct__ENetPacket.html" title="ENet packet structure.">ENetPacket</a> * packet = <a class="code" href="group__Packet.html#gaac61b251aebbf9f5e5e313eca51339ea" title="Creates a packet that may be sent to a peer.">enet_packet_create</a> (<span class="stringliteral">"packet"</span>,
- strlen (<span class="stringliteral">"packet"</span>) + 1,
- <a class="code" href="enet_8h.html#a9d1fc0ce6da0a057f18bd8b9c762003daab20e7088245ab2ddb7f11dcc9433738" title="packet must be received by the target peer and resend attempts should be made until the packet is del...">ENET_PACKET_FLAG_RELIABLE</a>);
- <span class="comment">/* Extend the packet so and append the string "foo", so it now */</span>
- <span class="comment">/* contains "packetfoo\0" */</span>
- <a class="code" href="group__Packet.html#ga0aee7f8c7e2d2c4b64f6d68d930155a8" title="Attempts to resize the data in the packet to length specified in the dataLength parameter.">enet_packet_resize</a> (packet, strlen (<span class="stringliteral">"packetfoo"</span>) + 1);
- strcpy (& packet -> data [strlen (<span class="stringliteral">"packet"</span>)], <span class="stringliteral">"foo"</span>);
-
- <span class="comment">/* Send the packet to the peer over channel id 0. */</span>
- <span class="comment">/* One could also broadcast the packet by */</span>
- <span class="comment">/* enet_host_broadcast (host, 0, packet); */</span>
- <a class="code" href="group__peer.html#gaf082a0ae58d9c435bed75c7325cf7290" title="Queues a packet to be sent.">enet_peer_send</a> (peer, 0, packet);
- ...
- ...
- ...
- <span class="comment">/* One could just use enet_host_service() instead. */</span>
- <a class="code" href="group__host.html#gac8f53bcdbd540043f87e7d59048559fa" title="Sends any queued packets on the host specified to its designated peers.">enet_host_flush</a> (host);
- </pre></div><h2><a class="anchor" id="Disconnecting"></a>
- Disconnecting an ENet peer</h2>
- <p>Peers may be gently disconnected with <a class="el" href="group__peer.html#ga0e807704b6ecace5004c2cdcfbf813c2" title="Request a disconnection from a peer.">enet_peer_disconnect()</a>. A disconnect request will be sent to the foreign host, and ENet will wait for an acknowledgement from the foreign host before finally disconnecting. An event of type ENET_EVENT_TYPE_DISCONNECT will be generated once the disconnection succeeds. Normally timeouts apply to the disconnect acknowledgement, and so if no acknowledgement is received after a length of time the peer will be forcefully disconnected.</p>
- <p><a class="el" href="group__peer.html#ga9444dfff9574a7d21dbbdd34385a7d4d" title="Forcefully disconnects a peer.">enet_peer_reset()</a> will forcefully disconnect a peer. The foreign host will get no notification of a disconnect and will time out on the foreign host. No event is generated.</p>
- <div class="fragment"><pre class="fragment"> <a class="code" href="struct__ENetEvent.html" title="An ENet event as returned by enet_host_service().">ENetEvent</a> event;
-
- <a class="code" href="group__peer.html#ga0e807704b6ecace5004c2cdcfbf813c2" title="Request a disconnection from a peer.">enet_peer_disconnect</a> (peer, 0);
- <span class="comment">/* Allow up to 3 seconds for the disconnect to succeed</span>
- <span class="comment"> and drop any packets received packets.</span>
- <span class="comment"> */</span>
- <span class="keywordflow">while</span> (<a class="code" href="group__host.html#ga6ba501b3ee576e5578c8e6d1694ebd49" title="Waits for events on the host specified and shuttles packets between the host and its peers...">enet_host_service</a> (client, & event, 3000) > 0)
- {
- <span class="keywordflow">switch</span> (event.<a class="code" href="struct__ENetEvent.html#a53cd59a74e46de03e8f4b3fd47822d96" title="type of the event">type</a>)
- {
- <span class="keywordflow">case</span> <a class="code" href="enet_8h.html#a3cf937b5cf72510493e0a7a2b71d3755a67d928ca38b289db53ec9f56c91c5d9d" title="a packet has been received from a peer.">ENET_EVENT_TYPE_RECEIVE</a>:
- <a class="code" href="group__Packet.html#gab58895376ee4ade8f4e13761a44ba263" title="Destroys the packet and deallocates its data.">enet_packet_destroy</a> (event.<a class="code" href="struct__ENetEvent.html#afb6303a5593fce6b9671efbc2ca1b5de" title="packet associated with the event, if appropriate">packet</a>);
- <span class="keywordflow">break</span>;
- <span class="keywordflow">case</span> <a class="code" href="enet_8h.html#a3cf937b5cf72510493e0a7a2b71d3755a4fa47af84cf901810510aeba077a1c2f" title="a peer has disconnected.">ENET_EVENT_TYPE_DISCONNECT</a>:
- puts (<span class="stringliteral">"Disconnection succeeded."</span>);
- <span class="keywordflow">return</span>;
- ...
- ...
- ...
- }
- }
-
- <span class="comment">/* We've arrived here, so the disconnect attempt didn't */</span>
- <span class="comment">/* succeed yet. Force the connection down. */</span>
- <a class="code" href="group__peer.html#ga9444dfff9574a7d21dbbdd34385a7d4d" title="Forcefully disconnects a peer.">enet_peer_reset</a> (peer);
- ...
- ...
- ...
- </pre></div><h2><a class="anchor" id="Connecting"></a>
- Connecting to an ENet host</h2>
- <p>A connection to a foreign host is initiated with <a class="el" href="group__host.html#ga23b3ac206326b84f42fa91673f12fca9" title="Initiates a connection to a foreign host.">enet_host_connect()</a>. It accepts the address of a foreign host to connect to, and the number of channels that should be allocated for communication. If N channels are allocated for use, their channel ids will be numbered 0 through N-1. A peer representing the connection attempt is returned, or NULL if there were no available peers over which to initiate the connection. When the connection attempt succeeds, an event of type ENET_EVENT_TYPE_CONNECT will be generated. If the connection attempt times out or otherwise fails, an event of type ENET_EVENT_TYPE_DISCONNECT will be generated.</p>
- <div class="fragment"><pre class="fragment"> <a class="code" href="struct__ENetAddress.html" title="Portable internet address structure.">ENetAddress</a> address;
- <a class="code" href="struct__ENetEvent.html" title="An ENet event as returned by enet_host_service().">ENetEvent</a> event;
- <a class="code" href="struct__ENetPeer.html" title="An ENet peer which data packets may be sent or received from.">ENetPeer</a> *peer;
- <span class="comment">/* Connect to some.server.net:1234. */</span>
- <a class="code" href="group__Address.html#ga32a6ae1ed3d7704858f237688e7384ee" title="Attempts to resolve the host named by the parameter hostName and sets the host field in the address p...">enet_address_set_host</a> (& address, <span class="stringliteral">"some.server.net"</span>);
- address.port = 1234;
- <span class="comment">/* Initiate the connection, allocating the two channels 0 and 1. */</span>
- peer = <a class="code" href="group__host.html#ga23b3ac206326b84f42fa91673f12fca9" title="Initiates a connection to a foreign host.">enet_host_connect</a> (client, & address, 2, 0);
-
- <span class="keywordflow">if</span> (peer == NULL)
- {
- fprintf (stderr,
- <span class="stringliteral">"No available peers for initiating an ENet connection.\n"</span>);
- exit (EXIT_FAILURE);
- }
-
- <span class="comment">/* Wait up to 5 seconds for the connection attempt to succeed. */</span>
- <span class="keywordflow">if</span> (<a class="code" href="group__host.html#ga6ba501b3ee576e5578c8e6d1694ebd49" title="Waits for events on the host specified and shuttles packets between the host and its peers...">enet_host_service</a> (client, & event, 5000) > 0 &&
- event.<a class="code" href="struct__ENetEvent.html#a53cd59a74e46de03e8f4b3fd47822d96" title="type of the event">type</a> == <a class="code" href="enet_8h.html#a3cf937b5cf72510493e0a7a2b71d3755aefd9fa36297e41ca4c1cbcfdeb7e4a9d" title="a connection request initiated by enet_host_connect has completed.">ENET_EVENT_TYPE_CONNECT</a>)
- {
- puts (<span class="stringliteral">"Connection to some.server.net:1234 succeeded."</span>);
- ...
- ...
- ...
- }
- <span class="keywordflow">else</span>
- {
- <span class="comment">/* Either the 5 seconds are up or a disconnect event was */</span>
- <span class="comment">/* received. Reset the peer in the event the 5 seconds */</span>
- <span class="comment">/* had run out without any significant event. */</span>
- <a class="code" href="group__peer.html#ga9444dfff9574a7d21dbbdd34385a7d4d" title="Forcefully disconnects a peer.">enet_peer_reset</a> (peer);
- puts (<span class="stringliteral">"Connection to some.server.net:1234 failed."</span>);
- }
- ...
- ...
- ...
- </pre></div> </div>
- <hr class="footer"/><address class="footer"><small>Generated on Wed Feb 9 2011 21:31:05 for enet by
- <a href="http://www.doxygen.org/index.html">
- <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.1 </small></address>
- </body>
- </html>