private async Task <PPError> ReadResponseBodyAsyncCore(byte[] buffer, int bytes_to_read, MessageLoop messageLoop = null) { var tcs = new TaskCompletionSource <PPError>(); EventHandler <PPError> handler = (s, e) => { tcs.TrySetResult(e); }; try { HandleReadResponseBody += handler; if (MessageLoop == null && messageLoop == null) { ReadResponseBody(buffer, bytes_to_read); } else { Action <PPError> action = new Action <PPError>((e) => { var result = (PPError)PPBURLLoader.ReadResponseBody(this, buffer, bytes_to_read, new BlockUntilComplete()); tcs.TrySetResult(result); } ); InvokeHelper(action, messageLoop); } return(await tcs.Task); } catch (Exception exc) { Console.WriteLine(exc.Message); tcs.SetException(exc); return(PPError.Aborted); } finally { HandleReadResponseBody -= handler; } }
/// <summary> /// This function is used to read the response body. The size of the buffer /// must be large enough to hold the specified number of bytes to read. /// This function might perform a partial read. /// </summary> /// <param name="buffer">A byte array for the response body.</param> /// <param name="bytesToRead">The number of bytes to read.</param> /// <returns></returns> public PPError ReadResponseBody(byte[] buffer, int bytesToRead) => (PPError)PPBURLLoader.ReadResponseBody(this, buffer, bytesToRead, new CompletionCallback(OnReadResponseBody));