Exemplo n.º 1
0
        private void HandleClientCommNew(object client)
        {
            var tcpClient    = (TcpClient)client;
            var clientStream = tcpClient.GetStream();
            var Client       = new ClientWrapper(tcpClient);

            while (true)
            {
                try
                {
                    var buffie = new byte[4096];
                    int receivedData;
                    receivedData = clientStream.Read(buffie, 0, buffie.Length);

                    if (receivedData > 0)
                    {
                        var buf = new MSGBuffer(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;

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

                        if (!new PackageFactory(Client, buf).Handle(packid))
                        {
                            ConsoleFunctions.WriteWarningLine("Unknown packet received! \"0x" + packid.ToString("X2") + "\"");
                        }
                        buf.Dispose();
                    }
                    else
                    {
                        //Stop the while loop. Client disconnected!
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Client.ThreadPool.KillAllThreads();
                    //Exception, disconnect!
                    ConsoleFunctions.WriteDebugLine("Error: \n" + ex);
                    new Disconnect(Client)
                    {
                        Reason = "§4SharpMC\n§fServer threw an exception!\n\nFor the nerdy people: \n" + ex.Message
                    }.Write();
                    break;
                }
            }
            //Close the connection with the client. :)
            Client.ThreadPool.KillAllThreads();
            Client.StopKeepAliveTimer();

            if (Client.Player != null)
            {
                Client.Player.Level.RemovePlayer(Client.Player);
                Client.Player.Level.BroadcastPlayerRemoval(Client);
            }
            Client.TcpClient.Close();
            Thread.CurrentThread.Abort();
        }