Search Unity

Showcase Mirror - Open Source Networking for Unity

Discussion in 'Multiplayer' started by mischa2k, Aug 11, 2016.

  1. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    Since I don't know much about this, I am guessing it is not a good idea to have platforms work with WS and it is just an exception to work with it for WebGL. Like why can't Standalone or mobile just work with WS as well? Don't they have that option inherently?
     
  2. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    The LLAPI protocol is normally on top of the UDP layer. UDP doesn't do packet ordering or guaranteed delivery by itself, which is actually a good thing when writing game networking. For network intensive stuff like games, you want to handle those jobs yourself for optimal performance and bandwidth, which is part of what the LLAPI optionally does for you. WS is a tcp-based protocol, so is going to have the overhead of tcp without the benefit. That's a good enough reason to default to UDP for non webGL for network intensive-games. If your requirements are more relaxed (like a board game) you may find that much of the HLAPI is irrelevant for you anyway, in which case you could probably use a normal C# websocket connection and you'd manage the messages and such yourself.

    But yeah...it would be nice to have both right there in the HLAPI. The modifications are what I described above, which are not too hard, and probably only minor adjustments to downstream code for most projects. I fussed around trying to find a way to do this and keep it plug-in compatible with existing HLAPI code, but the performance cost and weird hidden changes required were sure to cause bugs and confusion in some code anyway, so it didn't seem worth it compared to the ease of just changing how you call SendMessage (probably not the actual method name, I forget ;) You have to specify a host id as well as a connection id, but luckily the NetworkConnection object contains both of those ids.

    ---edit----
    The Master Server Framework (not my project) linked to in my sig is an example of how to use websockets for your client code even in a desktop client. Among other things, it's a messaging API, so if you aren't doing heavy syncing, it might be helpful to you.
     
    Last edited: Dec 12, 2017
  3. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    I see. Thanks a lot for your explanation and advice. Gotta reevaluate my expectations with the new knowledge and decide what I'll do.

    Thank you very much again! :)
     
  4. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    To keep everyone up to date: HLAPI Pro 2017.3 update will come soon...
     
    Deleted User likes this.
  5. Deleted User

    Deleted User

    Guest

    Thanks!
     
  6. hyperhuzaifa

    hyperhuzaifa

    Joined:
    Aug 21, 2015
    Posts:
    9
    Looking forward!
     
  7. jessejarvis

    jessejarvis

    Joined:
    Aug 9, 2013
    Posts:
    303
    Yay! Does this mean that 2017.3 will be stable with uMMORPG?

    Like for example, the bug that 2017.2 had, is that gone, with the UI?
     
  8. Wizkunde

    Wizkunde

    Joined:
    Sep 29, 2017
    Posts:
    1
    Good morning!

    Thanks for your elaborate description and providing clarity on the hlapi.

    I'm about to release a new mmorpg AR game and bought + heavily modified your ummorpg asset as a fundament. I used it mainly as a quick learning tool, to check the pitfalls and went from that. We're now 6 months on the way and are releasing in a couple of weeks. Exciting and it was a hardcore road!

    I've got a couple of suggestions for uMMORPG based on my countless debug sessions, hands-on experience and on what I read here so far. Ill send the uMMORPG part to you in a PM.

    This is my only networking based remark:

    Syncvar limit
    At first, the syncvar limit is something I ran into quite fast (since your Entity.cs already came in around 32 vars per override for player, monster and npc.). At first I raised it but later, as I went, I realised the concept of single responsibility in code is more important than having one main class which handles everything. So I separated out death management, aggro management (built this from scratch), combat mechanics, buff/debuffs. Which made every script on its own only have a reasonable amount of syncvar and lists instead of needing to raise the cap.

    This comes forth out of the basic principles of SOLID and if you apply these principles, i think there is little chance that the 32 syncvar limit would ever become a problem.

    Keep it up and sending you the uMMORPG suggestions in a short moment.

    Best Regards,

    Ron
    Wizkunde
     
  9. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Didn't try 2017.3 yet. I usually wait until Unity releases a few patches to fix the new bugs.

    Thanks
     
  10. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Progress: finished the 2017.3 code update. Downloading Unity 2017.3 over night and will test + release any time soon then.
     
    Deleted User likes this.
  11. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Update: HLAPI Pro 2017.3 was released!
    Code is on Bitbucket, DLL download links can be found in the first post.

    Enjoy!
     
  12. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    768
    Are you going to update the fixes & improvements version as well or the 2017.2 version can also be used in 2017.3?
     
  13. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    After finishing some uMMORPG updates. The bugfix-only version should do until then I hope.
     
  14. RajulG

    RajulG

    Joined:
    Jan 2, 2017
    Posts:
    3
    Thanks for the 2017.3 version!
    Can you make [SyncVar(bool onlyForOwner)] make work for Server spawned player authorized objects as well ? Thanks
     
  15. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Maybe in the future. I am more focused on bug fixes / maintenance at the moment and don't want to break compatibility with the built in HLAPI.
     
  16. BARKx4

    BARKx4

    Joined:
    Mar 29, 2017
    Posts:
    12
    I'm having a strange issue. Downloaded 2017.1 full release, followed the Install instructions for Windows, launched Unity project.. and Im getting this error at a rate of 1 per second:

    FileNotFoundException: Could not load file or assembly 'Unity.Cecil, Version=0.10.0.0, Culture=neutral, PublicKeyToken=fc15b93552389f74' or one of its dependencies.
    UnityEditor.Scripting.Serialization.Weaver.WeaveUnetFromEditor (IEnumerable`1 islands, System.String assemblyPath, System.String destPath, System.String unityEngine, System.String unityUNet, Boolean buildingForEditor) (at C:/buildslave/unity/build/Editor/Mono/Scripting/Serialization/Weaver.cs:80)
    UnityEditor.Scripting.ScriptCompilation.EditorCompilation+<CompileScripts>c__AnonStorey4.<>m__2 (UnityEditor.Scripting.ScriptCompilation.ScriptAssembly assembly, System.Collections.Generic.List`1 messages) (at C:/buildslave/unity/build/Editor/Mono/Scripting/ScriptCompilation/EditorCompilation.cs:437)
    UnityEditor.Scripting.ScriptCompilation.CompilationTask.Poll () (at C:/buildslave/unity/build/Editor/Mono/Scripting/ScriptCompilation/CompilationTask.cs:109)
    UnityEditor.Scripting.ScriptCompilation.EditorCompilation.TickCompilationPipeline (EditorScriptCompilationOptions options, BuildTargetGroup platformGroup, BuildTarget platform) (at C:/buildslave/unity/build/Editor/Mono/Scripting/ScriptCompilation/EditorCompilation.cs:560)
    UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface.TickCompilationPipeline (EditorScriptCompilationOptions options, BuildTargetGroup platformGroup, BuildTarget platform) (at C:/buildslave/unity/build/Editor/Mono/Scripting/ScriptCompilation/EditorCompilationInterface.cs:122)

    I'm positive Weaver is where yousaid to put it:



    (E: in my case, because its where Unity is installed)
    Any ideas?
     
  17. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Try these: https://bitbucket.org/Unity-Technologies/networking/branches/ and see if the same happens or not, then we know more. 2017.1 HLAPI Pro definitely works for me and several others.
     
  18. BARKx4

    BARKx4

    Joined:
    Mar 29, 2017
    Posts:
    12
    Can confirm it still happens with those too.. Odd. It didn't show up until I went through the HLAPI install process though.
     
  19. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Did you replace all 4 DLLs?
     
  20. BARKx4

    BARKx4

    Joined:
    Mar 29, 2017
    Posts:
    12
    Yeah, I wound up having to just install Unity 2017.3 to fix it all back up. I'll wait until the .3 improvements+fixes version is released to try this again, thanks though! :)
     
  21. Satanmist

    Satanmist

    Joined:
    Jun 7, 2017
    Posts:
    2
    Are there any plans on implementing host migration with relay servers?
    Unity apparently doesn't care about it, as it's marked ToDo for quite a while now.
     
  22. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Only bugfixes atm
     
  23. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    Code (CSharp):
    1.  
    2.         void GenerateError(byte error) // vis2k: byte instead of int
    3.         {
    4.             NetworkMessageDelegate msgDelegate = m_MessageHandlers.GetHandler(MsgType.Error);
    5.             if (msgDelegate == null)
    6.             {
    7.                 msgDelegate = m_MessageHandlers.GetHandler(MsgType.Error);
    8.             }
    9.             if (msgDelegate != null)
    10.             { ... }

    ok ok :p
     
  24. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    There is more :)
     
    Deleted User likes this.
  25. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    More of what ? ^^

    I'm digging into this HLAPI from Unet, and.. I am rewritting it :s

    -No multiple servers possible.
    -No command/rpc on differents projects.

    NetworkClient seems useless.
    NetworkServer is bottleneck.
    ClientScene seems useless.

    Boring Rpc/TargetRpc/Command function to write the same behaviour, just changing the target ... (and you know what ? if you want to change the channel some time ? WRITE AN OTHER FUNCTION)

    And .. more T-T

    Thanks to you for the improvement. I'm trying to make improvement on improvement xD.
     
  26. Peter-Dijkstra

    Peter-Dijkstra

    Joined:
    Feb 15, 2013
    Posts:
    8
    Hi vis2k!

    I'm currently working on a networked Hololens application (my first networked application in general, hoo boy), and I found your fixes and improvements and wanted to use these in my projects; especially the "ServerDisconnected due to error: Timeout" bugfix. However, when trying to build my project, I get a whole bunch of "Reference Rewriter" errors. Any idea if I can fix this?

    You can find the relevant part of my editor log output here: https://pastebin.com/kw3dvbkY. BuildPipeline.BuildPlayer() spits this out. The buildplatform Hololens uses is UWP x86.

    Thanks in advance!!
     
  27. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Does it happen with the original hlapi sources too? https://bitbucket.org/Unity-Technologies/networking/branches/
     
  28. Peter-Dijkstra

    Peter-Dijkstra

    Joined:
    Feb 15, 2013
    Posts:
    8
  29. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I mean the ones from the link above. Try those please and see if you get the error or not.
     
  30. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    Found a bug, maybe you have it (surely because i see no handle for this in your code).

    When you stop client or server, in fact... you don't stop it at all but you ... disconnect it !

    In StopServer and StopClient Unet shutdown the transport layer. So the last message of disconnecting the server/client is not send. The side effect is that other client/server have to wait the timeout to care about your disconnection. If you shutdown or removeHost some time after StopClient/StopServer you will have an instant disconnection to all clients.
     
  31. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    Sorry but both of those are a bummer to use. HL just feels to strange and I can't quite grasp it with server and client code mixed together and LL it just seems you CANNOT find some simple example...
     
  32. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I am just providing a fixed version of HLAPI, no changed functionality
     
  33. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    @vis2k Do you know what's the purpose of channel buffer ? NetworkTransport already have a queue system.
    Same for delay on NetworkServer/NetworkClient .. NEtworkTransport already have it (and In my version I removed this, it was a lot confusing and ..meh..usless ?)
     
  34. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    Code (CSharp):
    1.   public void FinishMessage()
    2.         {
    3.             // jump to zero, replace size (short) in header, jump back
    4.             long oldPosition = stream.Position;
    5.             ushort sz = (ushort)(Position - (sizeof(ushort) * 2)); // length - header(short,short)
    6.  
    7.             SeekZero();
    8.             Write(sz);
    9.             stream.Position = oldPosition;
    10.         }
    Wrong no ? Because Position is in byte unit, so sizeof(ushort)*2 is way to much no ?
     
  35. Xype

    Xype

    Joined:
    Apr 10, 2017
    Posts:
    339
    Without digging around to see everything involved with what you are asking about @Driiades I think what you may be seeing if that came from HLAPI pro, the HLAPI had the old fashioned long process breaking down everything into a bits and going around the elbow to wipe the behind method. Vis replaced it with memorystream. You can do Memorystream(int32) which is 2 shorts short *2 :)
     
  36. Driiade

    Driiade

    Joined:
    Nov 21, 2017
    Posts:
    80
    Oh yeah it's correct. I made a mistake because in my own header I didn't include the msgType :).
     
  37. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    166
    Suggestion: make all serialized integer values varints (a la Read/WritePackedInt*). Currently serializing signed shorts, ints, and longs just dumps and reads the raw values into to the network buffer. In exchange for a bit more CPU time, you can encode signed integers as unsigned integers, which can be serialized as packed varints, thereby reducing general message size. This is done using zig-zag encoding of signed integer values. https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba

    Also would suggest adding general utilities for compressing messages by constraining floating point values to specific ranges, converting to fixed point, changing NetworkReader/NetworkWriter to properly bitpack boolean values, etc.
     
  38. aranthel09

    aranthel09

    Joined:
    Jul 17, 2015
    Posts:
    66
    Heys vis!

    I'd like to know if it's possible to change SyncVar limit from 64 to 128. HLAPI Pro is a must to me since I end up using a lot of SyncVars.
     
  39. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Yes but that will be a bit more difficult since it uses 'long' right now. You'd need two longs or a byte buffer then.
     
  40. RayKatz

    RayKatz

    Joined:
    Oct 18, 2013
    Posts:
    13
    What's the difference between fixes only and fixes&improvements?
     
  41. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Fixes & improvements version has some improvements too
     
  42. RayKatz

    RayKatz

    Joined:
    Oct 18, 2013
    Posts:
    13
    y-yes, I suspected, haha. Does that mean "fixes" is all the things listed and "improvements" is some optimized stuff?
     
  43. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Sorry, the first page features list was all for the improvements version.
    Fixes just have fixes, I didn't list them anywhere. Check out the commit history in the repository to get the idea.

    In other words: use the fixes-only version for all your UNET (HLAPI) projects and you'll be very happy. There's no risk, no different architecture, only less bugs.
     
  44. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    chiapet1021 likes this.
  45. aranthel09

    aranthel09

    Joined:
    Jul 17, 2015
    Posts:
    66
    Sorry to ask for it here, but I just can't seem to resolve it nor get where it goes wrong.


    Code (CSharp):
    1.  
    2. System.IndexOutOfRangeException: NetworkReader:ReadByte out of range:NetBuf sz:0 pos:0
    3.   at UnityEngine.Networking.NetBuffer.ReadByte () [0x00000] in <filename unknown>:0
    4.   at UnityEngine.Networking.NetworkReader.ReadByte () [0x00000] in <filename unknown>:0
    5.   at UnityEngine.Networking.NetworkReader.ReadPackedUInt32 () [0x00000] in <filename unknown>:0
    6.   at PlayerBase.OnDeserialize (UnityEngine.Networking.NetworkReader reader, Boolean initialState) [0x00000] in <filename unknown>:0
    7.   at PlayerStats.OnDeserialize (UnityEngine.Networking.NetworkReader reader, Boolean initialState) [0x00000] in <filename unknown>:0
    8.   at UnityEngine.Networking.NetworkIdentity.OnDeserializeAllSafely (UnityEngine.Networking.NetworkBehaviour[] components, UnityEngine.Networking.NetworkReader reader, Boolean initialState) [0x00000] in <filename unknown>:0
    9. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    10.  
    I know it's just "write and read is just not equal" but I'm pretty lost about this. I mean, on Editor it runs perfectly, but on client it simply shows this error
     
  46. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    If it happens on the client then try running the client in the editor so you get that detailed logging to know what's up.
     
  47. aranthel09

    aranthel09

    Joined:
    Jul 17, 2015
    Posts:
    66
    Done it. It gives me 2 errors, now that I can see in detail.


    Code (CSharp):
    1. OnDeserialize failed for: object=Player(Clone) component=PlayerStats sceneId=0 length=0. Possible Reasons:
    2.   * Do PlayerStats's OnSerialize and OnDeserialize calls write the same amount of data(0 bytes)?
    3.  * Was there an exception in PlayerStats's OnSerialize/OnDeserialize code?
    4.   * Are the server and client the exact same project?
    5.   * Maybe this OnDeserialize call was meant for another GameObject? The sceneIds can easily get out of sync if the Hierarchy was modified only in the client OR the server. Try rebuilding both.
    6.  
    7. System.IndexOutOfRangeException: NetworkReader:ReadByte out of range:NetBuf sz:0 pos:0
    8.   at UnityEngine.Networking.NetBuffer.ReadByte () [0x00000] in <filename unknown>:0
    9.   at UnityEngine.Networking.NetworkReader.ReadByte () [0x00000] in <filename unknown>:0
    10.   at UnityEngine.Networking.NetworkReader.ReadPackedUInt32 () [0x00000] in <filename unknown>:0
    11.   at PlayerBase.OnDeserialize (UnityEngine.Networking.NetworkReader reader, Boolean initialState) [0x00000] in <filename unknown>:0
    12.   at PlayerStats.OnDeserialize (UnityEngine.Networking.NetworkReader reader, Boolean initialState) [0x00000] in <filename unknown>:0
    13.   at UnityEngine.Networking.NetworkIdentity.OnDeserializeAllSafely (UnityEngine.Networking.NetworkBehaviour[] components, UnityEngine.Networking.NetworkReader reader, Boolean initialState) [0x00000] in <filename unknown>:0
    14. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    15.  
    About this 1st one:
    - I didn't call any OnSerialize/OnDeserialize by myself. Since it says (0 bytes), I GUESS there's no desync between then.
    - Again, I didn't write any OnSerialize/OnDeserialize by myself
    - Yes, server and client are same project.
    - I'm not even changing scenes.


    Code (CSharp):
    1. IndexOutOfRangeException: NetworkReader:ReadByte out of range:NetBuf sz:2 pos:2
    2. UnityEngine.Networking.NetBuffer.ReadByte ()
    3. UnityEngine.Networking.NetworkReader.ReadUInt16 ()
    4. UnityEngine.Networking.NetworkReader.ReadBytesAndSize ()
    5. UnityEngine.Networking.NetworkIdentity.OnDeserializeAllSafely (UnityEngine.Networking.NetworkBehaviour[] components, UnityEngine.Networking.NetworkReader reader, Boolean initialState)
    6. UnityEngine.Networking.NetworkIdentity.OnUpdateVars (UnityEngine.Networking.NetworkReader reader, Boolean initialState)
    7. UnityEngine.Networking.ClientScene.ApplySpawnPayload (UnityEngine.Networking.NetworkIdentity uv, Vector3 position, System.Byte[] payload, NetworkInstanceId netId, UnityEngine.GameObject newGameObject)
    8. UnityEngine.Networking.ClientScene.OnObjectSpawn (UnityEngine.Networking.NetworkMessage netMsg)
    9. UnityEngine.Networking.NetworkConnection.HandleReader (UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId)
    10. UnityEngine.Networking.NetworkConnection.HandleBytes (System.Byte[] buffer, Int32 receivedSize, Int32 channelId)
    11. UnityEngine.Networking.NetworkConnection.TransportReceive (System.Byte[] bytes, Int32 numBytes, Int32 channelId)
    12. UnityEngine.Networking.NetworkClient.Update ()
    13. UnityEngine.Networking.NetworkClient.UpdateClients ()
    14. UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate ()
    And about this 2nd one I have no ideia where to begin with

    Oh, and again, I'm sorry to ask for help here, those kind of errors are really hard to find anything about and you're like the UNet guru atm
     
    Last edited: Feb 17, 2018
  48. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Can you post your PlayerStats script?
     
  49. aranthel09

    aranthel09

    Joined:
    Jul 17, 2015
    Posts:
    66
    Hey vis, I managed to solve it! While analyzing stuff, I found out I had 2 big problems:

    1st- calling a Debug.ErrorLog and had the option "Error Pause" marked.
    2nd- I had a child gameObject on Player that had its own NetworkIdentity. So, double NetIDs. After removing it, error went away.

    Now, I ran on a design issue: I was using this child prefab named GameController which was called on PlayerStats using a method that calls a CmdMethod and this CmdMethod calls a RpcMethod which called GameController that sits on Player prefab. This GameController was responsible to deal damage and receive damage through [Server] methods. Now with this GameController gone, I don't know how to do it, since it doesn't seem safe to me to allow a Player to directly call CmdMethods to deal damage. Is that right? Do you know how can I dodge this issue? Maybe using a Singleton GameController (does this even work on UNet?)

    Tyvm in advance for everything!
     
  50. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Not 100% sure what you mean, but you should probably read through the UNET manual / try asking that question in the networking forum and not on the HLAPI Pro thread :)
     
    aranthel09 likes this.