[ENet-discuss] Managing peers on the server

Lee Salzman lsalzman1 at cox.net
Tue Jun 3 14:40:00 PDT 2008


There is a "void *data;" field in ENetPeer which ENet never touches, so 
you can set it to whatever you want, i.e. it can point back to your 
player structure.

The internal array of peers is fixed size and never reordered. When a 
client comes in the first available entry is used for the new 
connection, but otherwise the same entry is always used for the duration 
of the connection.

Yes, what you pass in to enet_host_create is the maximum number of peers 
it can support.

You can't assign an index in the peers array like you suggest. Just 
don't do it. However, the first connection issued by enet_host_connect, 
so long as no other connections have been created, is almost guaranteed 
to be put at peers[0].

If you want the server to be recognizable, don't use localhost as 
address.host, it must either be an IP address that is guaranteed 
recognizable from outside, or ENET_HOST_ANY to just take whatever IP the 
OS assigns, whichever. You must still give it a fixed port number, however.

Lee

Ben Johnson wrote:
> 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 
> <http://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.
>  
>  
> ------------------------------------------------------------------------
>
> _______________________________________________
> ENet-discuss mailing list
> ENet-discuss at cubik.org
> http://lists.cubik.org/mailman/listinfo/enet-discuss
>   



More information about the ENet-discuss mailing list