Ejemplo n.º 1
0
        public async Task WritePacketAsync(OutPacket packet)
        {
            var buf    = new MemoryStream();
            var writer = new PrimitiveWriter(buf);

            writer.WriteVarint(OutPacket.GetId(State, packet));
            packet.Write(writer);

            var uncompressedLen = buf.Length;

            if (Threshold != null)
            {
                var compressedBuf = new MemoryStream();
                if (uncompressedLen > Threshold)
                {
                    ZlibCompress(compressedBuf, buf);

                    buf.SetLength(0);
                    var n = Varint.PutVarint(_varintBuf, (int)uncompressedLen);
                    buf.Write(_varintBuf, 0, n);
                    compressedBuf.CopyTo(buf);
                }
                else
                {
                    buf.CopyTo(compressedBuf);
                    buf.SetLength(0);
                    buf.WriteByte(0);
                    compressedBuf.CopyTo(buf);
                }
            }
            buf.Seek(0, SeekOrigin.Begin);

            var n2 = Varint.PutVarint(_varintBuf, (int)buf.Length);
            await BaseStream.WriteAsync(_varintBuf, 0, n2);

            await buf.CopyToAsync(BaseStream);
        }
Ejemplo n.º 2
0
 public static int GetId(State state, OutPacket packet)
 {
     return(Mappings[state][packet.GetType()]);
 }