/// <summary> /// Receive a data packet /// </summary> /// <param name="buffer"></param> /// <param name="ct"></param> /// <returns></returns> public async override Task <ProxyAsyncResult> ReceiveAsync( ArraySegment <byte> buffer, CancellationToken ct) { Message message = null; try { message = await ReceiveBlock.ReceiveAsync(ct).ConfigureAwait(false); var data = message.Content as DataMessage; var copy = Math.Min(data.Payload.Length, buffer.Count); Buffer.BlockCopy(data.Payload, 0, buffer.Array, buffer.Offset, copy); return(new ProxyAsyncResult { Address = data.Source, Count = copy }); } catch (Exception e) { if (ReceiveBlock.Completion.IsFaulted) { e = ReceiveBlock.Completion.Exception; } throw SocketException.Create("Failed to receive", e); } finally { message?.Dispose(); } }
public static TOutput Receive<TOutput> ( this ISourceBlock<TOutput> source, TimeSpan timeout, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException ("source"); if (timeout.TotalMilliseconds < -1) throw new ArgumentOutOfRangeException ("timeout"); if (timeout.TotalMilliseconds > int.MaxValue) throw new ArgumentOutOfRangeException ("timeout"); cancellationToken.ThrowIfCancellationRequested (); TOutput item; var receivableSource = source as IReceivableSourceBlock<TOutput>; if (receivableSource != null && receivableSource.TryReceive (null, out item)) return item; if (source.Completion.IsCompleted || source.Completion.IsCanceled || source.Completion.IsFaulted) throw new InvalidOperationException ( "No item could be received from the source."); int timeoutMilliseconds = (int)timeout.TotalMilliseconds; var block = new ReceiveBlock<TOutput> (cancellationToken, timeoutMilliseconds); var bridge = source.LinkTo (block, new DataflowLinkOptions { PropagateCompletion = true }); return block.WaitAndGet (bridge); }
protected override bool ReadProperties(BinaryReader reader) { BlockType blockType = (BlockType)((BitConverter.ToUInt16(Header.Extensions, 0) & Block.BlockTypeMask) >> 8); switch (blockType) { case BlockType.Receive: Block = new ReceiveBlock(); break; case BlockType.Send: Block = new SendBlock(); break; case BlockType.Open: Block = new OpenBlock(); break; case BlockType.Change: Block = new ChangeBlock(); break; } Block.ReadFromStream(reader); return(true); }
/// <summary> /// Attach send and receive blocks /// </summary> /// <param name="send"></param> /// <param name="receive"></param> public virtual void Attach( IPropagatorBlock <Message, Message> send, IPropagatorBlock <Message, Message> receive) { _socketSend = send.ConnectTo(SendBlock); _socketReceive = ReceiveBlock.ConnectTo(receive); }
/// <summary> /// Detach the connected socket /// </summary> public virtual void Detach() { _socketReceive.Dispose(); _socketSend.Dispose(); ReceiveBlock.Complete(); SendBlock.Complete(); }
/// <summary> /// Receive using async state machine /// </summary> /// <param name="buffer"></param> /// <param name="ct"></param> /// <returns></returns> private async Task <ProxyAsyncResult> ReceiveInternalAsync( ArraySegment <byte> buffer, CancellationToken ct) { var result = new ProxyAsyncResult(); while (true) { if (_lastData == null) { try { var message = await ReceiveBlock.ReceiveAsync(ct).ConfigureAwait(false); if (message.TypeId != MessageContent.Data) { message.Dispose(); continue; } _lastData = message.Content as DataMessage; message.Content = null; message.Dispose(); _offset = 0; } catch (Exception e) { if (ReceiveBlock.Completion.IsFaulted) { e = ReceiveBlock.Completion.Exception; } throw SocketException.Create("Failed to receive", e); } // Break on 0 sized packets if (_lastData == null) { break; } if (_lastData.Payload.Length == 0) { _lastData.Dispose(); _lastData = null; break; } #if PERF _transferred += _lastData.Payload.Length; Console.CursorLeft = 0; Console.CursorTop = 0; Console.WriteLine( $"{ _transferred / _transferredw.ElapsedMilliseconds} kB/sec"); #endif } result.Count = CopyBuffer(ref buffer); if (result.Count > 0) { break; } } return(result); }
public static Task<TOutput> ReceiveAsync<TOutput> ( this ISourceBlock<TOutput> source, TimeSpan timeout, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException ("source"); if (timeout.TotalMilliseconds < -1) throw new ArgumentOutOfRangeException ("timeout"); if (timeout.TotalMilliseconds > int.MaxValue) throw new ArgumentOutOfRangeException ("timeout"); cancellationToken.ThrowIfCancellationRequested (); int timeoutMilliseconds = (int)timeout.TotalMilliseconds; var block = new ReceiveBlock<TOutput> (cancellationToken, timeoutMilliseconds); var bridge = source.LinkTo (block); return block.AsyncGet (bridge); }
public void Visit(ReceiveBlock block) { FillValue(block); }
public void Visit(ReceiveBlock block) { throw new System.NotImplementedException(); }
public void PutBlock(UInt256 blockHash, ReceiveBlock block, UInt256 successorHash = null) { ThrowHelper.Sanity.ThrowIfTrue(block == null || GetBlock(successorHash) != null); }