public AsyncReadResult BeginRead(int offset, int size, TimeSpan timeout, WaitCallback callback, object state) { this.ThrowIfNotOpen(); Microsoft.ServiceBus.Channels.ConnectionUtilities.ValidateBufferBounds(this.AsyncReadBufferSize, offset, size); this.readCallback = callback; this.readObject = state; AsyncReadResult asyncReadResult = AsyncReadResult.Completed; bool flag = false; try { try { if (Interlocked.CompareExchange(ref this.asyncReadPending, 1, 0) != 0) { throw Fx.Exception.AsError(new InvalidOperationException(Resources.ClientWebSocketConnectionReadPending), this.Activity); } IAsyncResult asyncResult = this.webSocket.BeginReceive(this.AsyncReadBuffer, offset, size, timeout, this.onRead, this); if (asyncResult.CompletedSynchronously) { this.readCallback = null; this.readObject = null; this.bytesRead = this.webSocket.EndReceive(asyncResult); } else { asyncReadResult = AsyncReadResult.Queued; } flag = true; } catch (IOException oException1) { IOException oException = oException1; throw Fx.Exception.AsError(ClientWebSocketConnection.ConvertIOException(oException), this.Activity); } catch (ObjectDisposedException objectDisposedException1) { ObjectDisposedException objectDisposedException = objectDisposedException1; if (this.webSocket.State != ServiceBusClientWebSocket.WebSocketState.Faulted) { throw Fx.Exception.AsError(new CommunicationObjectAbortedException(objectDisposedException.Message, objectDisposedException), this.Activity); } throw Fx.Exception.AsError(new CommunicationObjectFaultedException(objectDisposedException.Message, objectDisposedException), this.Activity); } } finally { if (!flag || asyncReadResult == AsyncReadResult.Completed) { Interlocked.Exchange(ref this.asyncReadPending, 0); } } return(asyncReadResult); }
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { // according to the docs, the callback is optional var readTask = this.ReadAsync(buffer, offset, count, CancellationToken.None); var readResult = new AsyncReadResult(state, readTask, this); if (callback != null) { readTask.ContinueWith(_ => callback(readResult)); } return(readResult); }
public static async Task LoadAsync <T>(string filename, AsyncReadResult <T> callBack) { T data; WestdriveSettings.CheckIO = IOState.pending; WestdriveSettings.CheckIO = IOState.working; using (var file = File.OpenRead(filename)) { data = await Task.Run(() => { return(data = Serializer.Deserialize <T>(file)); }); file.Close(); } if (callBack != null) { callBack(data); } WestdriveSettings.CheckIO = IOState.ready; }
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { // according to the docs, the callback is optional var readTask = this.ReadAsync(buffer, offset, count, CancellationToken.None); var readResult = new AsyncReadResult(state, readTask, this); if (callback != null) { readTask.ContinueWith(_ => callback(readResult)); } return readResult; }
private void ReadComplete(bool calledSynchronously) { try { int num = this.connection.EndRead(); if (num != 0) { SocketMessageHelper.ReadBytesAsyncResult readBytesAsyncResult = this; readBytesAsyncResult.totalBytesRead = readBytesAsyncResult.totalBytesRead + num; if (this.totalBytesRead == this.requestedCount) { this.Bytes = new ArraySegment <byte>(this.connection.AsyncReadBuffer, 0, this.totalBytesRead); base.Complete(calledSynchronously); } else { AsyncReadResult asyncReadResult = AsyncReadResult.Completed; while (this.totalBytesRead != this.requestedCount) { if (asyncReadResult == AsyncReadResult.Completed) { asyncReadResult = this.BeginRead(); if (asyncReadResult != AsyncReadResult.Completed) { continue; } num = this.connection.EndRead(); if (num != 0) { SocketMessageHelper.ReadBytesAsyncResult readBytesAsyncResult1 = this; readBytesAsyncResult1.totalBytesRead = readBytesAsyncResult1.totalBytesRead + num; } else { if (this.throwIfNotAllRead) { TimeSpan originalTimeout = this.timeoutHelper.OriginalTimeout - this.timeoutHelper.RemainingTime(); throw new InvalidDataException(string.Concat(Resources.PrematureEOF, " ", originalTimeout.ToString())); } this.Bytes = new ArraySegment <byte>(this.connection.AsyncReadBuffer, 0, this.totalBytesRead); base.Complete(calledSynchronously); return; } } else { break; } } } } else { if (this.throwIfNotAllRead) { TimeSpan timeSpan = this.timeoutHelper.OriginalTimeout - this.timeoutHelper.RemainingTime(); throw new InvalidDataException(string.Concat(Resources.PrematureEOF, " ", timeSpan.ToString())); } this.Bytes = new ArraySegment <byte>(this.connection.AsyncReadBuffer, 0, this.totalBytesRead); base.Complete(calledSynchronously); } } catch (Exception exception1) { Exception exception = exception1; if (Fx.IsFatal(exception)) { throw; } base.Complete(calledSynchronously, exception); } }
protected internal override async Task <DictionaryEntry> UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken) { if (unpacker.IsArrayHeader) { var key = await unpacker.ReadObjectAsync(cancellationToken).ConfigureAwait(false); if (!key.Success) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } var value = await unpacker.ReadObjectAsync(cancellationToken).ConfigureAwait(false); if (!value.Success) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } return(new DictionaryEntry(key.Value, value.Value)); } else { // Previous DictionaryEntry serializer accidentally pack it as map... AsyncReadResult <MessagePackObject> key = default(AsyncReadResult <MessagePackObject>); AsyncReadResult <MessagePackObject> value = default(AsyncReadResult <MessagePackObject>); for (var propertyName = await unpacker.ReadStringAsync(cancellationToken).ConfigureAwait(false); (!key.Success || !value.Success) && propertyName.Success; propertyName = await unpacker.ReadStringAsync(cancellationToken).ConfigureAwait(false)) { switch (propertyName.Value) { case "Key": { key = await unpacker.ReadObjectAsync(cancellationToken).ConfigureAwait(false); if (!key.Success) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } break; } case "Value": { value = await unpacker.ReadObjectAsync(cancellationToken).ConfigureAwait(false); if (!value.Success) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } break; } } } if (!key.Success) { SerializationExceptions.ThrowMissingProperty("Key"); } if (!value.Success) { SerializationExceptions.ThrowMissingProperty("Value"); } return(new DictionaryEntry(key.Value, value.Value)); } }