private void Write(IList <ArraySegment <byte> > buffers, object state)
        {
            var args = new SocketAsyncEventArgs {
                BufferList = buffers, UserToken = state
            };

            args.Completed += (_, __) =>
            {
                var error = args.GetError();
                if (error == null)
                {
                    var result           = args.BytesTransferred;
                    var remainingBuffers = SocketHelpers.RemainingBuffers(buffers, result);
                    if (remainingBuffers.Count != 0)
                    {
                        this.scheduler.Schedule(
                            () =>
                        {
                            try
                            {
                                this.Write(remainingBuffers, state);
                            }
                            catch (Exception ex)
                            {
                                this.OnWriteComplete(state, ex);
                            }
                        });
                    }
                    else
                    {
                        this.scheduler.Schedule(() => this.OnWriteComplete(state));
                    }
                }
                else
                {
                    this.scheduler.Schedule(() => this.OnWriteComplete(state, error));
                }
            };
            this.socket.SendAsync(args);
        }
Exemplo n.º 2
0
 private void Write(IList <ArraySegment <byte> > buffers, object state)
 {
     this.socket.BeginSend(
         buffers,
         SocketFlags.None,
         asyncResult =>
     {
         try
         {
             var result           = this.socket.EndSend(asyncResult);
             var remainingBuffers = SocketHelpers.RemainingBuffers(buffers, result);
             if (remainingBuffers.Count != 0)
             {
                 this.scheduler.Schedule(
                     () =>
                 {
                     try
                     {
                         this.Write(remainingBuffers, state);
                     }
                     catch (Exception ex)
                     {
                         this.OnWriteComplete(state, ex);
                     }
                 });
             }
             else
             {
                 this.scheduler.Schedule(() => this.OnWriteComplete(state));
             }
         }
         catch (Exception ex)
         {
             this.scheduler.Schedule(() => this.OnWriteComplete(state, ex));
         }
     },
         state);
 }