1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

Networking Framework

Discussion in 'Programming Released Codes' started by Hax, Aug 30, 2018.

?

Do you think that something like this, is usefull for SE modding?

  1. Yes.

    0 vote(s)
    0.0%
  2. No.

    0 vote(s)
    0.0%
Thread Status:
This last post in this thread was made more than 31 days old.
  1. Hax

    Hax Trainee Engineer

    Messages:
    2
    So this here is a basic networking framework, it is by no means optimized or fast, but it is somewhat easy to use.
    It detects if the game is multiplayer and does networking if it is, and just passes the values though if the game is singleplayer.

    So how is it used?

    Firstly the object needs made, this will make a handler on both the server and the client.
    Code:
    // First the script and class has to be included in project.
    using PepiHax.Networking;
    
    // And it needs to be made.
    private NetworkHandler NetworkHandler;
    
    // And then initilized. I do it when i initilize my script.
    public override void UpdateBeforeSimulation()
    {
    	  if (first)
    	  {
    		   if (MyAPIGateway.Session == null) return;
    		   NetworkHandler = new NetworkHandler((ushort)46646);
    The ushort is the registration id, that the handler registers itself with to SE.
    Just make it a random number.

    Functions can then be subscribed to the network handler with identifing strings.
    Code:
    NetworkHandler.Subscribe("ServerToStorage", ServerToStorage);
    NetworkHandler.Subscribe("ServerClientUpdate", ServerClientUpdate);
    NetworkHandler.Subscribe("ServerRemoveButton", ServerRemoveButton);
    NetworkHandler.Subscribe("ClientStoreUpdate", ClientStoreUpdate);
    The function takes a sender which is a ulong, and a list of objects.
    Code:
    void ServerToStorageUpdate(ulong sender, List<object> item)
    {
    On a triggered event data can then be sent to the server with.
    Code:
    void box2List(IMyTerminalBlock block, List<MyTerminalControlListBoxItem> controlListBoxItems, List<MyTerminalControlListBoxItem> controlListBoxItems2)
    {
    	[...]
    		List<object> list = new List<object>();
    		list.Add(block.EntityId);
    		NetworkHandler.SendToServer("ServerClientUpdate", list);
    In this case i'm just send the entity id of the block that triggered the event.

    And for updating clients, you can send to either a single client with.
    Code:
    void ServerClientUpdate(ulong sender, List<object> block)
    {
    	[...]
    	NetworkHandler.SendToClient(sender, "ClientStoreUpdate", temps);
    Or send it to all the clients with.
    Code:
    Networkhandler.SendToAllClients("ClientStoreUpdate", temps);
    temps in this case is a list of objects and will should be handed back to the function in the same order as they are put in.

    The handler currently only supports longs, strings, floats, ints and booleans.
    Putting any other type of value into the object list will cause it to throw an expection with "Unsupported type:".

    However variable support can be added by adding them to the DeSerlizate and Serlizate class in the networkhandler. ( I know the spelling sucks ).

    And last the message handler can be removed upon a game exit or save with the Remove function.
    Code:
    public override void UpdatingStopped() // This isnt the best place to do it, but i havent found a better place.
    {
    	 NetworkHandler.Remove((ushort)46646);

    Suggestions and comments are very welcome.

    The script can be found here.
    https://www.dropbox.com/s/98c1ke5oifcdtev/NetworkingClass.cs?dl=0
     
    Last edited: Jun 18, 2019
Thread Status:
This last post in this thread was made more than 31 days old.