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
Native Event Tracing IO Wrappers #1444
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5f1da69
Consistant naming in function comments
86b472e
Add IOStream native wrapper
8ede688
Add IOStorage native wrapper
5ab2acf
Fix naming of Storage/Stream handles in params
d24a3e8
Consistant naming for Stream/Storage handles
fbd7af7
Code cleanup
2b1fec9
Fix return type for CIO StreamRead
152c85a
Variable naming in IOStream, add error handling
3a30943
Merge branch 'develop' into feature/native-event-tracing
bbe294e
Add changelog entry
445390c
Add GC suppress finialize call to dispose method
297ebf5
Change from throwing errors to return byte amounts
fe76a7f
Fix IO stream scope for StreamHandle
fe6c03b
Add sealed to IOStorage class
e3ada22
Merge branch 'develop' into feature/native-event-tracing
56a343a
Remove GC SuppressFinalize call
f9a6e74
Merge branch 'feature/native-event-tracing' of github.com:spatialos/g…
423dc22
Switch from tuple return to out var for IO read
b7409d6
Add error handling to check if stream closed
bc9adbd
Clarify function name if stream is closed
daa7b16
Merge branch 'develop' into feature/native-event-tracing
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
seanjparker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this kind of cast is actually valid. the C# Interop has some fancy conversion class for this reason.
Might want to double check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be a compiler error if it was an invalid cast I think (the
as
casts fail at runtime)