Exemplo n.º 1
0
 public FrameTestsBase()
 {
     Services = new FakeServices();
     App = new FakeApp();
     Output = new FakeOutput();
     Input = new FakeInput();
     Frame = new Frame(
         new FrameContext
         {
             Services = Services,
             App = App.Call,
             Write = Output.Write,
             Flush = Output.Flush,
             End = Output.End,
         });
     Input.Consume = (baton, callback) => Frame.Consume(baton, (frame, ex) => callback(ex));
 }
Exemplo n.º 2
0
        private void Go(bool newFrame, Frame frame)
        {
            if (newFrame)
            {
                frame = _frame = new Frame(
                    new FrameContext
                    {
                        Services = _services,
                        App = _app,
                        Write = _socketSender.Write,
                        Flush = _socketSender.Flush,
                        End = ProduceEnd
                    });

                if (_baton.Buffer.Count != 0)
                {
                    if (frame.Consume(
                        _baton,
                        _frameConsumeCallback,
                        _fault))
                    {
                        return;
                    }
                }
            }

            while (frame.LocalIntakeFin == false)
            {
                SocketError recvError;
                var buffer = _baton.Available(128);
                var receiveCount = _socket.Receive(
                    buffer.Array,
                    buffer.Offset,
                    buffer.Count,
                    SocketFlags.None,
                    out recvError);

                if (recvError == SocketError.WouldBlock)
                {
                    _baton.Free();
                    if (ReceiveAsync(frame))
                    {
                        return;
                    }

                    continue;
                }

                if (recvError != SocketError.Success || receiveCount == 0)
                {
                    _baton.RemoteIntakeFin = true;
                }
                else
                {
                    _baton.Extend(receiveCount);
                }

                if (frame.Consume(
                    _baton,
                    _frameConsumeCallback,
                    _fault))
                {
                    return;
                }
            }
        }
Exemplo n.º 3
0
 private void ReceiveAsyncCompleted()
 {
     var frame = _receiveAsyncCompletedFrame;
     _receiveSocketEvent.Completed = null;
     _receiveAsyncCompletedFrame = null;
     try
     {
         Go(false, frame);
     }
     catch (Exception ex)
     {
         _fault(ex);
     }
 }
Exemplo n.º 4
0
        private bool ReceiveAsync(Frame frame)
        {
            // Lazy initialization of callback Action
            if (_receiveAsyncCompleted == null)
            {
                _receiveAsyncCompleted = ReceiveAsyncCompleted;
            }

            // Point callback at "this" only while an operation is occurring
            // to avoid a cyclic reference that can cause memory leaks if
            // the connection machinary doesn't wind down properly
            _receiveSocketEvent.Completed = _receiveAsyncCompleted;
            _receiveAsyncCompletedFrame = frame;
            if (!_socket.ReceiveAsync(_receiveSocketEvent))
            {
                _receiveSocketEvent.Completed = null;
                return false;
            }
            return true;
        }