public AwaitableWrapper <Msg> RecvMsgR(BytesView buf) { if (readEOF) { ThrowInvalidOperation(); } raR.Reset(); raR_deq = recvQueue.DequeueAsync(out raR_noWaiting); if (raR_continuation == null) { raR_continuation = () => { Msg m; var tmp = raR_deq; raR_deq = null; if (!tmp.TryGetResult(out m, out var ex)) { raR.SetException(ex); } else { try { m = rar_continuation2(m); } catch (Exception e) { raR.SetException(e); return; } raR.SetResult(m); } };
public virtual AwaitableWrapper <ReceiveFromResult> ReadFromAsyncR(int maxSize, IPEndPoint ep) { if (raRf == null) { raRf = new ReusableAwaiter <ReceiveFromResult> .BeginEndStateMachine <SocketStream>(this, (thiz, ar) => { var from = thiz.readfromEp; var buf = thiz.readfromBuffer; thiz.readfromEp = null; thiz.readfromBuffer = null; var read = thiz.Socket.EndReceiveFrom(ar, ref from); if (ar.CompletedSynchronously) { Interlocked.Add(ref ctr.Rsync, read); } else { Interlocked.Add(ref ctr.Rasync, read); } return(new ReceiveFromResult { From = (IPEndPoint)from, Buffer = new BytesSegment(buf, 0, read) }); }); } raRf.Reset(); readfromEp = ep; var buffer = BufferPool.GlobalGet(maxSize); readfromBuffer = buffer; Socket.BeginReceiveFrom(buffer, 0, maxSize, SocketFlags.None, ref readfromEp, raRf.ArgCallback, raRf.ArgState); return(new AwaitableWrapper <ReceiveFromResult>(raRf)); }
public override AwaitableWrapper WriteAsyncRImpl(BytesSegment bs) { if (raW == null) { raW = new ReusableAwaiter <VoidType> .BeginEndStateMachine <SocketStream1>(this, WriteEndMethod); } raW.Reset(); WriteBeginMethod(this, bs, raW.ArgCallback, raW.ArgState); return(new AwaitableWrapper(raW)); }
public AwaitableWrapper WriteMultipleAsyncR(BytesView bv) { if (raWm == null) { raWm = new ReusableAwaiter <VoidType> .BeginEndStateMachine <SocketStream1>(this, WriteMultipleEnd); } raWm.Reset(); ArraySegment <byte>[] bufList = PrepareWriteMultiple(bv); WriteMultipleBegin(this, bufList, raWm.ArgCallback, raWm.ArgState); return(new AwaitableWrapper(raWm)); }
private AwaitableWrapper <BytesSegment> ReadNBAsyncRWrapper(int maxSize) { if (_nb_ra == null) { _nb_ra = new ReusableAwaiter <BytesSegment>(); } else { _nb_ra.Reset(); } var bs = BufferPool.GlobalGetBs(maxSize); var awaiter = BaseStream.ReadAsyncR(bs); if (awaiter.IsCompleted) { _nb_ra.SetResult(bs.Sub(0, awaiter.GetResult())); } else { _nb_bs = bs; if (_nb_continuation == null) { _nb_continuation = () => { var _bs = _nb_bs; var _awaiter = _nb_awaiter; _nb_bs.ResetSelf(); _nb_awaiter = default(AwaitableWrapper <int>); int r; try { r = _awaiter.GetResult(); } catch (Exception e) { _nb_ra.SetException(e); return; } _nb_ra.SetResult(_bs.Sub(0, r)); } } ; _nb_awaiter = awaiter; awaiter.OnCompleted(_nb_continuation); } return(new AwaitableWrapper <BytesSegment>(_nb_ra)); }
protected override AwaitableWrapper <int> ReadAsyncRImpl(BytesSegment bs) { //return new AwaitableWrapper<int>(AsyncHelper.Run(async () => { // this.Socket.BeginReceive(bs.Bytes, bs.Offset, bs.Len, SocketFlags.None, BeginEndAwaiter.Callback, bea); // var read = Socket.EndReceive(await bea); // this.OnAsyncReadCompleted(read); // if (read == 0) // this.State |= MyStreamState.RemoteShutdown; // return read; //})); //// The above code was used to test BeginEndAwaiter, it works well, but the following code should be faster. if (raR == null) { raR = new ReusableAwaiter <int> .BeginEndStateMachine <SocketStream1>(this, ReadEndMethod); } raR.Reset(); ReadBeginMethod(this, bs, raR.ArgCallback, raR.ArgState); return(raR.ToWrapper()); }
//ReusableAwaiter<VoidType>.BeginEndStateMachine<SocketStream> raWt; public virtual AwaitableWrapper WriteToAsyncR(BytesSegment bs, IPEndPoint ep) { var raWt = new ReusableAwaiter <VoidType> .BeginEndStateMachine <SocketStream>(this, (thiz, ar) => { var r = thiz.Socket.EndSendTo(ar); if (ar.CompletedSynchronously) { Interlocked.Add(ref ctr.Wsync, r); } else { Interlocked.Add(ref ctr.Wasync, r); } if (thiz._ePPair.LocalEP == null) { thiz._ePPair.LocalEP = (IPEndPoint)thiz.Socket.LocalEndPoint; } return(r); }); raWt.Reset(); Socket.BeginSendTo(bs.Bytes, bs.Offset, bs.Len, SocketFlags.None, ep, raWt.ArgCallback, raWt.ArgState); return(new AwaitableWrapper(raWt)); }