GameHub ( Server )

mail@pastecode.io avatar
unknown
plain_text
4 years ago
2.1 kB
3
Indexable
Never





    public class GameHub
    {
        public static GameHub Instance
        {
            get
            {
                if (_Instance == default)
                {
                    _Instance = new GameHub();
                }

                return _Instance;
            }
        }

        private static GameHub _Instance;

        //Net stuff

        private NetManager Manager;

        private EventBasedNetListener Listener;

        private NetPacketProcessor Processor;


        private GameHub()
        {
            Console.WriteLine("Starting Server...");

            //Initialize stuff

            Listener = new EventBasedNetListener();

            Manager = new NetManager(Listener);

            Processor = new NetPacketProcessor();

            Listener.ConnectionRequestEvent += (Req) => 
            {
                Req.AcceptIfKey("Felix");
            };

            Listener.PeerConnectedEvent += (Client) => 
            {
                Console.WriteLine($"Client connected: {Client.Ping} ms");
            };

            Listener.NetworkReceiveEvent += (Client, Reader, Delivery) =>
            {
                Processor.ReadAllPackets(Reader, Client);
            };

            Processor.RegisterNestedType(Vector2Util.Serialize, Vector2Util.Deserialize);


            Processor.SubscribeReusable<UserIDReq, NetPeer>((x, y) => OnUserIDReq(x, y));


            Manager.Start(1258);

            _ = Loop();
        }

        private async Task Loop()
        {
            Manager.PollEvents();

            await Task.Delay(20);

            _ = Loop();
        }


        private void OnUserIDReq(UserIDReq Req, NetPeer Peer)
        {

            Req.UserID = 1234123412341234;

            Callback(Req, Peer);
        }

      

        //Callback


        public void Callback(object CB, NetPeer Peer, DeliveryMethod DM = DeliveryMethod.ReliableOrdered)
        {
            Processor.Send(Peer, CB, DM);
        }
    }