Toemsel / Network

C# Network Library

Home Page:https://push-force.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RegisterStaticPacketHandler in ClientConnectionContainer only works on the first declared handler

lasse8290 opened this issue · comments

The code is as follows

        static void Main(string[] args) {

            //1. Establish a connection to the server.
            container = ConnectionFactory.CreateClientConnectionContainer("127.0.0.1", 5560);
            //2. Register what happens if we get a connection
            container.ConnectionEstablished += ConnectionEstablished;
            container.AutoReconnect = false;
            container.ConnectionLost += Container_ConnectionLost;

            Console.ReadKey();

        }

private static void ConnectionEstablished(Connection connection, ConnectionType type) {
            Console.WriteLine($"{type.ToString()} Connection established to 
{connection.IPRemoteEndPoint}");

            //Register Packet Handler for the types of incoming data

            connection.ConnectionClosed += (CR, C) => { Console.WriteLine($"{CR} {C}"); };

            
            connection.RegisterStaticPacketHandler<ResponsePacket1>(async (p, c) => {
                Console.WriteLine("ResponsePacket1");

            });

            connection.RegisterStaticPacketHandler< ResponsePacket2>(async (p, c) => {

                Console.WriteLine("ResponsePacket2");

            });
connection.Send(new RequestPacket1());

connection.Send(new RequestPacket2());

the server reads and send the packet fine but when client gets a response only the first declared handler is called
Any help as to why this doesn’t work would be greatly appreciated

The client shouldn't use the RegisterStaticPacketHandler. Please have a look at the examples

            //3. Register what happens if we receive a packet of type "CalculationResponse"
            connection.RegisterPacketHandler<CalculationResponse>((response, con) => Console.WriteLine($"Answer received {response.Result}"), this);
            //4. Send a calculation request.
            connection.Send(new CalculationRequest(10, 10), this);

In case you didnt find the examples: https://github.com/Toemsel/Network/tree/master/Examples

the issue presits even when using the non-static handler

Please note the "this" within the "connection.Send" and the "RegisterPacketHander".
In your provided example you are missing the "this" instance.

ah thank you that seemed to be the problem