示例#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
            }
        }
示例#2
0
文件: Package.cs 项目: LiveMC/SharpMC
 protected Package(ClientWrapper client, DataBuffer buffer)
 {
     if (client == null) return;
     if (!client.TcpClient.Connected) return;
     Client = client;
     Stream = client.TcpClient.GetStream();
     Buffer = buffer;
 }
示例#3
0
 public void CleanErrors(ClientWrapper client)
 {
     if (PacketErrors.ContainsKey(client.ClientIdentifier))
     {
         PacketErrors[client.ClientIdentifier] = 0;
     }
     else
     {
         //Something really wrong
     }
 }
示例#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;
        }
示例#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;
        }
示例#6
0
 internal void RemoveClient(ClientWrapper client)
 {
     if (Clients.ContainsKey(client.ClientIdentifier))
     {
         Clients.Remove(client.ClientIdentifier);
         PacketErrors.Remove(client.ClientIdentifier);
         GC.Collect();
     }
 }
示例#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());
     }
 }
示例#8
0
 public void ReportPing(ClientWrapper client)
 {
     if (ClientPing.ContainsKey(client.ClientIdentifier))
     {
         ClientPing[client.ClientIdentifier] = UnixTimeNow();
     }
 }
示例#9
0
 public DataBuffer(ClientWrapper client)
 {
     _client = client;
 }
示例#10
0
文件: Level.cs 项目: LiveMC/SharpMC
 internal void BroadcastPlayerRemoval(ClientWrapper caller)
 {
     new PlayerListItem(caller)
     {
         Action = 4,
         Uuid = caller.Player.Uuid
     }.Broadcast(this);
 }
示例#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();
        }
示例#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;
            }
        }
示例#13
0
 public virtual void HandleData(ClientWrapper client, DataBuffer buffer)
 {
 }
示例#14
0
文件: Package.cs 项目: LiveMC/SharpMC
 internal void SetTarget(ClientWrapper client)
 {
     Client = client;
     Stream = client.TcpClient.GetStream();
     Buffer = new DataBuffer(client);
 }
示例#15
0
 public override void HandleData(ClientWrapper client, DataBuffer buffer)
 {
     string c = buffer.ReadString();
     ConsoleFunctions.WriteInfoLine(client.Player.Username + "'s client: " + c);
 }
示例#16
0
 public LocalDataBuffer(ClientWrapper client) : base(client)
 {
 }
示例#17
0
 public DataBuffer(ClientWrapper client)
 {
     _client = client;
 }
示例#18
0
文件: Level.cs 项目: LiveMC/SharpMC
 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();
         }
     }
 }
示例#19
0
 public LocalDataBuffer(ClientWrapper client)
     : base(client)
 {
 }