Пример #1
0
        private async Task WriteUnifiedFrameContent(
            AsyncBinaryWriter writer,
            int streamId,
            UnifiedFrameContent unifiedFrameContent)
        {
            if (this.logger.IsEnabled(LogLevel.Debug))
            {
                using var memoryStream = new MemoryStream();
                using var memoryWriter = new AsyncBinaryWriter(memoryStream, Encoding.ASCII, false);

                await WriteFrame(memoryWriter, 2, 0, streamId, unifiedFrameContent.ToByteArray())
                .ConfigureAwait(false);

                byte[] sendingBytes = memoryStream.ToArray();

                this.logger.LogDebug(
                    "Sending to {0} with streamId={1}:" + Environment.NewLine + "{2}",
                    unifiedFrameContent.Header.Path,
                    streamId,
                    HexUtils.Dump(new ReadOnlyMemory <byte>(sendingBytes)));
                await writer.WriteAsync(sendingBytes).ConfigureAwait(false);
            }
            else
            {
                await WriteFrame(writer, 2, 0, streamId, unifiedFrameContent.ToByteArray())
                .ConfigureAwait(false);
            }
        }
Пример #2
0
        private async Task DrainReceivingContexts(AsyncBinaryReader binaryReader)
        {
            while (!(this.receivingContexts.IsEmpty && this.requestQueue.Count == 0))
            {
                byte[] frameHeaderBytes = await binaryReader.ReadBytesAsync(20).ConfigureAwait(false);

                if (frameHeaderBytes.Length < 20)
                {
                    throw new IOException("Socket closed during reading.");
                }

                var frameHeader = FrameHeader.Parse(frameHeaderBytes);
                if (frameHeader.ContentOffset != 20)
                {
                    this.logger.LogCritical(
                        "Invalid frame header with unexpected content offset, expected=20 actual={0}",
                        frameHeader.ContentOffset);
                    throw new InvalidDataException("Invalid frame header");
                }

                if (frameHeader.Version != 1)
                {
                    this.logger.LogWarning(
                        "Invalid frame header with unexpected version {0}",
                        frameHeader.Version);
                    throw new InvalidDataException("Invalid frame header");
                }

                byte[] framePayloadBytes = await binaryReader
                                           .ReadBytesAsync(frameHeader.ContentLength)
                                           .ConfigureAwait(false);

                if (framePayloadBytes.Length < frameHeader.ContentLength)
                {
                    throw new IOException("Socket closed during reading.");
                }

                if (this.logger.IsEnabled(LogLevel.Debug))
                {
                    var buffer = new byte[frameHeaderBytes.Length + framePayloadBytes.Length];
                    Buffer.BlockCopy(frameHeaderBytes, 0, buffer, 0, frameHeaderBytes.Length);
                    Buffer.BlockCopy(framePayloadBytes, 0, buffer, frameHeaderBytes.Length, framePayloadBytes.Length);

                    this.logger.LogDebug(
                        "Received streamId={0}:" + Environment.NewLine + "{1}",
                        frameHeader.StreamId,
                        HexUtils.Dump(buffer.AsMemory()));
                }

                if (this.receivingContexts.TryRemove((int)frameHeader.StreamId, out RequestContext requestContext))
                {
                    this.Process(requestContext, frameHeader, framePayloadBytes);
                }
                else
                {
                    this.logger.LogWarning("Dropped a frame with unexpected streamId {0}", frameHeader.StreamId);
                }
            }
        }