- Home /
Unity Networking LLAPI - High Bandwidth
Hi everyone!
I'm working on a 2D multiplayer platform shooter.
For the networking I am using Unity's LLAPI. This is my first time working with the LLAPI so the issue is probably in my code.
I'm currently testing with up to 4 players - 3 clients, and 1 host (Client with a server manager in scene)
My setup is that each client sends their position to the server at a frequency of 30hz. The server then records it in its own scene.
The server, regardless of client messages, sends updates to all clients (Except host) for every player object in its scene (Except for the object that belongs to the recipient). At a frequency of 30hz as well.
My issue, is that when testing this out with just two players, my bandwidth is up there at about 200Kb/s upload on the server side. This goes up to about 2Mb/s with just four players.
The position updates are three floats (Vector 3 pos) and a byte (Object's ID) per message. So a total of 13 bytes per message. So with 4 players, considering one is a host, the bandwidth used should be (13x3x3x30=117) 3510 bytes/s or just 27.4Kb/s. (A bit more I'm sure, because the message has some more properties of its own. But still marginally less than 2Mb/s)
// For Pos Updates: Recieves the message, and the object's ID
public void SendClientUpdateAll(NetMsg msg, byte ID){
// The buffer holds the data
byte[] buffer = new byte[BYTE_SIZE];
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream(buffer);
formatter.Serialize(ms, msg);
// MAX_USER = 4. i = client ID. clientArray[i]=connectionId of a client. clientArray[0] is server.
// <i != ID> Make sure we're not sending a player their own position (Client authoritative)
for(int i = 1; i < MAX_USER; i++) {
if(clientArray[i] != 0 && i != ID) {
NetworkTransport.Send(hostID, clientArray[i], unreliableChannel, buffer, BYTE_SIZE, out error);
}
}
}
This is the function that calls the actual NetworkTransport.Send(). It calls Send up to 3 times per run (Depending on number of connections). And is in turn called by another function up to 4 times (With different positions in "msg" every time)
I must be missing something obvious, but the documentation on LLAPI is scarce enough that I can't find it. If anyone has any experience with this, any help would be greatly appreciated.
Thanks!
Answer by GetLitGames · May 08, 2020 at 03:26 PM
Unfortunately Unity's built in networking is not worth trying to use and is abandoned/unsupported. You should invest your valuable time into Photon 2, Photon Bolt (my recommendation if you are a veteran developer), possibly Forge or anything else that has a good base, support, and online examples.
The safest bet is on Photon 2 while Photon Bolt is also very good but is a little more for advanced programmers and not as well documented/supported/google-able, and I don't know much about Forge but I believe it's very popular as well.
Thanks for the answer. In your opinion which of the systems is the most flexible and gives the developers the most control?
Well Forge is the most flexible and gives you full control, but you would have to setup your own Relay servers. If you are a skilled developer, then Photon Bolt otherwise Photon or Forge.
The right one for you depends on several factors like the type of game and hosting needed. If you need full server auth control, then Photon Bolt but it is also targeted mostly at FPS in that respect too. So if you are making an FPS, then Photon Bolt is the strongest choice especially if you want full server auth to reduce cheating. However, Photon Bolt could be used for a full server auth $$anonymous$$OBA like LoL as well - it's not just for FPS but it has things that are big benefits for FPS.
It's not a good idea to deploy a game without reliable high quality stable servers but it would work for indie games. Ultimately, setting up your own relays will cost money and if your game became that popular then cost shouldn't be an issue and having your own "free" relay servers that you have to control and manage will just be a drawback and not a plus.
However, if you intend to have dedicated servers and your game will be built around static dedicated servers then you don't need relay. Relay is for games where you want the player to be able to run a local host and have people join (like $$anonymous$$ecraft, etc). Forge supports NAT punch but that is not 100%, its more like 80% which means if you rely on hole punch you would get plenty of people unable to connect to a locally started host and if you entire game is built around that, then that's a big problem.
Well thank you again for taking the time to answer! I'll do some reading of my own on the subject as well. The gist of it is that I'm looking for a networking solution that will in the end allow me to have dedicated servers, a message structure that I create to allow per-client encryption, and full server authority over the synchronization of the game between players. So that client side cheating has no effect on the server, and any intercepted packets between a server and a client are only readable to the intended recipient. At least that's the plan, I'm by no means a veteran developer and this is the first time I'm dealing with multiplayer, but we'll see how it goes ;)