Esempio n. 1
0
        public void PacketError(ClientWrapper client, Exception exception)
        {
            if (PacketErrors.ContainsKey(client.ClientIdentifier))
            {
                int errors = PacketErrors[client.ClientIdentifier];
                PacketErrors[client.ClientIdentifier] = errors + 1;

                if (ServerSettings.DisplayPacketErrors)
                {
                    ConsoleFunctions.WriteWarningLine("Packet error for player: \"" + client.Player.Username + "\" Packet errors: " +
                                                      PacketErrors[client.ClientIdentifier] + "\nError:\n" + exception.Message);
                }

                if (PacketErrors[client.ClientIdentifier] >= 3)
                {
                    //RemoveClient(client);
                    if (ServerSettings.ReportExceptionsToClient)
                    {
                        new Disconnect(client) {Reason = new McChatMessage("You were kicked from the server!\n" + exception.Message)}.Write();
                    }
                    else
                    {
                        new Disconnect(client) { Reason = new McChatMessage("You were kicked from the server!") }.Write();
                    }
                    client.Kicked = true;
                }
            }
            else
            {
                //Something really wrong
            }
        }
Esempio n. 2
0
 protected Package(ClientWrapper client, DataBuffer buffer)
 {
     if (client == null) return;
     if (!client.TcpClient.Connected) return;
     Client = client;
     Stream = client.TcpClient.GetStream();
     Buffer = buffer;
 }
Esempio n. 3
0
 public void CleanErrors(ClientWrapper client)
 {
     if (PacketErrors.ContainsKey(client.ClientIdentifier))
     {
         PacketErrors[client.ClientIdentifier] = 0;
     }
     else
     {
         //Something really wrong
     }
 }
Esempio n. 4
0
        public PackageFactory(ClientWrapper client, DataBuffer buffer)
        {
            #region Ping

            PingPackages.Add(new Handshake(client, buffer));

            #endregion

            #region Login

            LoginPackages.Add(new EncryptionResponse(client, buffer));
            LoginPackages.Add(new LoginStart(client, buffer));

            #endregion

            #region Status

            StatusPackages.Add(new Request(client, buffer));
            StatusPackages.Add(new Ping(client, buffer));

            #endregion

            #region Play

            PlayPackages.Add(new ChatMessage(client, buffer));
            PlayPackages.Add(new Animation(client, buffer));
            PlayPackages.Add(new PlayerBlockPlacement(client, buffer));
            PlayPackages.Add(new HeldItemChange(client, buffer));
            PlayPackages.Add(new EntityAction(client, buffer));
            PlayPackages.Add(new PlayerAbilities(client, buffer));
            PlayPackages.Add(new PluginMessage(client, buffer));
            PlayPackages.Add(new KeepAlive(client, buffer));
            PlayPackages.Add(new PlayerPositionAndLook(client, buffer));
            PlayPackages.Add(new PlayerPosition(client, buffer));
            PlayPackages.Add(new PlayerLook(client, buffer));
            PlayPackages.Add(new OnGround(client, buffer));
            PlayPackages.Add(new ClientSettings(client, buffer));
            PlayPackages.Add(new PlayerDigging(client, buffer));
            PlayPackages.Add(new ClientStatus(client, buffer));
            PlayPackages.Add(new ClickWindow(client, buffer));
            PlayPackages.Add(new UseEntity(client, buffer));
            PlayPackages.Add(new CloseWindow(client, buffer));
            PlayPackages.Add(new UseItem(client, buffer));
            PlayPackages.Add(new CreativeInventoryAction(client, buffer));
            PlayPackages.Add(new UpdateSign(client, buffer));

            #endregion

            _client = client;
            _buffer = buffer;
        }
Esempio n. 5
0
        public bool HandleMessage(ClientWrapper client, DataBuffer buffer)
        {
            string raw = buffer.ReadString();
            string channel = raw.Split('|')[0];
            string command = raw.Split('|')[1];

            foreach (var msg in Messages)
            {
                if (msg.Key == channel)
                {
                    if (msg.Value.Command == command)
                    {
                        msg.Value.HandleData(client, buffer);
                    }
                    return true;
                }
            }
            return false;
        }
Esempio n. 6
0
 internal void RemoveClient(ClientWrapper client)
 {
     if (Clients.ContainsKey(client.ClientIdentifier))
     {
         Clients.Remove(client.ClientIdentifier);
         PacketErrors.Remove(client.ClientIdentifier);
         GC.Collect();
     }
 }
Esempio n. 7
0
 internal void AddClient(ref ClientWrapper client)
 {
     if (client.ClientIdentifier == -1)
     {
         CurrentIdentifier++;
         client.ClientIdentifier = CurrentIdentifier;
         Clients.Add(CurrentIdentifier, client);
         PacketErrors.Add(CurrentIdentifier, 0);
         ClientPing.Add(CurrentIdentifier, UnixTimeNow());
     }
 }
Esempio n. 8
0
 public void ReportPing(ClientWrapper client)
 {
     if (ClientPing.ContainsKey(client.ClientIdentifier))
     {
         ClientPing[client.ClientIdentifier] = UnixTimeNow();
     }
 }
Esempio n. 9
0
 public DataBuffer(ClientWrapper client)
 {
     _client = client;
 }
Esempio n. 10
0
 internal void BroadcastPlayerRemoval(ClientWrapper caller)
 {
     new PlayerListItem(caller)
     {
         Action = 4,
         Uuid = caller.Player.Uuid
     }.Broadcast(this);
 }
Esempio n. 11
0
        private void HandleClientCommNew(object client)
        {
            var tcpClient = (TcpClient) client;
            var clientStream = tcpClient.GetStream();
            var Client = new ClientWrapper(tcpClient);

            Globals.ClientManager.AddClient(ref Client);

            while (true)
            {
                try
                {
                    while (!clientStream.DataAvailable)
                    {
                        if (Client.Kicked)
                        {
                            break;
                        }
                        Thread.Sleep(5);
                    }

                    if (Client.Kicked)
                    {
                        break;
                    }

                    if (ServerSettings.UseCompression && Client.PacketMode == PacketMode.Play)
                    {
                        int packetLength = ReadVarInt(clientStream);
                        int dataLength = ReadVarInt(clientStream);
                        int actualDataLength = packetLength - GetVarIntBytes(dataLength).Length;

                        if (dataLength == 0)
                        {
                            if (!ReadCompressed(Client, clientStream, actualDataLength)) break;
                        }
                        else
                        {
                            if (!ReadUncompressed(Client, clientStream, dataLength)) break;
                        }
                    }
                    else
                    {
                        int dlength = ReadVarInt(clientStream);
                        if (!ReadUncompressed(Client, clientStream, dlength)) break;
                    }
                }
                catch (Exception ex)
                {
                    //Exception, disconnect!
                    ConsoleFunctions.WriteDebugLine("Error: \n" + ex);
                    if (ServerSettings.ReportExceptionsToClient)
                    {
                        new Disconnect(Client)
                        {
                            Reason = new McChatMessage("§fServer threw an exception!\n\nFor the nerdy people: \n" + ex.Message)
                        }.Write();
                    }
                    else
                    {
                        new Disconnect(Client)
                        {
                            Reason = new McChatMessage("§fYou were kicked because of an unknown problem!")
                        }.Write();
                    }
                    break;
                }
            }

            if (Client.Kicked)
            {
                new Disconnect(Client)
                {
                    Reason = new McChatMessage("§fYou were kicked because of a network problem!")
                }.Write();
            }

            //Close the connection with the client. :)
            Client.ThreadPool.KillAllThreads();
            //Client.StopKeepAliveTimer();

            if (Client.Player != null)
            {
                Client.Player.SavePlayer();
                Client.Player.Level.RemovePlayer(Client.Player.EntityId);
                Client.Player.Level.BroadcastPlayerRemoval(Client);
            }

            Client.TcpClient.Close();
            Globals.ClientManager.RemoveClient(Client);
            Thread.CurrentThread.Abort();
        }
Esempio n. 12
0
        private bool ReadUncompressed(ClientWrapper client, NetworkStream clientStream, int dlength)
        {
            var buffie = new byte[dlength];
            int receivedData;
            receivedData = clientStream.Read(buffie, 0, buffie.Length);
            if (receivedData > 0)
            {
                var buf = new DataBuffer(client);

                if (client.Decrypter != null)
                {
                    var date = new byte[4096];
                    client.Decrypter.TransformBlock(buffie, 0, buffie.Length, date, 0);
                    buf.BufferedData = date;
                }
                else
                {
                    buf.BufferedData = buffie;
                }

                buf.BufferedData = buffie;

                buf.Size = dlength;
                var packid = buf.ReadVarInt();

                if (!new PackageFactory(client, buf).Handle(packid))
                {
                    ConsoleFunctions.WriteWarningLine("Unknown packet received! \"0x" + packid.ToString("X2") + "\"");
                }

                buf.Dispose();
                return true;
            }
            else
            {
                return false;
            }
        }
Esempio n. 13
0
 public virtual void HandleData(ClientWrapper client, DataBuffer buffer)
 {
 }
Esempio n. 14
0
 internal void SetTarget(ClientWrapper client)
 {
     Client = client;
     Stream = client.TcpClient.GetStream();
     Buffer = new DataBuffer(client);
 }
Esempio n. 15
0
 public override void HandleData(ClientWrapper client, DataBuffer buffer)
 {
     string c = buffer.ReadString();
     ConsoleFunctions.WriteInfoLine(client.Player.Username + "'s client: " + c);
 }
Esempio n. 16
0
 public LocalDataBuffer(ClientWrapper client) : base(client)
 {
 }
Esempio n. 17
0
 public DataBuffer(ClientWrapper client)
 {
     _client = client;
 }
Esempio n. 18
0
 internal void IntroduceNewPlayer(ClientWrapper caller)
 {
     foreach (var i in OnlinePlayers.Values)
     {
         if (i.Wrapper != caller)
         {
             new PlayerListItem(caller)
             {
                 Action = 0,
                 Gamemode = i.Gamemode,
                 Username = i.Username,
                 Uuid = i.Uuid
             }.Write(); //Send TAB Item
             new SpawnPlayer(caller) {Player = i}.Write(); //Spawn the old player to new player
             new SpawnPlayer(i.Wrapper) {Player = caller.Player}.Write(); //Spawn the new player to old player
             i.BroadcastEquipment();
         }
     }
 }
Esempio n. 19
0
 public LocalDataBuffer(ClientWrapper client)
     : base(client)
 {
 }