Exemple #1
0
        private void onPacketReceive(IAsyncResult result)
        {
            if (CurrentState == ConnectionState.Disconnect) // mmm sexy race condition
            {
                return;
            }
            int bytesRead;

            try
            {
                bytesRead = tcpStream.EndRead(result);
            } catch (ObjectDisposedException) // mmm sexy race condition pt. 2
            {
                return;
            }
            if (bytesRead == 0)
            {
                Server.ServerThread.Queue(completeDisconnect, DisconnectReason.ClientClosedConnection);
                Console.WriteLine("blah");
            }
            else
            {
                try
                {
                    int len = tcpStream.ReadVarInt(firstByte[0]);
                    ServerBoundPacket packet;
                    if (Server.CompressionEnabled)
                    {
                        byte[] wholePacket = new byte[len];
                        tcpStream.Read(wholePacket, 0, len);
                        using (MemoryStream ms = new MemoryStream(wholePacket))
                        {
                            int dataLength = ms.ReadVarInt();
                            if (dataLength == 0) // uncompressed
                            {
                                int read;
                                int packetID = ms.ReadVarInt(out read);
                                packet = Server.PacketParser.ParsePacket(tcpStream, len - read - DataUtils.MeasureVarInt(0), packetID, Version, CurrentState);
                            }
                            else
                            {
                                using (ZlibStream zlibStream = new ZlibStream(ms, CompressionMode.Decompress))
                                {
                                    int read;
                                    int packetID = zlibStream.ReadVarInt(out read);
                                    packet = Server.PacketParser.ParsePacket(zlibStream, dataLength - read, packetID, Version, CurrentState);
                                }
                            }
                        }
                    }
                    else
                    {
                        int read;
                        int packetID = tcpStream.ReadVarInt(out read);
                        packet = Server.PacketParser.ParsePacket(tcpStream, len - read, packetID, Version, CurrentState);
                    }
                    Server.ServerThread.Queue(completePacketReceive, packet);
                } catch
                {
                    Server.ServerThread.Queue(completeDisconnect, DisconnectReason.UnexpectedDataFromClient);
                }
            }
        }