aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Core/Common/Utils.StreamMonitor.cs
blob: 38c52745f468abca82e9861fbb57013b6ad85962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

using System;
using System.IO;

using Task = System.Threading.Tasks.Task;

namespace QtVsTools.Core.Common
{
    public static partial class Utils
    {
        public enum StreamAction
        {
            KeepStreaming,
            StopStreaming
        }

        public class StreamDataEventArgs : EventArgs
        {
            public byte[] Data { get; }

            public StreamDataEventArgs(byte[] data, int count)
            {
                Data = new byte[count];
                Buffer.BlockCopy(data, 0, Data, 0, count);
            }
        }

        public class StreamMonitor : Stream
        {
            private readonly Stream monitoredStream;

            public bool LoggingEnabled { get; set; }

            public delegate void DataReceivedDelegate(StreamDataEventArgs args);
            public event DataReceivedDelegate DataReceived;

            public delegate (StreamData Data, StreamAction action)
                StreamModifierDelegate(StreamData data);
            public StreamModifierDelegate StreamModifier { get; set; }

            public bool IsConnected => monitoredStream is { CanRead: true } or { CanWrite: true };

            public StreamMonitor(Stream stream)
            {
                monitoredStream = stream ?? throw new ArgumentNullException(nameof(stream));
            }

            public override int Read(byte[] buffer, int offset, int count)
            {
                var bytesRead = monitoredStream.Read(buffer, offset, count);
                if (LoggingEnabled && bytesRead > 0)
                    _ = Task.Run(() => DataReceived?.Invoke(new StreamDataEventArgs(buffer, bytesRead)));
                return bytesRead;
            }

            public override void Write(byte[] buffer, int offset, int count)
            {
                if (StreamModifier != null) {
                    var streamData = new StreamData
                    {
                        Bytes = buffer,
                        Offset = offset,
                        Count = count
                    };

                    var (modifiedData, action) = StreamModifier(streamData);
                    buffer = modifiedData.Bytes;
                    count = modifiedData.Count;

                    if (action == StreamAction.StopStreaming)
                        StreamModifier = null;
                }

                monitoredStream.Write(buffer, offset, count);

                if (LoggingEnabled && count > 0)
                    _ = Task.Run(() => DataReceived?.Invoke(new StreamDataEventArgs(buffer, count)));
            }

            public override bool CanRead => monitoredStream.CanRead;

            public override bool CanSeek => monitoredStream.CanSeek;

            public override bool CanWrite => monitoredStream.CanWrite;

            public override long Length => monitoredStream.Length;

            public override long Position
            {
                get => monitoredStream.Position;
                set => monitoredStream.Position = value;
            }

            public override void Flush() => monitoredStream.Flush();

            public override long Seek(long offset, SeekOrigin origin)
                => monitoredStream.Seek(offset, origin);

            public override void SetLength(long value) => monitoredStream.SetLength(value);

            #region IDisposable

            private bool disposed;

            protected override void Dispose(bool disposing)
            {
                if (!disposed) {
                    if (disposing)
                        monitoredStream?.Dispose();

                    disposed = true;
                    GC.SuppressFinalize(this);
                }

                base.Dispose(disposing);
            }

            ~StreamMonitor()
            {
                Dispose(false);
            }

            #endregion IDisposable
        }
    }
}