[ENet-discuss] ENet package broadcasting

Boris bstih at zootfly.com
Tue Feb 24 07:47:46 PST 2009

I must say, that ENet is a great network library.

We are working on a multiplayer based game, so we decided to use ENet 

To make our lives easier, we decided to use fixed time step as in Halo 
games, so that we can send players inputs over network. What this means 
is, that if we send same inputs over and over, we get the same results. 
This is also great for replaying and debugging.

To understand what I am talking about here is a link: 

By using ENet, we are making peer-to-peer based network, so that every 
client sends its input to others.
We do this by creating a client host with enet_host_create() function. 
Ofcoarse we flag one of the clients as server, so that it manages some 
of the server methods.

The following code creates client host:

void CreateClient(ENetAddress address, int server)
    ENetHost *client = enet_host_create (& address , MAX_CONNECTIONS, 0, 

When a new client connects to our server flagged client, a respond 
message is send back to that client. A message contains a list of all 
clients connected to server. We tell our new client to connect to every 
client from this list by using the following code:

void ClientConnect()
    for(int a=0;a<aClientList;a++)
       ENetAddress address = aClientList[a]->GetAddress();
       ENetPeer *clientpeer = enet_host_connect( this_client , address , 
1 );

So this is how we get our peer-to-peer based network.

Because of our fixed time step, we wait every client to send its input 
to every one. When data arrives, we update next frame.

But the problem is that it takes way to long for data to arrive from 
client to server or vice versa.
To send a message we use the following code:
void SendMessage(ENetHost *client, Data data)
    ENetPacket *pPacket = enet_packet_create( data->GetBuffer() , 
data->GetSize() ,   ENET_PACKET_FLAG_RELIABLE );

      enet_host_broadcast( client , 0 , pPacket );
      enet_host_flush( client ); 

The NetworkManager does something like in the following code:

    if(updateinput > input_lag-1)
        // send only once
         pNetPeer->UpdateServer(numloop>1 ? deltadt : 

         pNetPeer->UpdateClient(numloop>1 ? deltadt : 
// read from input buffers for dummy players                   
The isInputBuffer() function checks if there is still any input in 
buffer to read from. If the input buffers are empty go to while loop and 
stay in this loop until data arrived from all connected clients.
UpdateNetworkPlayerInput() function creates a message containing players 
input. This functions calls our SendMessage() function, so that inputs 
are sent to other connected clients.

UpdateClient() and UpdateServer(), these two functions call 
enet_host_service() where events are processed.
while( enet_host_service( pHost , &Event , 0 ) > 0)
      switch( Event.type ) {
             //handle connected events
             //handle receive events
like in tutorial.

So my question is, what could cause such big lag? The data can take even 
half a second to arrive. When sending text messages, or ping messages it 
appears to work properly. The data arrives with minimal delay.
The size of input data that we are sending is about 120 bytes depends on 
amount of inputs pressed.

We are using latest ENet 1.22

Thank you in advance, for any further clarifications please reply.

More information about the ENet-discuss mailing list