Hi,
I’ve been working on implementing client-side prediction for an FPS, but I’ve run into some problems using the messaging system in Unet. After debugging for awhile I found out that when using ConnectWithSimulator with latency my messages from the client weren’t arriving in proper order at the server. I have packet loss set to 0 for the simulation, and with a larger latency value the misordering is larger. Even if the messages are buffered at one point I would assume that it would be a queue with the first that arrive being the first that are read by the server.
To debug this I built a simple test script that can be easily run.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class MessageTest : MonoBehaviour {
[SerializeField]
bool useSimulator;
[SerializeField]
float packetLoss = 0;
[SerializeField]
int latency = 0;
public class MsgTest : MessageBase
{
public int index;
}
NetworkClient client;
void InitializeClient()
{
client = new NetworkClient();
if (useSimulator)
client.ConnectWithSimulator("localhost", 7777, latency, packetLoss);
else
client.Connect("localhost", 7777);
}
int i = 0;
void Update()
{
if (client != null && client.isConnected)
{
client.Send(MsgType.Highest + 1, new MsgTest() { index = i });
i++;
}
}
void InitializeServer()
{
NetworkServer.Listen(7777);
NetworkServer.RegisterHandler(MsgType.Highest + 1, ReceiveFromClient);
}
void ReceiveFromClient(NetworkMessage netMsg)
{
var msg = netMsg.ReadMessage<MsgTest>();
Debug.Log(msg.index);
}
void OnGUI()
{
if (!NetworkServer.active)
{
if (GUI.Button(new Rect(400, 20, 200, 20), "Init Server"))
{
InitializeServer();
}
}
if (client == null || !client.isConnected)
{
if (GUI.Button(new Rect(100, 20, 200, 20), "Client Connect"))
{
InitializeClient();
}
}
}
}
When I run this with 100 latency and 0 packet loss I get:
0
1
2
3
4
5
11
6
13
7
8
9
10
12
Is this just an issue with the simulator, or should I expect this in real-world conditions with latency?
Thanks,
Erik
