Example #1
0
        private void HandleClientCommNew(TcpClient tcpClient, NetworkStream clientStream)
        {
            ClientWrapper Client = new ClientWrapper(tcpClient);

            while (tcpClient.Connected)
            {
                try
                {
                    MSGBuffer Buf          = new MSGBuffer(Client, clientStream);
                    int       ReceivedData = clientStream.Read(Buf.BufferedData, 0, Buf.BufferedData.Length);
                    if (ReceivedData > 0)
                    {
                        int length = Buf.ReadVarInt();
                        Buf.Size = length;
                        int  packid = Buf.ReadVarInt();
                        bool found  = false;
                        foreach (IPacket i in Globals.Packets)
                        {
                            if (i.PacketID == packid && i.IsPlayePacket == Client.PlayMode)
                            {
                                i.Read(Client, Buf, new object[0]);
                                found = true;
                                break;
                            }
                        }
                        if (!found)
                        {
                            Globals.Logger.Log(LogType.Error, "Unknown packet received! \"0x" + packid.ToString("X2") + "\"");
                        }
                    }
                    else
                    {
                        break;
                    }
                } catch (Exception e)
                {
                    Globals.Logger.Log(LogType.Error, e.Message);
                    new Disconnect().Write(Client, new MSGBuffer(Client), new object[] { "Server threw an exception!" });
                    break;
                }
            }
            //Close the connection with the client. :)
            Client.StopKeepAliveTimer();
            if (Client.Player != null)
            {
                Globals.Players.Remove(Client.Player);
            }
            Client.TCPClient.Close();
        }
        public void Read(ClientWrapper state, MSGBuffer buffer, object[] Arguments)
        {
            int    Protocol = buffer.ReadVarInt();
            string Host     = buffer.ReadString();
            short  Port     = buffer.ReadShort();
            int    State    = buffer.ReadVarInt();

            switch (State)
            {
            case 1:
                HandleStatusRequest(state, buffer);
                break;

            case 2:
                HandleLoginRequest(state, buffer);
                break;
            }
        }
Example #3
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();
        }