using System;
using System.IO;

namespace XP.Hardware.RaySource.Comet.Host.Pipe
{
    /// <summary>
    /// 管道写入流包装器
    /// 重写 Flush() 为空操作，避免 StreamWriter.AutoFlush 触发
    /// NamedPipe 的 FlushFileBuffers（该 API 会阻塞直到对端读取数据）
    /// Write 操作直接写入底层管道的内核缓冲区，对端可立即读取
    /// </summary>
    internal class WriteOnlyPipeStream : Stream
    {
        private readonly Stream _inner;

        public WriteOnlyPipeStream(Stream inner)
        {
            _inner = inner;
        }

        public override bool CanRead => false;
        public override bool CanSeek => false;
        public override bool CanWrite => _inner.CanWrite;
        public override long Length => _inner.Length;
        public override long Position
        {
            get => _inner.Position;
            set => _inner.Position = value;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            _inner.Write(buffer, offset, count);
        }

        /// <summary>
        /// 空操作，不调用底层管道的 Flush（FlushFileBuffers）
        /// </summary>
        public override void Flush()
        {
            // 故意不调用 _inner.Flush()
        }

        public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
        public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
        public override void SetLength(long value) => throw new NotSupportedException();

        protected override void Dispose(bool disposing)
        {
            // 不释放底层流，由调用方统一管理
            base.Dispose(disposing);
        }
    }
}
