示例#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);
        }
示例#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");
        }
示例#4
0
 public static extern bool imageflow_json_response_read(JobContextHandle context, JsonResponseHandle responseIn,
                                                        out int statusCodeOut, out IntPtr bufferUtf8NoNullsOut, out UIntPtr bufferSizeOut);
示例#5
0
 public static extern bool imageflow_context_print_and_exit_if_error(JobContextHandle context);
示例#6
0
 public static extern int imageflow_context_error_as_http_code(JobContextHandle context);
示例#7
0
 public static extern bool imageflow_context_error_try_clear(JobContextHandle context);
示例#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);
示例#9
0
 public static extern bool imageflow_context_error_write_to_buffer(JobContextHandle context, IntPtr buffer,
                                                                   UIntPtr bufferLength,
                                                                   out UIntPtr bytesWritten);
示例#10
0
 public static extern bool imageflow_context_get_output_buffer_by_id(JobContextHandle context,
                                                                     int ioId, out IntPtr resultBuffer, out UIntPtr resultBufferLength);
示例#11
0
 public static extern bool imageflow_context_add_output_buffer(JobContextHandle context, int ioId);
示例#12
0
 public static extern bool  imageflow_context_add_input_buffer(JobContextHandle context, int ioId, IntPtr buffer,
                                                               UIntPtr bufferByteCount, Lifetime lifetime);
示例#13
0
 public static extern IntPtr imageflow_context_memory_allocate(JobContextHandle context, IntPtr bytes,
                                                               IntPtr filename, int line);
示例#14
0
 public static extern bool imageflow_json_response_destroy(JobContextHandle context, IntPtr response);
示例#15
0
 public static extern bool imageflow_context_begin_terminate(JobContextHandle context);
示例#16
0
 public static extern bool imageflow_context_memory_free(JobContextHandle context, IntPtr pointer,
                                                         IntPtr filename, int line);
示例#17
0
 public static extern bool imageflow_context_has_error(JobContextHandle context);
示例#18
0
 public static extern IntPtr imageflow_context_send_json(JobContextHandle context, IntPtr method,
                                                         IntPtr jsonBuffer, UIntPtr jsonBufferSize);
示例#19
0
 public static extern bool imageflow_context_error_recoverable(JobContextHandle context);
示例#20
0
 public JobContext()
 {
     _handle = new JobContextHandle();
 }
示例#21
0
 public JsonResponseHandle(JobContextHandle parent, IntPtr ptr)
     : base(true)
 {
     ParentContext = parent ?? throw new ArgumentNullException(nameof(parent));
     SetHandle(ptr);
 }