| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using KernelCommunication; |
| | 4 | |
|
| | 5 | | namespace RPC.Services |
| | 6 | | { |
| | 7 | | public class CRDTStream : Stream, IBinaryReader |
| | 8 | | { |
| | 9 | | private byte[] streamBuffer; |
| | 10 | | private int offset; |
| | 11 | |
|
| 2 | 12 | | public override bool CanRead => Position < streamBuffer.Length; |
| | 13 | |
|
| 0 | 14 | | public override bool CanSeek => false; |
| | 15 | |
|
| 0 | 16 | | public override bool CanWrite => true; |
| | 17 | |
|
| 0 | 18 | | public override long Length => streamBuffer.Length; |
| | 19 | |
|
| 2 | 20 | | public override long Position { set => offset = (int)value; get => offset; } |
| | 21 | |
|
| 0 | 22 | | public override void Flush() { } |
| | 23 | |
|
| | 24 | | public override int Read(byte[] buffer, int offset, int count) |
| | 25 | | { |
| 0 | 26 | | return 0; |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public override long Seek(long offset, SeekOrigin origin) |
| | 30 | | { |
| 0 | 31 | | return 0; |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | public override void SetLength(long value) { } |
| | 35 | |
|
| | 36 | | public override void Write(byte[] buffer, int offset, int count) |
| | 37 | | { |
| 1 | 38 | | streamBuffer = buffer; |
| 1 | 39 | | this.offset = offset; |
| 1 | 40 | | } |
| | 41 | |
|
| 0 | 42 | | int IBinaryReader.offset => offset; |
| | 43 | |
|
| 2 | 44 | | bool IBinaryReader.CanRead() => CanRead; |
| | 45 | |
|
| | 46 | | unsafe int IBinaryReader.ReadInt32() |
| | 47 | | { |
| 5 | 48 | | int ofs = offset; |
| 5 | 49 | | offset += 4; |
| | 50 | |
|
| 5 | 51 | | if (streamBuffer == null) |
| 0 | 52 | | throw new NullReferenceException("bytes array == null"); |
| 5 | 53 | | if ((long)(uint)ofs >= (long)streamBuffer.Length) |
| 0 | 54 | | throw new IndexOutOfRangeException("offset is bigger than bytes array lenght"); |
| 5 | 55 | | if (ofs > streamBuffer.Length - 4) |
| 0 | 56 | | throw new IndexOutOfRangeException("bytes.Length is not large enough to contain a valid Int32"); |
| | 57 | |
|
| 5 | 58 | | fixed (byte* numPtr = &streamBuffer[ofs]) |
| | 59 | | { |
| 5 | 60 | | return ByteUtils.PointerToInt32(numPtr); |
| | 61 | | } |
| | 62 | | } |
| | 63 | |
|
| | 64 | | unsafe long IBinaryReader.ReadInt64() |
| | 65 | | { |
| 1 | 66 | | int ofs = offset; |
| 1 | 67 | | offset += 8; |
| | 68 | |
|
| 1 | 69 | | if (streamBuffer == null) |
| 0 | 70 | | throw new NullReferenceException("bytes array == null"); |
| 1 | 71 | | if ((long)(uint)ofs >= (long)streamBuffer.Length) |
| 0 | 72 | | throw new IndexOutOfRangeException("offset is bigger than bytes array lenght"); |
| 1 | 73 | | if (ofs > streamBuffer.Length - 8) |
| 0 | 74 | | throw new IndexOutOfRangeException("bytes.Length is not large enough to contain a valid Int64"); |
| | 75 | |
|
| 1 | 76 | | fixed (byte* numPtr = &streamBuffer[ofs]) |
| | 77 | | { |
| 1 | 78 | | return ByteUtils.PointerToInt64(numPtr); |
| | 79 | | } |
| | 80 | | } |
| | 81 | |
|
| | 82 | | byte[] IBinaryReader.ReadBytes(int length) |
| | 83 | | { |
| 1 | 84 | | int ofs = offset; |
| 1 | 85 | | offset += length; |
| 1 | 86 | | byte[] data = new byte[length]; |
| 1 | 87 | | Buffer.BlockCopy(streamBuffer, ofs, data, 0, length); |
| 1 | 88 | | return data; |
| | 89 | | } |
| | 90 | |
|
| | 91 | | void IBinaryReader.Skip(int bytesCount) |
| | 92 | | { |
| 0 | 93 | | offset += bytesCount; |
| 0 | 94 | | } |
| | 95 | | } |
| | 96 | | } |