Code Monkey home page Code Monkey logo

Comments (5)

arcturus-prime avatar arcturus-prime commented on August 25, 2024

Removed the reflection for packet IDs entirely and added the new constraint with commit 9c8fd5a and 412dc44. We don't really need the extension method, and we'd like to use the type, so we neglected to add that.

from sm_server.

aelsi2 avatar aelsi2 commented on August 25, 2024

412dc44:

No more reflection!

There's still reflection though (SmartSocket.OnMessage):

dynamic packet = Activator.CreateInstance(packetType);
//...
Type[] typeArgs = { typeof(Connection), packetType };
var callbackType = typeof(Action<,>).MakeGenericType(typeArgs);
//etc.

I was going to open a PR and close the issue myself.

from sm_server.

aelsi2 avatar aelsi2 commented on August 25, 2024

My thoughts were:
Add a field with the list of handlers by ids like this:

private readonly List<(byte packetId, Action<Connection, BinaryReader> handler)> handlers;

Make ReceivePacket add a delegate to the list:

public void ReceivePacket<T>(byte packetId, Action<Connection, T> handler) where T : IPacket, new()
{
    Action<Connection, BinaryReader> outerHandler = (connection, reader) =>
    {
        var packet = new T();
        packet.Deserialize(reader);
        handler(connection, packet);
    };
    handlers.Add((packetId, outerHandler));
}

Then in OnMessage iterate throught the list and call the handlers with the matching packetId:

// ...
foreach (var (curPid, callback) in callbacks)
{
    if (curPid == 0 || curPid == id)
    {
        callback(connection, dReader);
        dStream.Position = 0;
    }
}

An extension method would simplify the subscription:

public static void ReceivePacket<T>(this SmartSocket socket, Action<Connection, T> handler) where T : IPacket, new()
{
    byte packetId =  typeof(T).GetProperty(nameof(IPacket.PacketId), BindingFlags.Static | BindingFlags.Public)
        ?.GetValue(null) as byte? ?? throw new NotSupportedException();
    socket.ReceivePacket(packetId, handler);
}

from sm_server.

aelsi2 avatar aelsi2 commented on August 25, 2024

I've just realized static virtual members were a thing so I guess the extension is really not needed.

from sm_server.

aelsi2 avatar aelsi2 commented on August 25, 2024

Closed by 69df77f

from sm_server.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.