private static async Task UnpackToAsyncCore(Unpacker unpacker, int count, MessagePackObjectDictionary collection, CancellationToken cancellationToken) { for (int i = 0; i < count; i++) { if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } var key = unpacker.LastReadData; if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } if (unpacker.IsCollectionHeader) { var value = await unpacker.UnpackSubtreeDataAsyncCore(cancellationToken).ConfigureAwait(false); if (!value.Success) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } collection.Add(key, value.Value); } else { collection.Add(key, unpacker.LastReadData); } } }
private async Task UnpackToAsyncCore(Unpacker unpacker, T[] collection, int count, CancellationToken cancellationToken) { for (int i = 0; i < count; i++) { if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowMissingItem(i, unpacker); } T item; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { item = await this._itemSerializer.UnpackFromAsync(unpacker, cancellationToken).ConfigureAwait(false); } else { using (var subtreeUnpacker = unpacker.ReadSubtree()) { item = await this._itemSerializer.UnpackFromAsync(subtreeUnpacker, cancellationToken).ConfigureAwait(false); } } collection[i] = item; } }
protected internal override async Task UnpackToAsyncCore(Unpacker unpacker, Queue <TItem> collection, CancellationToken cancellationToken) { var itemsCount = UnpackHelpers.GetItemsCount(unpacker); for (int i = 0; i < itemsCount; i++) { if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowMissingItem(i, unpacker); } TItem item; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { item = await this._itemSerializer.UnpackFromAsync(unpacker, cancellationToken).ConfigureAwait(false); } else { using (Unpacker subtreeUnpacker = unpacker.ReadSubtree()) { item = await this._itemSerializer.UnpackFromAsync(subtreeUnpacker, cancellationToken).ConfigureAwait(false); } } collection.Enqueue(item); } }
protected internal override async Task <T> UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken) { if (!unpacker.IsArrayHeader) { SerializationExceptions.ThrowIsNotArrayHeader(unpacker); } var itemsCount = UnpackHelpers.GetItemsCount(unpacker); if (itemsCount != this._itemSerializers.Count) { SerializationExceptions.ThrowTupleCardinarityIsNotMatch(this._itemSerializers.Count, itemsCount, unpacker); } var unpackedItems = new List <object>(this._itemSerializers.Count); for (var i = 0; i < this._itemSerializers.Count; i++) { if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowMissingItem(i, unpacker); } unpackedItems.Add(await this._itemSerializers[i].UnpackFromAsync(unpacker, cancellationToken).ConfigureAwait(false)); } return(this.CreateTuple(unpackedItems)); }
private async void Receive(CancellationToken cancelToken) { try { while (_disposed == 0 && !IsFaulted && !cancelToken.IsCancellationRequested) { _ = await _unpacker.ReadAsync(cancelToken).ConfigureAwait(false); var nextMsg = await _unpacker.UnpackAsync <byte[]>(cancelToken).ConfigureAwait(false); var receivedTime = DateTime.Now; if (nextMsg == null) { continue; } try { _datagramEmitter?.DatagramReceived(this, nextMsg, receivedTime); } catch (Exception ex) { _logger?.LogError(ex, "Error emitting received datagram"); } } } catch (TargetInvocationException tiex) { _logger?.LogError(tiex, "Socket fault while receiving data"); OnConnectionFaulted(tiex); } catch (IOException ioex) { _logger?.LogError(ioex, "Socket fault while receiving data"); OnConnectionFaulted(ioex); } catch (SocketException sexc) { _logger?.LogError(sexc, "Socket fault while receiving data"); OnConnectionFaulted(sexc); } catch (TaskCanceledException) { _logger?.LogInformation("Receiving was stopped"); } catch (OperationCanceledException) { _logger?.LogInformation("Receiving was stopped"); } catch (AggregateException ex) when(ex.InnerExceptions.Any(p => p is OperationCanceledException)) { _logger?.LogInformation("Receiving was stopped"); } catch (Exception ex) { _logger?.LogError(ex, "Error while receiving data"); } }
protected internal override async Task <KeyValuePair <TKey, TValue> > UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken) { if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } var key = unpacker.LastReadData.IsNil ? default(TKey) : await this._keySerializer.UnpackFromAsync(unpacker, cancellationToken).ConfigureAwait(false); if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } var value = unpacker.LastReadData.IsNil ? default(TValue) : await this._valueSerializer.UnpackFromAsync(unpacker, cancellationToken).ConfigureAwait(false); return(new KeyValuePair <TKey, TValue>(key, value)); }
private static async Task UnpackToAsyncCore(Unpacker unpacker, List <MessagePackObject> collection, int count, CancellationToken cancellationToken) { for (var i = 0; i < count; i++) { if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } collection.Add(unpacker.LastReadData); } }
protected internal override async Task UnpackToAsyncCore(Unpacker unpacker, Queue collection, CancellationToken cancellationToken) { var itemsCount = UnpackHelpers.GetItemsCount(unpacker); for (int i = 0; i < itemsCount; i++) { if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowMissingItem(i, unpacker); } collection.Enqueue(unpacker.LastReadData); } }
private async Task <Tuple <int[], int[]> > ReadArrayMetadataAsync(Unpacker metadataUnpacker, CancellationToken cancellationToken) { if (!await metadataUnpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowUnexpectedEndOfStream(metadataUnpacker); } if (!metadataUnpacker.IsArrayHeader) { SerializationExceptions.ThrowIsNotArrayHeader(metadataUnpacker); } int[] lengths, lowerBounds; using (var lengthsUnpacker = metadataUnpacker.ReadSubtree()) { lengths = await this._int32ArraySerializer.UnpackFromAsync(lengthsUnpacker, cancellationToken).ConfigureAwait(false); } if (!await metadataUnpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowUnexpectedEndOfStream(metadataUnpacker); } if (!metadataUnpacker.IsArrayHeader) { SerializationExceptions.ThrowIsNotArrayHeader(metadataUnpacker); } using (var lowerBoundsUnpacker = metadataUnpacker.ReadSubtree()) { lowerBounds = await this._int32ArraySerializer.UnpackFromAsync(lowerBoundsUnpacker, cancellationToken).ConfigureAwait(false); } return(Tuple.Create(lengths, lowerBounds)); }
public static async Task <ArraySegment <T> > UnpackGenericArraySegmentFromAsync <T>(Unpacker unpacker, MessagePackSerializer <T> itemSerializer, CancellationToken cancellationToken) { T[] array = new T[unpacker.ItemsCount]; for (int i = 0; i < array.Length; i++) { if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowMissingItem(i, unpacker); } array[i] = await itemSerializer.UnpackFromAsyncCore(unpacker, cancellationToken).ConfigureAwait(false); } return(new ArraySegment <T>(array)); }
private async Task UnpackToAsyncCore(Unpacker unpacker, Dictionary <TKey, TValue> collection, int count, CancellationToken cancellationToken) { for (int i = 0; i < count; i++) { if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } TKey key; if (unpacker.IsCollectionHeader) { using (var subTreeUnpacker = unpacker.ReadSubtree()) { key = await this._keySerializer.UnpackFromAsyncCore(subTreeUnpacker, cancellationToken).ConfigureAwait(false); } } else { key = await this._keySerializer.UnpackFromAsyncCore(unpacker, cancellationToken).ConfigureAwait(false); } if (!unpacker.Read()) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } if (unpacker.IsCollectionHeader) { using (var subTreeUnpacker = unpacker.ReadSubtree()) { collection.Add(key, await this._valueSerializer.UnpackFromAsyncCore(subTreeUnpacker, cancellationToken).ConfigureAwait(false)); } } else { collection.Add(key, await this._valueSerializer.UnpackFromAsyncCore(unpacker, cancellationToken).ConfigureAwait(false)); } } }
private async Task UnpackToAsyncCore(Unpacker unpacker, List <T> collection, int count, CancellationToken cancellationToken) { for (int i = 0; i < count; i++) { if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } if (unpacker.IsCollectionHeader) { using (var subTreeUnpacker = unpacker.ReadSubtree()) { collection.Add(await this._itemSerializer.UnpackFromAsync(subTreeUnpacker, cancellationToken).ConfigureAwait(false)); } } else { collection.Add(await this._itemSerializer.UnpackFromAsync(unpacker, cancellationToken).ConfigureAwait(false)); } } }
private static async Task UnpackToAsyncCore(Unpacker unpacker, NameValueCollection collection, int keyCount, CancellationToken cancellationToken) { for (var k = 0; k < keyCount; k++) { var key = await unpacker.ReadStringAsync(cancellationToken).ConfigureAwait(false); if (!key.Success) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false)) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } if (!unpacker.IsArrayHeader) { throw new SerializationException("Invalid NameValueCollection value."); } var itemsCount = UnpackHelpers.GetItemsCount(unpacker); using (var valuesUnpacker = unpacker.ReadSubtree()) { for (var v = 0; v < itemsCount; v++) { var value = await valuesUnpacker.ReadStringAsync(cancellationToken).ConfigureAwait(false); if (!value.Success) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } collection.Add(key.Value, value.Value); } } } }