[ENet-discuss] find peers connected to host

Alexander Shyrokov sj at sjcomp.com
Tue Mar 2 14:26:22 PST 2010


Hello Christian,

> How to enumerates client connected to host.?
I do not know if you can ask host to provide you with the list of all 
connected peers, instead you can follow the procedure outlined in the 
answer to the next question.

> I need to know all peers connected to host server.
Every time host receives ENET_EVENT_TYPE_CONNECT you have information 
about the connected peer in the event structure. You need to remember 
this peer in a list of currently connected peers.

> But, I looked on enethost struct and found peerCount but this int is always
> mas number peers available.
> So, how to find the connected peers list to the host..
> And send them message from the host..
> Or reply, after performed calculation by example..
> Or enumerate them, and know their state..

So in general you do the following, this is an illustration:
// List of all peers that are currently connected.
std::vector<ENetPeer> AllPeers;
// Event with the new information.
ENetEvent Event;
// Process all received messages.
while(enet_host_service(m_Host, &Event, 0) >= 0)
{
   switch (Event.type)
   {
   case ENET_EVENT_TYPE_CONNECT:
     // Remember the new peer.
     AllPeers.push_back(m_Event.peer);
     break;
   case ENET_EVENT_TYPE_RECEIVE:
     // AllPeers has the list of all the connected clients as far
     // as you know it.
     // Here you want to go through all peers and send them something.
     // How you do it is up to you.
     int Result = DoSomething(Event.packet->data);
     // For example you may have SendIt predicate,
     // Or a loop through the peers will do as well.
     std::for_each(AllPeers.begin(), AllPeers.end(), SendIt(Result));
     // Clean up the packet now that we're done using it.
     enet_packet_destroy(Event.packet);
     break;
   case ENET_EVENT_TYPE_DISCONNECT:
     // This is where you are informed about disconnects.
     // Simply remove the peer from the list of all peers.
     AllPeers.erase(
        std::find(m_AllPeers.begin(), m_AllPeers.end(), Event.peer));
     break;
   }
}

Hope that helps,
Alexander


More information about the ENet-discuss mailing list