<div>Hi,</div>
<div> </div>
<div>As a newbie, I am wondering how to use ENet for peer-to-peer communiation, instead of just client/server?</div>
<div>can anyone kindly post a peer-to-peer model code snippet made by ENet? like, the following is the test I made according to the tutorial, but, how to organize it for a peer-to-peer mode?</div>
<div> </div>
<div>thanks</div>
<div>--------------------</div>
<div><br>
<p>#include <stdio.h><br>#include "enet/enet.h"</p>
<p>bool Bs2s =false;<br>class EnetSrv<br>{<br>public:<br> EnetSrv()<br> {<br> _psrvenet = 0;<br> enet_initialize ();<br> <br> }<br> <br> ~EnetSrv()<br> {<br> if(_psrvenet)<br> enet_host_destroy(_psrvenet);<br> enet_deinitialize();
<br> }<br> <br> bool Start(char* host,int port)<br> {<br> ENetAddress address;<br> <br> enet_address_set_host (& address, host); <br> address.port = port;<br> _psrvenet = enet_host_create (& address /* the address to bind the server host to */,
<br> 32 /* allow up to 32 clienets and/or outgoing connections */,<br> 0 /* assume any amount of incoming bandwidth */,<br> 0 /* assume any amount of outgoing bandwidth */);<br> <br> return _psrvenet!=0;
<br> }<br> <br> void Subscribe()<br> {<br> ENetEvent event;<br> <br> // Wait up to 1000 milliseconds for an event. <br> bool kup=true;<br> while(kup)<br> {<br> printf(".");<br> <br> while (enet_host_service (_psrvenet, &event, 500) > 0)
<br> {<br> switch (event.type)<br> {<br> case ENET_EVENT_TYPE_CONNECT:<br> printf ("A new _pclienet connected from %x:%u.\n", <br> event.peer -> address.host,<br> event.peer -> address.port
);<br> /* Store any relevant _pclienet information here. */<br> event.peer -> data = "_pclienet information";<br> break;<br> case ENET_EVENT_TYPE_RECEIVE:<br> printf ("A packet of length %u containing %s was received from %s on channel %u.\n",
<br> event.packet -> dataLength,<br> event.packet -> data,<br> event.peer ->data,<br> event.channelID);<br> /* Clean up the packet now that we're done using it. */<br> //send back data
<br> {<br> ENetPacket * packet = enet_packet_create (event.packet -> data, event.packet -> dataLength + 1, ENET_PACKET_FLAG_RELIABLE);<br> /* Send the packet to the peer over channel id 3. */<br> /* One could also broadcast the packet by */
<br> enet_host_broadcast (_psrvenet, 1, packet);<br> //enet_peer_send (event.peer, 3, packet);<br> enet_host_flush (_psrvenet);<br> }<br> <br> enet_packet_destroy (event.packet);<br> break;<br>
case ENET_EVENT_TYPE_DISCONNECT:<br> printf ("%s disconected.\n", event.peer ->data);<br> /* Reset the peer's _pclienet information. */<br> event.peer -> data = NULL;<br> }<br> }<br> }
<br> }<br> <br> ENetHost * _psrvenet;<br>};</p>
<p><br>class EnetCli<br>{<br>public: <br> EnetCli()<br> { <br> enet_initialize (); <br> } <br> <br> ~EnetCli()<br> { <br> if(_pclienet) <br> enet_host_destroy(_pclienet);<br> enet_deinitialize();
<br> }<br> <br> bool Create() <br> { <br> _pclienet = enet_host_create (NULL /* create a _pclienet host */, <br> 1 /* only allow 1 outgoing connection */, <br> 576000 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
<br> 144000 / 8 /* 56K modem with 14 Kbps upstream bandwidth */); <br> if (_pclienet == NULL) <br> { <br> fprintf (stderr, "An error occurred while trying to create an ENet _pclienet host.\n");
<br> } <br> return _pclienet!=0; <br> } <br> void Disconnect() <br> { <br> enet_peer_disconnect (& _pclienet -> peers [0]); <br> } <br> <br> bool Connect(char* ip, int port) <br> {
<br> ENetAddress address; <br> ENetEvent event; <br> <br> /* Connect to <a href="http://some.server.net:1234">some.server.net:1234</a>. */ <br> enet_address_set_host (& address, ip);
<br> address.port = port; /* Initiate the connection, allocating the two channels 0 and 1. */ <br> _peer = enet_host_connect (_pclienet, & address, 2); <br> <br> if (_peer == NULL)
<br> { <br> fprintf (stderr, "No available peers for initiating an ENet connection.\n"); <br> return false; <br> } /* Wait up to 5 seconds for the connection attempt to succeed.*/
<br> <br> if (enet_host_service (_pclienet, & event, 5000) > 0 && <br> event.type == ENET_EVENT_TYPE_CONNECT) <br> { <br> puts("Connection to succeeded."); <br>
return true; <br> } <br> else <br> { <br> /* Either the 5 seconds are up or a disconnect event was */ <br> /* received. Reset the _peer in the event the 5 seconds */
<br> /* had run out without any significant event. */ <br> enet_peer_reset (_peer); <br> puts ("Connection failed."); <br> } <br> return false; <br> }
<br> <br> void Subscribe() <br> { <br> ENetEvent event; <br> int loops = 0; <br> <br> while(1) <br> { <br> while (enet_host_service (_pclienet, & event, 20) > 0)
<br> { <br> switch (event.type) <br> { <br> case ENET_EVENT_TYPE_RECEIVE: <br> printf ("A packet of length %u containing %s was received from %s on channel %u.\n",
<br> event.packet -> dataLength, <br> event.packet -> data, <br> event.peer -> data,
<br> event.channelID); <br> /* Clean up the packet now that we're done using it. */ <br> enet_packet_destroy (event.packet); <br> break;
<br> case ENET_EVENT_TYPE_DISCONNECT: <br> puts ("Disconnection succeeded."); <br> return; <br> } <br> } //send back data
<br> <br> { <br> if(Bs2s) <br> { <br> enet_host_flush (_pclienet); <br> ENetPacket * packet = enet_packet_create ("packet", 1024, ENET_PACKET_FLAG_RELIABLE);
<br> enet_peer_send (_peer, 1, packet); <br> } <br> } <br> } <br> Disconnect(); <br> enet_peer_reset (& _pclienet -> peers [0]); <br> } <br>
<br> ENetPeer* _peer; <br> ENetHost * _pclienet;<br>};</p>
<p>void RunServer()<br>{ <br> EnetSrv s; <br> if(s.Start("<a href="http://127.0.0.1">127.0.0.1</a>",1234)) <br> { <br> s.Subscribe(); <br> }<br>}</p>
<p>void RunClient()<br>{ <br> EnetCli c; <br> if(c.Create()) <br> { <br> if(c.Connect("<a href="http://127.0.0.1">127.0.0.1</a>",1234)) <br> { <br> c.Subscribe();
<br> } <br> }<br>}</p>
<p>int main(int argc, char* argv[])<br>{ <br> if(argc==2) <br> { <br> if(!strcmp(argv[1],"/s")) <br> RunServer(); <br> else <br> { <br> if(!strcmp(argv[1],"/cs"))
<br> Bs2s=true; <br> RunClient(); <br> } <br> } <br> <br> return 0;<br>}</p><br> </div>