示例#1
0
 public void HandleBadPacket(NebulaUser user)
 {
     user.BadPackets++;
     if (user.BadPackets >= MaximumUserBadPackets)
     {
         user.Peer.Disconnect(NetDataWriter.FromString($"Too many bad packets ({user.BadPackets})"));
     }
     WriteLine($"Handled bad packet for user '{user.Id}'. User has a total of {user.BadPackets} bad packets.", ConsoleColor.Red);
 }
示例#2
0
        public override void OnConnectionRequest(ConnectionRequest request)
        {
            WriteLine($"Connection Request from '{request.RemoteEndPoint.Address}:{request.RemoteEndPoint.Port}'", ConsoleColor.Cyan);
            if (NetManager.ConnectedPeersCount >= MaximumConnections)
            {
                request.Reject(NetDataWriter.FromString("Server Full"));
                WriteLine($"Refused Connection from '{request.RemoteEndPoint.Address}:{request.RemoteEndPoint.Port}'. Reason: Server Full", ConsoleColor.DarkRed);
                return;
            }

            if (!String.IsNullOrWhiteSpace(ServerKey))
            {
                request.AcceptIfKey(ServerKey);
            }
            else
            {
                request.Accept();
            }
        }
示例#3
0
        public override void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod)
        {
            NebulaUser user = GetUser(peer.Id);

            if (user == null)
            {
                peer.Disconnect(NetDataWriter.FromString("User not registered server side"));
                WriteLine($"Received packet from '{peer.EndPoint.Address}:{peer.EndPoint.Port}' but the user is not registered.", ConsoleColor.Red);
                return;
            }

            try
            {
                PacketProcessor.ReadAllPackets(reader, user);
            }
            catch
            {
                HandleBadPacket(user);
            }
        }
示例#4
0
        public void OnConnectionRequest(ConnectionRequest rq)
        {
            try
            {
                PlayerAuth data = new PlayerAuth(rq.Data);
                //This will throw an exception in case of protocol error
                data.Validate();

                _gm.AddPlayer(rq.Accept(), data);
            }
            catch (Exception e)
            {
                if (e is PlayerAuthException)
                {
                    rq.Reject(NetDataWriter.FromString(e.Message));
                    return;
                }

                Console.WriteLine(e);
                rq.Reject(NetDataWriter.FromString("An unknown error occured"));
            }
        }
示例#5
0
 /// <summary>
 /// Connect to remote host
 /// </summary>
 /// <param name="target">Server end point (ip and port)</param>
 /// <param name="key">Connection key</param>
 /// <returns>Null if connections limit reached, New NetPeer if new connection, Old NetPeer if already connected</returns>
 /// <exception cref="InvalidOperationException">Manager is not running. Call <see cref="Start()"/></exception>
 public NetPeer Connect(NetEndPoint target, string key)
 {
     return(Connect(target, NetDataWriter.FromString(key)));
 }
示例#6
0
 public override void Disconnect(string reason)
 {
     _socket.Disconnect(NetDataWriter.FromString(reason));
 }
示例#7
0
 public void Disconnect(string reason)
 {
     Disconnect(NetDataWriter.FromString(reason));
 }
示例#8
0
        public override void Run()
        {
            var     connected = false;
            NetPeer?peer      = null;

            Console.WriteLine("Stating Client...");
            RequestRelayServerIp();

            var serverIp = RequestServerIp();

            var globalServer = new IPEndPoint(IPAddress.Parse(RelayServerIp), 4240);

            var natPunchListener = new EventBasedNatPunchListener();

            natPunchListener.NatIntroductionSuccess += (point, _, _) =>
            {
                Console.WriteLine("Nat Introduction Success, Connecting to " + point);
                peer      = Net.Connect(point, "CSM");
                connected = true;
            };

            var netListener = new EventBasedNetListener();

            netListener.NetworkReceiveEvent += (peer, reader, method) =>
            {
                var message = reader.GetString();
                Console.WriteLine($"[{peer}] {message}");
            };

            netListener.PeerConnectedEvent += netPeer =>
            {
                Console.WriteLine("Connected to server: " + netPeer.EndPoint);
            };

            netListener.NetworkErrorEvent += (point, error) =>
            {
                Console.WriteLine("Network Error: " + error);
            };

            Net = new NetManager(netListener);
            Net.NatPunchEnabled            = true;
            Net.UnconnectedMessagesEnabled = true;
            Net.NatPunchModule.Init(natPunchListener);

            Net.Start();
            Net.NatPunchModule.SendNatIntroduceRequest(globalServer, $"client_{serverIp}");

            var running = true;

            while (running)
            {
                Net.NatPunchModule.PollEvents();
                Net.PollEvents();

                // Wait till connected
                if (peer == null)
                {
                    Thread.Sleep(100);
                    continue;
                }

                Console.WriteLine("Sending ping:");
                peer.Send(NetDataWriter.FromString("Ping from client"), DeliveryMethod.Unreliable);

                Thread.Sleep(100);
            }

            Net.Stop();
        }
示例#9
0
        public override void Run()
        {
            Console.WriteLine("Stating Server...");
            RequestRelayServerIp();

            var globalServer = new IPEndPoint(IPAddress.Parse(RelayServerIp), 4240);

            var natPunchListener = new EventBasedNatPunchListener();

            natPunchListener.NatIntroductionSuccess += (point, _, _) =>
            {
                Console.WriteLine("Nat Introduction Success, Accepting connection from " + point);
            };

            var netListener = new EventBasedNetListener();

            netListener.NetworkReceiveEvent += (peer, reader, method) =>
            {
                Console.WriteLine("Server received: " + reader.GetString() + ". Sending pong...");
                peer.Send(NetDataWriter.FromString("Pong from server!"), DeliveryMethod.ReliableOrdered);
            };

            netListener.PeerConnectedEvent += peer =>
            {
                Console.WriteLine("Client connected to server: " + peer.EndPoint);
            };

            netListener.ConnectionRequestEvent += request =>
            {
                Console.WriteLine("Connection request from: " + request.RemoteEndPoint);
                request.AcceptIfKey("CSM");
            };

            netListener.NetworkErrorEvent += (point, error) =>
            {
                Console.WriteLine("Network Error: " + error);
            };

            Net = new NetManager(netListener);
            Net.NatPunchEnabled            = true;
            Net.UnconnectedMessagesEnabled = true;
            Net.NatPunchModule.Init(natPunchListener);

            var success = Net.Start(4230);

            if (!success)
            {
                Console.WriteLine("Could not start server for some reason");
            }

            Net.NatPunchModule.SendNatIntroduceRequest(globalServer, "server_a7Gd3H");

            var running = true;

            while (running)
            {
                Net.NatPunchModule.PollEvents();
                Net.PollEvents();

                // This would be sent every 5 seconds in the actual game
                Net.SendUnconnectedMessage(NetDataWriter.FromString("server_a7Gd3H"), globalServer);

                Thread.Sleep(100);
            }

            Net.Stop();
        }