Exemplo n.º 1
0
        private void HandlePacket(ReadOnlySequence <byte> buffer, MCConnectionContext ctx, IPacketQueue packetQueue)
        {
            var reader = _packetReaderFactory.CreateReader(buffer);
            var length = reader.ReadVarInt();

            if (length > reader.Buffer.Length || length < 1 /* 1 = small ID but no fields*/)
            {
                _logger.LogCritical($"Read Invalid length {length:X}. Aborting");
                ctx.Abort();
                return;
            }

            var lengthLength = buffer.Length - reader.Buffer.Length;

            reader = new MCPacketReader(reader.Buffer.Slice(0, length));
            var id = reader.ReadVarInt();

            using var packetIdScope = _logger.BeginScope($"Packet ID: {id:x2}");

            _packetHandler.HandlePacket(ctx, reader, packetQueue, id);

            // NOT IDEAL, but easiest
            var packetSize = length + lengthLength;

            ctx.Transport.Input.AdvanceTo(buffer.GetPosition(packetSize));
            _metrics.Measure.Histogram.Update(MetricsRegistry.ReadPacketSize, packetSize);
        }
Exemplo n.º 2
0
        public void HandlePacket(MCConnectionContext ctx, IPacketReader reader, IPacketQueue packetQueue, int id)
        {
            var packet = _resolver.GetReadablePacket(id, ctx);

            if (packet == null)
            {
                _logger.LogInformation($"Unknown {ctx.ConnectionStage.AsString()} Packet {id:x2}");
                return;
            }

            packet.Read(reader);
            packet.Process(_logger, ctx, _serviceProvider);
        }
Exemplo n.º 3
0
        private async Task HandleConnection(MCConnectionContext ctx)
        {
            var packetQueue = ctx.PacketQueue;

            while (!ctx.ConnectionClosed.IsCancellationRequested)
            {
                var readResult = await ctx.Transport.Input.ReadAsync(ctx.ConnectionClosed);

                if (readResult.IsCanceled || readResult.IsCompleted)
                {
                    _logger.LogInformation("Connection Closed");
                    return;
                }

                var buffer = readResult.Buffer;
                HandlePacket(buffer, ctx, packetQueue);

                if (packetQueue.NeedsWriting)
                {
                    packetQueue.WriteQueued();
                    await ctx.Transport.Output.FlushAsync();
                }
            }
        }