This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Native Event Tracing IO Wrappers (#1444)
* Add IOStream native wrapper * Add IOStorage native wrapper * Add changelog entry * Add sealed to IOStorage class * Remove GC SuppressFinalize call
- Loading branch information
Sean Parker
authored
Aug 6, 2020
1 parent
31251a5
commit ad77599
Showing
7 changed files
with
207 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
workers/unity/Packages/io.improbable.worker.sdk/IOStorage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using Improbable.Worker.CInterop.Internal; | ||
|
||
namespace Improbable.Worker.CInterop | ||
{ | ||
public sealed class IOStorage : IDisposable | ||
{ | ||
private readonly CIO.StorageHandle storage; | ||
|
||
public IOStorage() | ||
{ | ||
storage = CIO.StorageCreate(); | ||
} | ||
|
||
/// <inheritdoc cref="IDisposable"/> | ||
public void Dispose() | ||
{ | ||
storage.Dispose(); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
CIO.StorageClear(storage); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
workers/unity/Packages/io.improbable.worker.sdk/IOStorage.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
156 changes: 156 additions & 0 deletions
156
workers/unity/Packages/io.improbable.worker.sdk/IOStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
using System; | ||
using System.IO; | ||
using Improbable.Worker.CInterop.Internal; | ||
|
||
namespace Improbable.Worker.CInterop | ||
{ | ||
public enum OpenMode | ||
{ | ||
/* Opens the stream in the default mode. */ | ||
OpenModeDefault = 0x00, | ||
} | ||
|
||
public sealed unsafe class IOStream : IDisposable | ||
{ | ||
private readonly CIO.StreamHandle stream; | ||
|
||
private IOStream(CIO.StreamHandle stream) | ||
{ | ||
this.stream = stream; | ||
} | ||
|
||
/// <inheritdoc cref="IDisposable"/> | ||
public void Dispose() | ||
{ | ||
stream.Dispose(); | ||
} | ||
|
||
public static IOStream CreateRingBufferStream(uint capacity) | ||
{ | ||
return new IOStream(CIO.CreateRingBufferStream(capacity)); | ||
} | ||
|
||
public static IOStream CreateFileStream(string fileName, OpenMode openMode) | ||
{ | ||
fixed (byte* fileNameBytes = ApiInterop.ToUtf8Cstr(fileName)) | ||
{ | ||
return new IOStream(CIO.CreateFileStream(fileNameBytes, (CIO.OpenMode) openMode)); | ||
} | ||
} | ||
|
||
public long Write(byte[] data) | ||
{ | ||
ThrowIfStreamClosed(); | ||
|
||
var remainingCapacity = CIO.StreamGetRemainingWriteCapacityBytes(stream); | ||
if (remainingCapacity < data.Length) | ||
{ | ||
throw new NotSupportedException("Not enough stream capacity to write data."); | ||
} | ||
|
||
var bytesWritten = 0L; | ||
fixed (byte* dataToWrite = data) | ||
{ | ||
bytesWritten = CIO.StreamWrite(stream, dataToWrite, 1); | ||
} | ||
|
||
if (bytesWritten != -1) | ||
{ | ||
return bytesWritten; | ||
} | ||
|
||
var rawError = CIO.StreamGetLastError(stream); | ||
throw new IOException(ApiInterop.FromUtf8Cstr(rawError)); | ||
} | ||
|
||
public long Read(uint bytesToRead, out byte[] streamData) | ||
{ | ||
ThrowIfStreamClosed(); | ||
|
||
streamData = new byte[bytesToRead]; | ||
|
||
var bytesRead = 0L; | ||
fixed (byte* streamDataPointer = streamData) | ||
{ | ||
bytesRead = CIO.StreamRead(stream, streamDataPointer, bytesToRead); | ||
} | ||
|
||
if (bytesRead != -1) | ||
{ | ||
return bytesRead; | ||
} | ||
|
||
var rawError = CIO.StreamGetLastError(stream); | ||
throw new IOException(ApiInterop.FromUtf8Cstr(rawError)); | ||
} | ||
|
||
public long Read(byte[] streamData) | ||
{ | ||
ThrowIfStreamClosed(); | ||
|
||
var bytesToRead = (uint) streamData.Length; | ||
var bytesRead = 0L; | ||
fixed (byte* streamDataPointer = streamData) | ||
{ | ||
bytesRead = CIO.StreamRead(stream, streamDataPointer, bytesToRead); | ||
} | ||
|
||
if (bytesRead != -1) | ||
{ | ||
return bytesRead; | ||
} | ||
|
||
var rawError = CIO.StreamGetLastError(stream); | ||
throw new IOException(ApiInterop.FromUtf8Cstr(rawError)); | ||
} | ||
|
||
public long Peek(uint bytesToPeek, out byte[] streamData) | ||
{ | ||
ThrowIfStreamClosed(); | ||
|
||
streamData = new byte[bytesToPeek]; | ||
|
||
var bytesPeeked = 0L; | ||
fixed (byte* streamDataPointer = streamData) | ||
{ | ||
bytesPeeked = CIO.StreamPeek(stream, streamDataPointer, bytesToPeek); | ||
} | ||
|
||
if (bytesPeeked != -1) | ||
{ | ||
return bytesPeeked; | ||
} | ||
|
||
var rawError = CIO.StreamGetLastError(stream); | ||
throw new IOException(ApiInterop.FromUtf8Cstr(rawError)); | ||
} | ||
|
||
public long Ignore(uint bytesToIgnore) | ||
{ | ||
ThrowIfStreamClosed(); | ||
|
||
var bytesIgnored = CIO.StreamIgnore(stream, bytesToIgnore); | ||
|
||
if (bytesIgnored != -1) | ||
{ | ||
return bytesIgnored; | ||
} | ||
|
||
var rawError = CIO.StreamGetLastError(stream); | ||
throw new IOException(ApiInterop.FromUtf8Cstr(rawError)); | ||
} | ||
|
||
public uint GetRemainingCapacity() | ||
{ | ||
return CIO.StreamGetRemainingWriteCapacityBytes(stream); | ||
} | ||
|
||
private void ThrowIfStreamClosed() | ||
{ | ||
if (stream.IsClosed) | ||
{ | ||
throw new ObjectDisposedException("Cannot access a disposed object."); | ||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
workers/unity/Packages/io.improbable.worker.sdk/IOStream.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.