lidgren / lidgren-network-gen3

Lidgren Network Library
https://groups.google.com/forum/#!forum/lidgren-network-gen3
MIT License
1.19k stars 331 forks source link

Delay #77

Open ramonfr2014 opened 8 years ago

ramonfr2014 commented 8 years ago

I'm writing a server and a client.

Each client, sends a NetOutgoingMessage every 50 ms, that's equal to 20 messages per second. This message cointains 4 variables, which represent the current player's position and rotation.

The server, after receiving this message, calculates the next position of the player, then sends it back to all connected players from that match, using the following line:

> server.SendMessage(outMessage, connectionsList, NetDeliveryMethod.Unreliable, 0);

(connectionsList is a list of NetConnection objects, not an array)

Also, the server constantly sends an update of the current position of another object, a ball, every 30 ms; that's equal to 33 times per second. The message is sent by a separate thread.

The problem is:

I get severe delay when running over 3 instances of the client on the same machine, or a different machine in the local network. It looks like the network messages are getting stuck in a stack of messages, because even after I close the other clients, the messages from those disconnected clients take too long to arrive at the client that's still running.

Are the messages being sent too frequently? What's going on?

(I'm using the latest version of the library, and coding the client on Unity)

AgentFire commented 8 years ago

I am heavily sure that the source of the issue lies inside your code.

AgentFire commented 7 years ago

@ramonfr2014 like how exactly do you read your incoming messages on both ends?

willthiswork89 commented 7 years ago

You should reconsider your message structure to follow traditional networked games.

Generally you only send Deltas, and every x seconds send a true position update.

So i push the stick forward, i send out a packet with direction(rotation etc) and velocity. I do not send another packet until one of those variables changes. This should minimize your chatter on the wire and achieve the same results. You allow the client to extrapolate the position.

This is why in Halo, CoD etc when you lose network connection players run straight into a wall and continue running. They never received a velocity and vector update to change the behavior to the client continues to extrapolate from the previous packet it received.

RevoluPowered commented 6 years ago

@ramonfr2014 Sending too frequently can cause issues I had a similar issue, put a time delay on the update. You should only send network updates at the same rate as the server updates.

To resolve this, I just use a timer locally on the entities which I network. 60 ticks a second, means 60 updates in that game object, which is more than enough for things like player information.

Then as @willthiswork89 says, send delta's and make sure to use autocorrection when the values become hairy.