Example #1
0
        public void ReadFromStream(IPacketCodec rawCodec, PacketBoundTo boundTo, Func <int> compressThresholdCallback, Func <ProtocolState> stateCallback)
        {
            BoundTo = boundTo;
            State   = stateCallback();
            var length = rawCodec.ReadVarInt(); // Length of Packet ID + Data

            PacketLength = length;
            var threshold = compressThresholdCallback();
            int dataLength;

            using var recvStream = new MemoryStream(); //no extra bytes read
            rawCodec.CopyTo(recvStream, length);
            var recvCodec = rawCodec.Clone(recvStream);

            recvStream.Position = 0;
            if (threshold == 0 || (dataLength = recvCodec.ReadVarInt()) < threshold)
            {
                DataLength = length;
                recvCodec.CopyTo(BaseStream, length);
                BaseStream.Position = 0;
                PacketId            = Content.ReadVarInt();
                return;
            }
            DataLength = dataLength;
            using var compressedStream = new InflaterInputStream(recvStream)
                  {
                      IsStreamOwner = false
                  };
            rawCodec.Clone(compressedStream).CopyTo(BaseStream, dataLength);
            BaseStream.Position = 0;
            PacketId            = Content.ReadVarInt();
        }
Example #2
0
 public bool TryCreatePacket(int packetId, PacketBoundTo boundTo, ProtocolState state, out IPacket packet)
 {
     packet = _packetInfo.FirstOrDefault(info =>
                                         info.PacketId == packetId &&
                                         info.BoundTo == boundTo &&
                                         (info.State == state || info.State == ProtocolState.Any)
                                         )?.Constructor();
     return(packet != null);
 }
Example #3
0
 public ProtocolAdapterBase(Stream baseStream, PacketBoundTo boundTo)
 {
     BaseStream          = baseStream;
     BufferedReadStream  = new BufferedStream(baseStream);
     BufferedWriteStream = new BufferedStream(baseStream);
     _rawReadCodec       = DefaultCodec.Clone(BufferedReadStream);
     _rawWriteCodec      = DefaultCodec.Clone(BufferedWriteStream);
     BoundTo             = boundTo;
     RemoteBoundTo       = boundTo switch
     {
         PacketBoundTo.Client => PacketBoundTo.Server,
         PacketBoundTo.Server => PacketBoundTo.Client,
         _ => default
     };
 }
Example #4
0
 public MCVer756ProtocolAdapter(Stream baseStream, PacketBoundTo boundTo) : base(baseStream, boundTo)
 {
 }