Comments (5)
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.
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.
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.
I've just realized static virtual members were a thing so I guess the extension is really not needed.
from sm_server.
Closed by 69df77f
from sm_server.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sm_server.