Пример #1
0
        public bool Send(SenderAsyncEventArgs args)
        {
            args.Error = _socketException;

            _senderContext.SendState = 0;
            _senderContext.PartIndex = 0;
            _senderContext.EventArgs = args;
            _senderContext.CallBack  = args.Completed;

            args.Completed = SendDataTask;
            args.UserToken = _senderContext;

            return(SendDataTask(false, args));
        }
Пример #2
0
        public bool Flush(SenderAsyncEventArgs args)
        {
            if (_socketException != null)
            {
                args.Error = _socketException;
                return(false);
            }
            if (_writingBufferIsFlushed == 0)
            {
                _sendingQueue.Enqueue(new ArraySegment <byte>(_writingBuffer, 0, _writingOffset));
                Interlocked.Exchange(ref _writingBufferIsFlushed, 1);
            }

            EnterFlushing();
            return(false);
        }
Пример #3
0
        private bool SendDataTask(bool async, SenderAsyncEventArgs args)
        {
            var context = (SenderContext)args.UserToken;

TryAgain:

            if (context.EventArgs.HasError)
            {
                return(CallOnSendCompleted(async, context));
            }

            if (context.SendState == 0)
            {
                var writeResult = WriteArgumentsCountLine(context.EventArgs.DataToSend.Length);
                if (!writeResult.HasValue)
                {
                    return(true);
                }
                if (!writeResult.Value)
                {
                    if (Flush(context.EventArgs))
                    {
                        return(true);
                    }
                    goto TryAgain;
                }
                context.SendState = 1;
            }

            if (context.SendState > 0 && context.SendState < 4)
            {
                for (; context.PartIndex < context.EventArgs.DataToSend.Length; context.PartIndex++)
                {
                    if (context.SendState == 1)
                    {
                        var writeResult = WriteDataSizeLine(context.EventArgs.DataToSend[context.PartIndex].Length);
                        if (!writeResult.HasValue)
                        {
                            return(true);
                        }
                        if (!writeResult.Value)
                        {
                            if (Flush(context.EventArgs))
                            {
                                return(true);
                            }
                            goto TryAgain;
                        }
                        context.SendState    = 2;
                        context.ArraySegment = new ArraySegment <byte>(context.EventArgs.DataToSend[context.PartIndex], 0,
                                                                       context.EventArgs.DataToSend[context.PartIndex].Length);
                    }

                    if (context.SendState == 2)
                    {
                        var writeResult = WriteData(context.ArraySegment);
                        if (!writeResult.HasValue)
                        {
                            return(true);
                        }
                        context.ArraySegment = writeResult.Value;
                        if (context.ArraySegment.Count > 0)
                        {
                            if (Flush(context.EventArgs))
                            {
                                return(true);
                            }
                            goto TryAgain;
                        }

                        context.SendState = 3;
                    }

                    if (context.SendState == 3)
                    {
                        var writeResult = WriteNewLineToBuffer();
                        if (!writeResult.HasValue)
                        {
                            return(true);
                        }
                        if (!writeResult.Value)
                        {
                            if (Flush(context.EventArgs))
                            {
                                return(true);
                            }
                            goto TryAgain;
                        }
                        context.SendState = 1;
                    }
                }
            }

            if (context.SendState != 4 && _autoFlush)
            {
                context.SendState = 4;
                if (Flush(context.EventArgs))
                {
                    return(true);
                }
                goto TryAgain;
            }

            return(CallOnSendCompleted(async, context));
        }
Пример #4
0
 private void SendDataTask(SenderAsyncEventArgs args)
 {
     SendDataTask(true, args);
 }