| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.IO.Pipelines; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | using CoreWCF.Runtime; |
| | | 12 | | using Microsoft.Extensions.Logging; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.Channels.Framing |
| | | 15 | | { |
| | | 16 | | internal class LoggingDuplexPipe : DuplexPipeStreamAdapter<LoggingStream> |
| | | 17 | | { |
| | | 18 | | public LoggingDuplexPipe(IDuplexPipe transport, ILogger logger) : |
| | 0 | 19 | | base(transport, stream => new LoggingStream(stream, logger)) |
| | | 20 | | { |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public bool LoggingEnabled |
| | | 24 | | { |
| | 0 | 25 | | get => Stream.LoggingEnabled; |
| | 0 | 26 | | internal set => Stream.LoggingEnabled = value; |
| | | 27 | | } |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | internal class DuplexPipeStreamAdapter<TStream> : DuplexPipeStream, IDuplexPipe where TStream : Stream |
| | | 31 | | { |
| | | 32 | | public DuplexPipeStreamAdapter(IDuplexPipe duplexPipe, Func<Stream, TStream> createStream) : |
| | | 33 | | this(duplexPipe, new StreamPipeReaderOptions(leaveOpen: false), new StreamPipeWriterOptions(leaveOpen: false |
| | | 34 | | { |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public DuplexPipeStreamAdapter(IDuplexPipe duplexPipe, StreamPipeReaderOptions readerOptions, StreamPipeWriterOp |
| | | 38 | | { |
| | | 39 | | Stream = createStream(this); |
| | | 40 | | Input = PipeReader.Create(Stream, readerOptions); |
| | | 41 | | Output = PipeWriter.Create(Stream, writerOptions); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public TStream Stream { get; } |
| | | 45 | | |
| | | 46 | | public PipeReader Input { get; } |
| | | 47 | | |
| | | 48 | | public PipeWriter Output { get; } |
| | | 49 | | |
| | | 50 | | protected override void Dispose(bool disposing) |
| | | 51 | | { |
| | | 52 | | Input.Complete(); |
| | | 53 | | Output.Complete(); |
| | | 54 | | base.Dispose(disposing); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | //public override ValueTask DisposeAsync() |
| | | 58 | | //{ |
| | | 59 | | // Input.Complete(); |
| | | 60 | | // Output.Complete(); |
| | | 61 | | // return base.DisposeAsync(); |
| | | 62 | | //} |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | internal sealed class LoggingStream : Stream |
| | | 66 | | { |
| | | 67 | | private readonly Stream _inner; |
| | | 68 | | private readonly ILogger _logger; |
| | | 69 | | |
| | | 70 | | public LoggingStream(Stream inner, ILogger logger) |
| | | 71 | | { |
| | | 72 | | _inner = inner; |
| | | 73 | | _logger = logger; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public override bool CanRead => _inner.CanRead; |
| | | 77 | | |
| | | 78 | | public override bool CanSeek => _inner.CanSeek; |
| | | 79 | | |
| | | 80 | | public override bool CanWrite => _inner.CanWrite; |
| | | 81 | | |
| | | 82 | | public override long Length => _inner.Length; |
| | | 83 | | |
| | | 84 | | public override long Position |
| | | 85 | | { |
| | | 86 | | get => _inner.Position; |
| | | 87 | | set => _inner.Position = value; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | public bool LoggingEnabled { get; internal set; } |
| | | 91 | | |
| | | 92 | | public override void Flush() => _inner.Flush(); |
| | | 93 | | |
| | | 94 | | public override Task FlushAsync(CancellationToken cancellationToken) => _inner.FlushAsync(cancellationToken); |
| | | 95 | | |
| | | 96 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 97 | | { |
| | | 98 | | int read = _inner.Read(buffer, offset, count); |
| | | 99 | | Log("Read", new ReadOnlySpan<byte>(buffer, offset, read)); |
| | | 100 | | return read; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | // TODO: Enable code when moving to .NET 5 |
| | | 104 | | //public override int Read(Span<byte> destination) |
| | | 105 | | //{ |
| | | 106 | | // int read = _inner.Read(destination); |
| | | 107 | | // Log("Read", destination.Slice(0, read)); |
| | | 108 | | // return read; |
| | | 109 | | //} |
| | | 110 | | |
| | | 111 | | public async override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationTo |
| | | 112 | | { |
| | | 113 | | int read = await _inner.ReadAsync(buffer, offset, count, cancellationToken); |
| | | 114 | | Log("ReadAsync", new ReadOnlySpan<byte>(buffer, offset, read)); |
| | | 115 | | return read; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | // TODO: Enable code when moving to .NET 5 |
| | | 119 | | //public override async ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = |
| | | 120 | | //{ |
| | | 121 | | // int read = await _inner.ReadAsync(destination, cancellationToken); |
| | | 122 | | // Log("ReadAsync", destination.Span.Slice(0, read)); |
| | | 123 | | // return read; |
| | | 124 | | //} |
| | | 125 | | |
| | | 126 | | public override long Seek(long offset, SeekOrigin origin) |
| | | 127 | | { |
| | | 128 | | return _inner.Seek(offset, origin); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | public override void SetLength(long value) |
| | | 132 | | { |
| | | 133 | | _inner.SetLength(value); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | public override void Write(byte[] buffer, int offset, int count) |
| | | 137 | | { |
| | | 138 | | Log("Write", new ReadOnlySpan<byte>(buffer, offset, count)); |
| | | 139 | | _inner.Write(buffer, offset, count); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | // TODO: Enable code when moving to .NET 5 |
| | | 143 | | //public override void Write(ReadOnlySpan<byte> source) |
| | | 144 | | //{ |
| | | 145 | | // Log("Write", source); |
| | | 146 | | // _inner.Write(source); |
| | | 147 | | //} |
| | | 148 | | |
| | | 149 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | | 150 | | { |
| | | 151 | | Log("WriteAsync", new ReadOnlySpan<byte>(buffer, offset, count)); |
| | | 152 | | return _inner.WriteAsync(buffer, offset, count, cancellationToken); |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | // TODO: Enable code when moving to .NET 5 |
| | | 156 | | //public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = defaul |
| | | 157 | | //{ |
| | | 158 | | // Log("WriteAsync", source.Span); |
| | | 159 | | // return _inner.WriteAsync(source, cancellationToken); |
| | | 160 | | //} |
| | | 161 | | |
| | | 162 | | private void Log(string method, ReadOnlySpan<byte> buffer) |
| | | 163 | | { |
| | | 164 | | if (!LoggingEnabled || !_logger.IsEnabled(LogLevel.Debug)) |
| | | 165 | | { |
| | | 166 | | return; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | var builder = new StringBuilder(); |
| | | 170 | | //builder.Append(method); |
| | | 171 | | //builder.Append('['); |
| | | 172 | | //builder.Append(buffer.Length); |
| | | 173 | | //builder.Append(']'); |
| | | 174 | | |
| | | 175 | | if (buffer.Length > 0) |
| | | 176 | | { |
| | | 177 | | builder.AppendLine(); |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | var charBuilder = new StringBuilder(); |
| | | 181 | | |
| | | 182 | | // Write the hex |
| | | 183 | | for (int i = 0; i < buffer.Length; i++) |
| | | 184 | | { |
| | | 185 | | builder.Append(buffer[i].ToString("X2", CultureInfo.InvariantCulture)); |
| | | 186 | | builder.Append(' '); |
| | | 187 | | |
| | | 188 | | var bufferChar = (char)buffer[i]; |
| | | 189 | | if (char.IsControl(bufferChar)) |
| | | 190 | | { |
| | | 191 | | charBuilder.Append('.'); |
| | | 192 | | } |
| | | 193 | | else |
| | | 194 | | { |
| | | 195 | | charBuilder.Append(bufferChar); |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | if ((i + 1) % 16 == 0) |
| | | 199 | | { |
| | | 200 | | builder.Append(" "); |
| | | 201 | | builder.Append(charBuilder); |
| | | 202 | | if (i != buffer.Length - 1) |
| | | 203 | | { |
| | | 204 | | builder.AppendLine(); |
| | | 205 | | } |
| | | 206 | | charBuilder.Clear(); |
| | | 207 | | } |
| | | 208 | | else if ((i + 1) % 8 == 0) |
| | | 209 | | { |
| | | 210 | | builder.Append(' '); |
| | | 211 | | charBuilder.Append(' '); |
| | | 212 | | } |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | // Different than charBuffer.Length since charBuffer contains an extra " " after the 8th byte. |
| | | 216 | | var numBytesInLastLine = buffer.Length % 16; |
| | | 217 | | |
| | | 218 | | if (numBytesInLastLine > 0) |
| | | 219 | | { |
| | | 220 | | // 2 (between hex and char blocks) + num bytes left (3 per byte) |
| | | 221 | | var padLength = 2 + (3 * (16 - numBytesInLastLine)); |
| | | 222 | | // extra for space after 8th byte |
| | | 223 | | if (numBytesInLastLine < 8) |
| | | 224 | | { |
| | | 225 | | padLength++; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | builder.Append(new string(' ', padLength)); |
| | | 229 | | builder.Append(charBuilder); |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | _logger.LogBytes(method, buffer.Length, builder.ToString()); |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | // The below APM methods call the underlying Read/WriteAsync methods which will still be logged. |
| | | 236 | | public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object stat |
| | | 237 | | { |
| | | 238 | | return ReadAsync(buffer, offset, count, default(CancellationToken)).ToApm(callback, state); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | public override int EndRead(IAsyncResult asyncResult) |
| | | 242 | | { |
| | | 243 | | return asyncResult.ToApmEnd<int>(); |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object sta |
| | | 247 | | { |
| | | 248 | | return WriteAsync(buffer, offset, count, default(CancellationToken)).ToApm(callback, state); |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | public override void EndWrite(IAsyncResult asyncResult) |
| | | 252 | | { |
| | | 253 | | asyncResult.ToApmEnd(); |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | protected override void Dispose(bool disposing) |
| | | 257 | | { |
| | | 258 | | base.Dispose(disposing); |
| | | 259 | | _inner.Dispose(); |
| | | 260 | | } |
| | | 261 | | } |
| | | 262 | | } |