dimanche 15 mars 2020

C# Networking - How to register methods reliably

Ok this might be a tought one,

I am making my multiplayer transport layer for my game and i am getting at the RPC part, so what i need is to have methods that i can call over the network, immidiatly i thought of a List at least for the simple commands that return void, so now i can call the methods with an index number over the network but before i got to the part of actually making reliable/callbacks/threadsafe parts i am having a workflow problem...

How can i register those methods in a way so it is as easy as writing a method anywhere and initiating the network calling by a simple action like just calling the method, the goal is to avoid large code and make sure the methods are linked the same way for all players (ex: KickAllPlayers() should be at the same index on all machines)

To register the methods i thought of Reflection and Attributes, i just put attributes on my methods and at the instantiation of the Server class i get all the methods in the assembly that have the attribute and add them to the list

        [Rpc]
        public void KickAllPlayers()
        {
            throw new NotImplementedException();
        }
        public Server()
        {
            var taggedMethods = from t in Assembly.GetExecutingAssembly().GetTypes()
                                from m in t.GetMethods()
                                where m.GetCustomAttributes<RpcAttribute>().Any()
                                select m;

            foreach (var method in taggedMethods)
            {
                Rpc.Commands.Add((Action)Delegate.CreateDelegate(typeof(Action), null, method));
            }
        }

This is all well and good but now i need to be able to call those methods, i would have loved to be able to just call the methods like any other method KickAllPlayers(); but of course it requires that the method gets changed in some way so when KickAllPlayers(); gets called, under the hood the networking part works out the response from the Server and plays the method as a Callback answere. I have looked all day only to find that the AOP pattern might maybe do it...

So while still keeping the previous code i thought of making a static method responsible for doing the network part, something like

        Server.Call(<Action or Index>); // Send command request to server and return callback method

But now the stupid thing is that i can't rely on autocompletion to pass the right method to call since methods will be registered at runtime, i can probably generate yet an other list for method index but it this is horrible coding design and i'd like to avoid code generation.

Of course i could just write the methods in a ServerCommands class and we could problably call it a day but i had in mind to make this assembly loosely coupled so i could export it to a .dll and use it everywhere...

Is there a way to do this ?

Am i mad ?





Aucun commentaire:

Enregistrer un commentaire