Hi
I started experimenting with the UNET LLAPI yesterday and I can’t seem to get it work right. The documentation seems very limited also.
I created a simple client and server system. Server accepts multiple client connections and client just tries to connect to an ip of the server. Server keeps active connections in a list. If connection is made, it adds it to the list, if connection disconnects, it is removed from the list. When the connection is not in the list, no messages are sent by my code.
Clients then start sending their input data every fixedUpdate (I used 0.015 timesetep and data size was 27 bytes). Server simulates game world based on the input data and sends it back to the clients on every of its own fixedupdate. Clients apply the simulation data whenever one is received from server.
Then all kind of things started going wrong. If there is 2 clients connected and 1 of them disconnects (without sending disconnect message before disconnecting), then about 50% times the server either crashes or spams some meaningless error like “>= 0” or “> 0”, I think it comes from a code line that does NetworkTransport.Send, but there is no data about where the error message came from and I can’t reliably reproduce it either. Seems to be completely random. Second option is that Unity editor just crashes. I haven’t tried with sending a disconnect message, but that can’t be a requirement, every client could just crash the server then by not sending it if that would be the case.
I set gConf.ReactorMaximumSentMessages to 2048 and lowered the fixedupdate timestep from 0.015 to 0.0333, now it doesn’t seem to crash. Haven’t been able to reproduce it ever since I changed that, even when setting them back. I’d still assume that 66 <50 byte messages per second shouldn’t be an issue on a local network and that it would give me errors instead of crashing the editor. The crashes and strange errors only occurred when clients disconnected so there is probably some kind of bug there.
I tried using NetworkTransport.GetCurrentOutgoingMessageAmount() to get the status of the buffer, but all it shows is how many messages have been sent in runtime. The number never goes down. The function description says “Return total message amount waiting for sending”, so I assumed it would tell me how many messages are sitting in the buffer to be sent. The client receives the messages just fine. I tried using both unreliable channel and stateupdate channel, with the only difference being that with stateupdate at some point it decides it will not send any messages at all through that channel.
Next issue is that I have no idea when the packets are actually sent. If I do NetworkTransport.Send(), does it send packet right away, or does it pack multiple of them in one packet and then sends it out? If the latter then what would be the send rate at which it sends packets? I feel like that’s something that would be nice to have control over, It would be hard to do any kind of more advanced server-client synchronization otherwise. For example for my purpose, I’d like to have at least 30 updates per second and if the client’s connection can’t handle that, he will get disconnected. If it uses other send rate internally, it would not be very optimal because they’d get out of sync, which would cause extra latency.
Now about the provided examples. The example provided in the documentation shows the following receiving code in Update(): (http://docs.unity3d.com/Manual/UNetUsingTransport.html)
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.Nothing: //1
break;
case NetworkEventType.ConnectEvent: //2
break;
case NetworkEventType.DataEvent: //3
break;
case NetworkEventType.DisconnectEvent: //4
break;
}
Wouldn’t it be a (while) loop until the event is “NetworkEventType.Nothing” in case multiple events happen within a single frame? Or am I missing something.
The next problem is that sometimes when I do NetworkTransport.Send() in server and the client just disconnected, I sometimes get the warning that I’m sending to a not connected connection. I can imagine that is because the networking runs on another thread and the client disconnects in the middle of the game frame code, so I haven’t processed the disconnect event, even though the connection is dead already. If I receive connection events in Update() and send messages in FixedUpdate(), it can happen very often as Unity can drop rendering frames and the code keeps trying to send stuff in fixedupdate() without update() being called. The warning spam is just annoying and made me wonder if there is some way to solve it?
Attached all the networking code I used.
2233267–148835–UNet.cs (6.49 KB)
2233267–148844–UServer.cs (9.64 KB)