[ENet-discuss] Managing peers on the server

Ben Johnson bencelot at gmail.com
Tue Jun 3 03:28:11 PDT 2008


Hey all, first post!

I was looking around and Enet appears to be what I'm after. I'm making a
game using a server/client model, but there are a few things I'm unsure
about.

What is the best way to manage peers from the server end? So I can store
peers, and associate them with a particular player. Basically, when a player
joins the server, I'd like the server to add this peer to a list, and assign
it an ID so I can link it with a player. Then, when they disconnect, I'd
like them to be taken off the list.

At first I tried making: std::vector<ENetPeer*> peers;

and each time a player joins, scanning through this vector, checking:
if(peers[i] == NULL) peers[i] = event.peer;

thus replacing that peer. Then, when a peer disconnects, looping through
this vector, checking:
if(peers[i]->address.host == event.peer->address.host &&
peers[i]->address.port == event.peer->address.port) peers[i] = NULL;

Then, the way I associate each peer with it's corresponding player object is
simply that they both share the same index in their respective vectors. So
player[7] would belong to peers[7].

ANNYWAY.. this doesn't seem to be the best method. Because some people in
the archives have mentioned that the host actually has it's own list of
peers. And that it does all this for you? And that when a new peer connects,
you can get the index of the new peer like:

case ENET_EVENT_TYPE_CONNECT:
int index = (int)(event.peer - host->peers);
...

So my questions are (there's a lot, I know.. sorry):

1) is my original way a good way? Or should I use host->peers instead?
2) If I use host->peers.. will the host also manage the deleting of peers
when it gets a disconnect? If it does, does it rearrange the array? So if I
have 3 peersconnected, and then the 2nd one leaves.. will I still be able to
access the third player with host->peers[2] or will it get dropped down to
the 2nd position at host->peers[1]? Furthermore, would it be best to save
the playerID into the data field?

3) Is the array size of host->peers set by the 2nd parameter in
enet_host_create? So if I put in 64, will that mean 64 clients can be
connected at once?

4) I want my game to have a dedicated and a non-dedicated server option. If
it's non-dedicated, then you will control player[0]. Thus it would be
convenient if I could also block off host->peers[0] (or my original
peers[0]) with a 'dummy' connection to itself. Is this possible? Is it wise?
Like, upon creating the host, could I go:

if(!dedicated) host->peers[0] = enet_host_connect(host,&address,2);

5) LAN vs Internet vs same computer. At the moment, all I'm able to do is
get 2 applications on the same computer connecting. Is this because I'm
using enet_address_set_host(&address, "127.0.0.1") when creating the server?
If I wanted the server to be recognisable on LAN what address would I use?
How about the net? or both at the same time? Would I just use address.host =
ENET_HOST_ANY? If so, how do I know which IP to search for on the clients
end?

Ok.. that's all for now. If you can answer any questions at all, that'd be
great :)

Loving Enet so far, hopefully I can get past these basic concepts and get
into it more!

Thanks,
Ben.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubik.org/pipermail/enet-discuss/attachments/20080603/62241372/attachment.htm>


More information about the ENet-discuss mailing list