[ENet-discuss] peer-to-peer model

YongGang Li yonggangli at gmail.com
Fri May 20 07:55:14 PDT 2005


Hi,
 As a newbie, I am wondering how to use ENet for peer-to-peer communiation, 
instead of just client/server?
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?
 thanks
--------------------

#include <stdio.h>
#include "enet/enet.h"

bool Bs2s =false;
class EnetSrv
{
public:
EnetSrv()
{
_psrvenet = 0;
enet_initialize ();

}

~EnetSrv()
{
if(_psrvenet)
enet_host_destroy(_psrvenet);
enet_deinitialize();
}

bool Start(char* host,int port)
{
ENetAddress address;

enet_address_set_host (& address, host); 
address.port = port;
_psrvenet = enet_host_create (& address /* the address to bind the server 
host to */, 
32 /* allow up to 32 clienets and/or outgoing connections */,
0 /* assume any amount of incoming bandwidth */,
0 /* assume any amount of outgoing bandwidth */);

return _psrvenet!=0;
}

void Subscribe()
{
ENetEvent event;

// Wait up to 1000 milliseconds for an event. 
bool kup=true;
while(kup)
{
printf(".");

while (enet_host_service (_psrvenet, &event, 500) > 0)
{
switch (event.type)
{
case ENET_EVENT_TYPE_CONNECT:
printf ("A new _pclienet connected from %x:%u.\n", 
event.peer -> address.host,
event.peer -> address.port);
/* Store any relevant _pclienet information here. */
event.peer -> data = "_pclienet information";
break;
case ENET_EVENT_TYPE_RECEIVE:
printf ("A packet of length %u containing %s was received from %s on channel 
%u.\n",
event.packet -> dataLength,
event.packet -> data,
event.peer ->data,
event.channelID);
/* Clean up the packet now that we're done using it. */
//send back data
{
ENetPacket * packet = enet_packet_create (event.packet -> data,
event.packet-> dataLength + 1, ENET_PACKET_FLAG_RELIABLE);
/* Send the packet to the peer over channel id 3. */
/* One could also broadcast the packet by */
enet_host_broadcast (_psrvenet, 1, packet);
//enet_peer_send (event.peer, 3, packet);
enet_host_flush (_psrvenet);
}

enet_packet_destroy (event.packet);
break;
case ENET_EVENT_TYPE_DISCONNECT:
printf ("%s disconected.\n", event.peer ->data);
/* Reset the peer's _pclienet information. */
event.peer -> data = NULL;
}
}
}
}

ENetHost * _psrvenet;
};


class EnetCli
{
public: 
EnetCli()
{ 
enet_initialize (); 
} 

~EnetCli()
{ 
if(_pclienet) 
enet_host_destroy(_pclienet);
enet_deinitialize();
}

bool Create() 
{ 
_pclienet = enet_host_create (NULL /* create a _pclienet host */, 
1 /* only allow 1 outgoing connection */, 
576000 / 8 /* 56K modem with 56 Kbps downstream bandwidth */, 
144000 / 8 /* 56K modem with 14 Kbps upstream bandwidth */); 
if (_pclienet == NULL) 
{ 
fprintf (stderr, "An error occurred while trying to create an ENet _pclienet 
host.\n"); 
} 
return _pclienet!=0; 
} 
void Disconnect() 
{ 
enet_peer_disconnect (& _pclienet -> peers [0]); 
} 

bool Connect(char* ip, int port) 
{ 
ENetAddress address; 
ENetEvent event; 

/* Connect to some.server.net:1234 <http://some.server.net:1234>. */ 
enet_address_set_host (& address, ip); 
address.port = port; /* Initiate the connection, allocating the two channels 
0 and 1. */ 
_peer = enet_host_connect (_pclienet, & address, 2); 

if (_peer == NULL) 
{ 
fprintf (stderr, "No available peers for initiating an ENet connection.\n"); 

return false; 
} /* Wait up to 5 seconds for the connection attempt to succeed.*/ 

if (enet_host_service (_pclienet, & event, 5000) > 0 && 
event.type == ENET_EVENT_TYPE_CONNECT) 
{ 
puts("Connection to succeeded."); 
return true; 
} 
else 
{ 
/* Either the 5 seconds are up or a disconnect event was */ 
/* received. Reset the _peer in the event the 5 seconds */ 
/* had run out without any significant event. */ 
enet_peer_reset (_peer); 
puts ("Connection failed."); 
} 
return false; 
} 

void Subscribe() 
{ 
ENetEvent event; 
int loops = 0; 

while(1) 
{ 
while (enet_host_service (_pclienet, & event, 20) > 0) 
{ 
switch (event.type) 
{ 
case ENET_EVENT_TYPE_RECEIVE: 
printf ("A packet of length %u containing %s was received from %s on channel 
%u.\n", 
event.packet -> dataLength, 
event.packet -> data, 
event.peer -> data, 
event.channelID); 
/* Clean up the packet now that we're done using it. */ 
enet_packet_destroy (event.packet); 
break; 
case ENET_EVENT_TYPE_DISCONNECT: 
puts ("Disconnection succeeded."); 
return; 
} 
} //send back data 

{ 
if(Bs2s) 
{ 
enet_host_flush (_pclienet); 
ENetPacket * packet = enet_packet_create ("packet", 1024, 
ENET_PACKET_FLAG_RELIABLE); 
enet_peer_send (_peer, 1, packet); 
} 
} 
} 
Disconnect(); 
enet_peer_reset (& _pclienet -> peers [0]); 
} 

ENetPeer* _peer; 
ENetHost * _pclienet;
};

void RunServer()
{ 
EnetSrv s; 
if(s.Start("127.0.0.1 <http://127.0.0.1>",1234)) 
{ 
s.Subscribe(); 
}
}

void RunClient()
{ 
EnetCli c; 
if(c.Create()) 
{ 
if(c.Connect("127.0.0.1 <http://127.0.0.1>",1234)) 
{ 
c.Subscribe(); 
} 
}
}

int main(int argc, char* argv[])
{ 
if(argc==2) 
{ 
if(!strcmp(argv[1],"/s")) 
RunServer(); 
else 
{ 
if(!strcmp(argv[1],"/cs")) 
Bs2s=true; 
RunClient(); 
} 
} 

return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.puremagic.com/pipermail/enet-discuss/attachments/20050520/67308d64/attachment.htm


More information about the ENet-discuss mailing list