lsalzman / enet

ENet reliable UDP networking library
MIT License
2.77k stars 673 forks source link

Don't want the client to be able to be connected to #252

Closed cuppajoeman closed 6 months ago

cuppajoeman commented 6 months ago

Hi, from what I understand in a client - server setup using enet, we create an ENetHost on the client and the server.

I want clients to only connect to a server, with my setup I don't want to allow clients to connect to each other. Is there specifically a method to make sure that this does not occur?

So far I was thinking that we just don't implement a case ENET_EVENT_TYPE_CONNECT: in the clients enet_host_service loop, but from what I understand this wouldn't stop clients from trying to connect to eachother, but just stop the handling if a client does try to connect.

Is this this the right approach to making sure that clients cannot connect to eachother?


Also along the same lines when a client tries to connect to the server we have something like this: (as per the enet docs)

ENetAddress address;
ENetEvent event;
ENetPeer *peer;

enet_address_set_host (& address, "some.server.net");
address.port = 1234;

peer = enet_host_connect (client, & address, 2, 0);    

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

/* Wait up to 5 seconds for the connection attempt to succeed. */
if (enet_host_service (client, & event, 5000) > 0 &&
    event.type == ENET_EVENT_TYPE_CONNECT){
    puts ("Connection to some.server.net:1234 succeeded.");
} else {
    enet_peer_reset (peer);
    puts ("Connection to some.server.net:1234 failed.");
}

The docs say:

An event of type ENET_EVENT_TYPE_CONNECT is returned when either a new client host has connected to the server host or when an attempt to establish a connection with a foreign host has succeeded

Then to my understanding it's possible that we try to connect, and then a client attempts to connect to our current client, and so we get a ENET_EVENT_TYPE_CONNECT before the server sends back the event that the connection was successful, so how can we actually differentiate between those two different events?

cuppajoeman commented 6 months ago

By passing null as the first argument to enet_host_create you can get this functionality: http://enet.bespin.org/group__host.html#ga95b851cfb4a56aa3d760e5f974c66361