Пример #1
0
        bool BeginSend()
        {
            while (true)
            {
                if (BufferIsEmpty())
                {
                    break;
                }

                int          written = 0;
                Exception    error;
                IAsyncResult ar0;

                try
                {
                    ar0 = socket.BeginSend(buffer.Data, ar =>
                    {
                        if (ar.CompletedSynchronously)
                        {
                            return;
                        }

                        written = EndSend(ar, out error);

                        // small optimization
                        if (error is ObjectDisposedException)
                        {
                            return;
                        }

                        scheduler.Post(() =>
                        {
                            if (error != null)
                            {
                                HandleSendError(error);
                            }
                            else
                            {
                                HandleSendResult(written, false);
                            }

                            if (!BeginSend() && continuation != null)
                            {
                                var c        = continuation;
                                continuation = null;
                                c();
                            }
                        });
                    });
                }
                catch (Exception e)
                {
                    HandleSendError(e);
                    break;
                }

                if (!ar0.CompletedSynchronously)
                {
                    return(true);
                }

                written = EndSend(ar0, out error);

                if (error != null)
                {
                    HandleSendError(error);
                    break;
                }
                else
                {
                    HandleSendResult(written, true);
                }
            }

            return(false);
        }