Example #1
0
        internal static ImageflowException FromContext(JobContextHandle c, ulong defaultBufferSize = 2048)
        {
            var result = ErrorFetchResult.BufferTooSmall;

            for (var bufferSize = defaultBufferSize; bufferSize < MaxBufferSize; bufferSize *= 2)
            {
                result = TryGetErrorString(c, bufferSize, out var message);
                switch (result)
                {
                case ErrorFetchResult.Success:
                    return(new ImageflowException(message));

                case ErrorFetchResult.ContextInvalid:
                    return(null);

                case ErrorFetchResult.NoError:
                    return(null);

                case ErrorFetchResult.BufferTooSmall: break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            if (result == ErrorFetchResult.BufferTooSmall)
            {
                throw new ImageflowAssertionFailed(
                          $"Imageflow error and stacktrace exceeded {MaxBufferSize} bytes");
            }

            return(null);
        }
Example #2
0
        private static ErrorFetchResult TryGetErrorString(JobContextHandle c, ulong bufferSize, out string message)
        {
            message = null;
            if (c.IsClosed || c.IsInvalid)
            {
                return(ErrorFetchResult.ContextInvalid);
            }
            if (!NativeMethods.imageflow_context_has_error(c))
            {
                return(ErrorFetchResult.NoError);
            }
            var buffer = new byte[bufferSize];
            var pinned = GCHandle.Alloc(buffer, GCHandleType.Pinned);

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

                message = bytesWritten.ToUInt64() > 0
                    ? Encoding.UTF8.GetString(buffer, 0, (int)Math.Min(bytesWritten.ToUInt64(), bufferSize))
                    : "";

                return(everythingWritten ? ErrorFetchResult.Success : ErrorFetchResult.BufferTooSmall);
            }
            finally
            {
                pinned.Free();
            }
        }
        internal static ImageflowException FromContext(JobContextHandle c, ulong defaultBufferSize = 2048)
        {
            if (c.IsClosed || c.IsInvalid || !NativeMethods.imageflow_context_has_error(c))
            {
                return(null);
            }
            var buffer = new byte[defaultBufferSize];
            var pinned = GCHandle.Alloc(buffer, GCHandleType.Pinned);


            bool everythingWritten;

            string message = null;

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

                if (bytesWritten.ToUInt64() > 0)
                {
                    message = Encoding.UTF8.GetString(buffer, 0, (int)Math.Min(bytesWritten.ToUInt64(), defaultBufferSize)).Replace("\n", "");
                    message = message + message.Length;
                    Debug.WriteLine(message);
                    Console.WriteLine(message);
                }
            }
            finally
            {
                pinned.Free();
            }

            if (everythingWritten)
            {
                return(new ImageflowException(message ?? "Empty error and stacktrace"));
            }

            if (defaultBufferSize < MaxBufferSize)
            {
                return(FromContext(c, MaxBufferSize));
            }
            throw new ImageflowAssertionFailed(
                      $"Imageflow error and stacktrace exceeded {MaxBufferSize} bytes");
        }
Example #4
0
 public static extern bool imageflow_json_response_read(JobContextHandle context, JsonResponseHandle responseIn,
                                                        out int statusCodeOut, out IntPtr bufferUtf8NoNullsOut, out UIntPtr bufferSizeOut);
Example #5
0
 public static extern bool imageflow_context_print_and_exit_if_error(JobContextHandle context);
Example #6
0
 public static extern int imageflow_context_error_as_http_code(JobContextHandle context);
Example #7
0
 public static extern bool imageflow_context_error_try_clear(JobContextHandle context);
Example #8
0
 public static extern bool imageflow_json_response_read(JobContextHandle context, JsonResponseHandle response_in,
                                                        out int status_code_out, out IntPtr buffer_utf8_no_nulls_out, out UIntPtr buffer_size_out);
Example #9
0
 public static extern bool imageflow_context_error_write_to_buffer(JobContextHandle context, IntPtr buffer,
                                                                   UIntPtr bufferLength,
                                                                   out UIntPtr bytesWritten);
Example #10
0
 public static extern bool imageflow_context_get_output_buffer_by_id(JobContextHandle context,
                                                                     int ioId, out IntPtr resultBuffer, out UIntPtr resultBufferLength);
Example #11
0
 public static extern bool imageflow_context_add_output_buffer(JobContextHandle context, int ioId);
Example #12
0
 public static extern bool  imageflow_context_add_input_buffer(JobContextHandle context, int ioId, IntPtr buffer,
                                                               UIntPtr bufferByteCount, Lifetime lifetime);
Example #13
0
 public static extern IntPtr imageflow_context_memory_allocate(JobContextHandle context, IntPtr bytes,
                                                               IntPtr filename, int line);
Example #14
0
 public static extern bool imageflow_json_response_destroy(JobContextHandle context, IntPtr response);
Example #15
0
 public static extern bool imageflow_context_begin_terminate(JobContextHandle context);
Example #16
0
 public static extern bool imageflow_context_memory_free(JobContextHandle context, IntPtr pointer,
                                                         IntPtr filename, int line);
Example #17
0
 public static extern bool imageflow_context_has_error(JobContextHandle context);
Example #18
0
 public static extern IntPtr imageflow_context_send_json(JobContextHandle context, IntPtr method,
                                                         IntPtr jsonBuffer, UIntPtr jsonBufferSize);
Example #19
0
 public static extern bool imageflow_context_error_recoverable(JobContextHandle context);
Example #20
0
 public JobContext()
 {
     _handle = new JobContextHandle();
 }
Example #21
0
 public JsonResponseHandle(JobContextHandle parent, IntPtr ptr)
     : base(true)
 {
     ParentContext = parent ?? throw new ArgumentNullException(nameof(parent));
     SetHandle(ptr);
 }