// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.IO; using System.Threading; using System.Threading.Tasks; namespace Progmasoft.Tazel.Guest; /// A connected vsock socket as a stream, over read and write; .NET has no vsock address family. [System.Runtime.Versioning.SupportedOSPlatform("linux")] internal sealed class VsockStream(int socket) : Stream { private readonly Lock _writing = new(); private int _fd = socket; public override bool CanRead => true; public override bool CanSeek => false; public override bool CanWrite => true; public override long Length => throw new NotSupportedException(); public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); } public override unsafe int Read(byte[] buffer, int offset, int count) { ArgumentNullException.ThrowIfNull(buffer); fixed (byte* start = &buffer[offset]) { while (true) { var read = Libc.Read(_fd, start, (nuint)count); if (read >= 0) { return (int)read; } if (System.Runtime.InteropServices.Marshal.GetLastPInvokeError() != 4) { throw new IOException($"reading the connection failed: {Libc.LastError()}"); } } } } // A blocking read on a thread of its own, so the agent serves other connections meanwhile. public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => Task.Factory.StartNew(() => Read(buffer, offset, count), cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) { var array = new byte[buffer.Length]; return new ValueTask(ReadAsync(array, 0, array.Length, cancellationToken).ContinueWith( task => { array.AsSpan(0, task.Result).CopyTo(buffer.Span); return task.Result; }, cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default)); } public override unsafe void Write(byte[] buffer, int offset, int count) { ArgumentNullException.ThrowIfNull(buffer); lock (_writing) { fixed (byte* start = buffer) { var done = 0; while (done < count) { var written = Libc.Write(_fd, start + offset + done, (nuint)(count - done)); if (written < 0) { if (System.Runtime.InteropServices.Marshal.GetLastPInvokeError() == 4) { continue; } throw new IOException($"writing the connection failed: {Libc.LastError()}"); } done += (int)written; } } } } public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { Write(buffer, offset, count); return Task.CompletedTask; } public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) { var array = buffer.ToArray(); Write(array, 0, array.Length); return ValueTask.CompletedTask; } public override void Flush() { } 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) { var closing = Interlocked.Exchange(ref _fd, -1); if (closing >= 0) { Libc.Close(closing); } base.Dispose(disposing); } }