public static async Task ReadFullImpl(IMyStream myStream, BytesSegment bs) { int pos = 0; if (myStream is IMyStreamReadR r) { while (pos < bs.Len) { var read = await r.ReadAsyncR(bs.Sub(pos)); if (read == 0) { throw new DisconnectedException($"unexpected EOF while ReadFull() (count={bs.Len}, pos={pos})"); } pos += read; } return; } while (pos < bs.Len) { var read = await myStream.ReadAsync(bs.Sub(pos)).CAF(); if (read == 0) { throw new DisconnectedException($"unexpected EOF while ReadFull() (count={bs.Len}, pos={pos})"); } pos += read; } }
public static AwaitableWrapper <int> ReadAsyncR(this IMyStream myStream, BytesSegment bs) { if (myStream is IMyStreamReadR myStreamReuse) { return(myStreamReuse.ReadAsyncR(bs)); } return(new AwaitableWrapper <int>(myStream.ReadAsync(bs))); }
public static int Read(this IMyStream myStream, byte[] buf, int offset, int count) { if (myStream is IMyStreamSync sync) { return(sync.Read(new BytesSegment(buf, offset, count))); } else { return(myStream.ReadAsync(buf, offset, count).RunSync()); } }
public static Task <int> ReadAsync(this IMyStream myStream, byte[] buf, int offset, int count) { return(myStream.ReadAsync(new BytesSegment(buf, offset, count))); }