Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Client/Server Network State-Synchronization for ECS

Discussion in 'Entity Component System' started by e199, Nov 6, 2018.

  1. e199

    e199

    Joined:
    Mar 24, 2015
    Posts:
    101
    Hey!

    I found that solution for synchronization : https://github.com/ashoulson/RailgunNet
    It's OOP heavy, a lot of callbacks, polymorphism and so on..

    One the most important thing there is State - https://github.com/ashoulson/RailgunNet/blob/master/RailgunNet/Logic/RailState.cs
    Which can be implemented like this - https://github.com/ashoulson/NetDemo/blob/master/Include/GameLogic/EntityState.cs

    In implementation you see how all information about entity is stored in a single class, it makes it easy to send that state, calculate delta between states, store it, etc.

    But when we switch to ECS, then we can have unknown amount of components on an entity, which can be added/removed/changed.. how would you approach it?

    Then there is scoping/priorities/freezing/deltacompression.
    Imagine Client1 knows state of entity1 at 11 frame, state of entity2 at 5 frame. Client2 knows state of entity1 at 10 frame, state of entity3 at 12 frame.
    Server should keep around 60 frames (1 second) of world states in memory to do delta compression. (We can do it with multiple ECS worlds and duplicating all entities at the end of frame to those worlds)
    And it should track which frame of that particular entity is known to each client (DynamicBuffer probably)

    How would you go about making it? With support for client-prediction and scoping?

    (I know about FPS sample and it's networking is hacky https://forum.unity.com/threads/ecs-world-replication.574528/#post-3848839 )
     
  2. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    954
    Pretty much all that you are asking has been done in the FPSSample. They use the ReplicatedEntity system and PredictedStates to give you a starting point.
     
  3. e199

    e199

    Joined:
    Mar 24, 2015
    Posts:
    101
  4. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Hi,

    I wrote a network framework for ECS some time ago:
    https://github.com/Spy-Shifty/BBSNetworkSystem

    This Framework will autosync all entities with the corosponding components.

    It synchronizes the creation and destruction of entities and their components.
    So you can dynamically add and remove components on an synchronized entity.

    You can define the level of sync. Which components of your entity and what member of your component will be synchronized and with what accuracy.

    It provides an interface for your network solution. So it's independend of the your backend. You can use it as peer to peer solution with a Masterclient concept and host migration system, as well as an authoritative server solution.
    (host migration system is supported and implemented)

    It is optimized for network traffic (package size and numbers of messages).
    It uses modern techniques like delta compression and google's Protobuf library for minimizing package size.


    I'll start to rewrite the framework in a few days. So it will use the latest ecs api. The framework will be jobyfied and I will remove the reflection part. I also want to implement a rolle back mechanism.

    Feel free to use it in your project.
    I would appreciate a feedback
     
    Enzi likes this.
  5. e199

    e199

    Joined:
    Mar 24, 2015
    Posts:
    101
    I want authoritative client-server solution, PUN is not that helpful with that

    Also, there is no scoping/interest calculation and client prediction setup
     
  6. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    954
    Ok, sorry, missed that. I don't think it's hacky. Why do you think that? It doesn't have the best byte serialization but that can be added. Most use protobuf anyway.
    Spy-Shifty has a cool system that already has protobuf integrated. It certainly has an edge there so a good starting point.
     
  7. e199

    e199

    Joined:
    Mar 24, 2015
    Posts:
    101
    In the link I posted there is a comment from FPS sample creator, who thinks it's hacky too :p

    Protobuf generates a lot of garbage
     
  8. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    954
    So? One part is hacky/complicated and you think nothing is usable? It's just one small part of the bigger picture. The prediction and replicated system follows a thread of logic from the client/server loop to the network reads/writes that is exchangable/reusable in other code bases. The complete code of what you need is there. You won't get a complete package tailored to you. For that it's too soon as this is all beta and very new.

    Source?
     
  9. e199

    e199

    Joined:
    Mar 24, 2015
    Posts:
    101
    We use protobuf in production and it generates garbage, in my friend's company they used it too and atfer heavy allocations on mobiles they decided to write their own code generator for bit packing

    Sure, I'm looking at all solutions out there and do write my own using that experience.
    Just don't want to miss any better solutions for that kind of stuff.
     
  10. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Did you read my post?

    You can use it as Client Server as well. Just implement the Networkinterface for your backend...

    Prediction, Scoping/Interest calculation will be a part of the future release.

    EDIT:

    Which protobuf lib do you use?
     
  11. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    954
    I see. The garbage problem could be due to implementation though. I used it 5 years ago and I wasn't aware of produced garbage. Do you know anything about it Spy-Shifty as you are using it right now?

    Ever heard of MsgPack? Might be worth it if you target mobile. Should be quite fast.
    Problem about data serialization is that no really good generic ones exist. They always have some drawback.
     
  12. e199

    e199

    Joined:
    Mar 24, 2015
    Posts:
    101
    Oops, gonna check it out. Thank you

    I don't remember what lib we use. Gonna check it out when I get back to work.

    Yep, I checked out .net version + some other serializers from https://github.com/neuecc

    It was working, but I guess I'm used to bitpacking everything with custom code generators
     
  13. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    I can say protobuf-net don't produces garbage in my framework.

    Well it can produce some garbage, depending on your implementation....
    But if you reuse your data it won't do that.

    I use protobuf-net and I don't have garbage problems with it.
     
    Last edited: Nov 8, 2018