Skip to content

Commit

Permalink
.NET: bindings cleanup/fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed Sep 6, 2017
1 parent bc337ab commit 9e2a4fc
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 25 deletions.
1 change: 0 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Language: Cpp
# BasedOnStyle: WebKit
AccessModifierOffset: -4
ConstructorInitializerIndentWidth: 4
AlignEscapedNewlinesLeft: false
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
Expand Down
31 changes: 31 additions & 0 deletions bindings/dn/Imageflow.Test/TestApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Diagnostics;
using Xunit;
using Imageflow;
using System.Dynamic;
using System.IO;
using System.Text;
using Imageflow.Native;
using Xunit.Abstractions;

namespace Imageflow.Test
{
public class TestApi
{
private readonly ITestOutputHelper output;

public TestApi(ITestOutputHelper output)
{
this.output = output;
}

[Fact]
public void TestCreateDestroyContext()
{
using (var c = new JobContext())
{
c.AssertReady();
}
}
}
}
9 changes: 5 additions & 4 deletions bindings/dn/Imageflow/ImageflowException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ImageflowException : Exception
{
const int MaxBufferSize = 8096;

internal ImageflowException(string message) : base(message)
private ImageflowException(string message) : base(message)
{

}
Expand All @@ -25,14 +25,15 @@ public static Exception FromContext(JobContext c, bool fullPaths = true, ulong d
var buffer = new byte[defaultBufferSize];
var pinned = GCHandle.Alloc(buffer, GCHandleType.Pinned);

var bytesWritten = UIntPtr.Zero;
var everythingWritten = false;

bool everythingWritten;

string message = null;
try
{

everythingWritten = NativeMethods.imageflow_context_error_write_to_buffer(c.Pointer,
pinned.AddrOfPinnedObject(), new UIntPtr((ulong) buffer.LongLength), out bytesWritten);
pinned.AddrOfPinnedObject(), new UIntPtr((ulong) buffer.LongLength), out var bytesWritten);

if (bytesWritten.ToUInt64() > 0)
{
Expand Down
1 change: 1 addition & 0 deletions bindings/dn/Imageflow/ImageflowUnmanagedReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Imageflow
/// <summary>
/// An UnmanagedMemoryStream that checks that the underlying Imageflow context isn't in a disposed or errored state
/// </summary>
/// <inheritdoc cref="UnmanagedMemoryStream"/>
public class ImageflowUnmanagedReadStream : UnmanagedMemoryStream
{
private readonly IAssertReady _underlying;
Expand Down
15 changes: 6 additions & 9 deletions bindings/dn/Imageflow/JobContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public JobContext()
throw new Exception($".NET Imageflow bindings only support ABI {NativeMethods.ABI_MAJOR}.{NativeMethods.ABI_MINOR}. libimageflow ABI {major}.{minor} is loaded.");
}

internal void AddPinnedData(GCHandle handle)
private void AddPinnedData(GCHandle handle)
{
if (_pinned == null) _pinned = new List<GCHandle>();
_pinned.Add(handle);
}

public bool HasError => NativeMethods.imageflow_context_has_error(Pointer);

public static byte[] SerializeToJson<T>(T obj){
private static byte[] SerializeToJson<T>(T obj){
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream, new UTF8Encoding(false))){
JsonSerializer.Create().Serialize(writer, obj);
Expand Down Expand Up @@ -82,7 +82,7 @@ public void AssertReady()
{
if (HasError) throw ImageflowException.FromContext(this);
}

public JsonResponse ExecuteImageResizer4CommandString( int inputId, int outputId, string commands)
{
var message = new
Expand All @@ -96,7 +96,7 @@ public JsonResponse ExecuteImageResizer4CommandString( int inputId, int outputId
command_string = new
{
kind = "ir4",
value = "w=200&h=200&scale=both&format=jpg",
value = commands,
decode = inputId,
encode = outputId
}
Expand Down Expand Up @@ -208,11 +208,8 @@ public void AddOutputBuffer(int ioId)
public Stream GetOutputBuffer(int ioId)
{
AssertReady();
IntPtr buffer;
UIntPtr bufferSize;
AssertReady();
if (!NativeMethods.imageflow_context_get_output_buffer_by_id(Pointer, ioId, out buffer,
out bufferSize))
if (!NativeMethods.imageflow_context_get_output_buffer_by_id(Pointer, ioId, out var buffer,
out var bufferSize))
{
AssertReady();
throw new ImageflowAssertionFailed("AssertReady should raise an exception if method fails");
Expand Down
16 changes: 5 additions & 11 deletions bindings/dn/Imageflow/JsonResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class JsonResponse : IDisposable, IAssertReady
private IntPtr _ptr;
private readonly JobContext _parent;

internal IntPtr Pointer
private IntPtr Pointer
{
get
{
Expand Down Expand Up @@ -39,26 +39,20 @@ private void Read(out int statusCode, out IntPtr utf8Buffer, out UIntPtr bufferS

public int GetStatusCode()
{
int statusCode;
IntPtr utf8Buffer;
UIntPtr bufferSize;
Read(out statusCode, out utf8Buffer, out bufferSize);
Read(out var statusCode, out var utf8Buffer, out var bufferSize);
return statusCode;
}

public Stream GetStream()
{
int statusCode;
IntPtr utf8Buffer;
UIntPtr bufferSize;
Read(out statusCode, out utf8Buffer, out bufferSize);
Read(out var statusCode, out var utf8Buffer, out var bufferSize);
return new ImageflowUnmanagedReadStream(this, utf8Buffer, bufferSize);
}

public T Deserialize<T>() where T : class
{
using (var reader = new StreamReader(GetStream(), Encoding.UTF8))
return JsonSerializer.Create().Deserialize((JsonReader) new JsonTextReader(reader), typeof(T)) as T;
return JsonSerializer.Create().Deserialize(new JsonTextReader(reader), typeof(T)) as T;
}

public dynamic DeserializeDynamic()
Expand Down Expand Up @@ -108,7 +102,7 @@ protected virtual void Dispose(bool disposing)
public void AssertReady()
{
_parent.AssertReady();
if (this.Pointer == IntPtr.Zero) throw new ImageflowAssertionFailed("Pointer must never return zero");
if (this.Pointer == IntPtr.Zero) throw new ImageflowDisposedException("JsonResponse");
}

}
Expand Down

0 comments on commit 9e2a4fc

Please sign in to comment.