コード例 #1
0
 public bool ConnectAsync(SocketAsyncEventArgs e)
 {
     lock (_lock)
     {
         if (ConnectPending)
         {
             _pendings.Add(succeeded =>
             {
                 Connected = succeeded;
                 e.InvokeSetResults(succeeded ? SocketError.Success : SocketError.ConnectionRefused, 0, SocketFlags.None);
                 e.InvokeOnCompleted();
             });
             return true;
         }
         else
         {
             e.SocketError = SocketError.Success;
             Connected = true;
             return false;
         }
     }
 }
コード例 #2
0
        public bool SendAsync(SocketAsyncEventArgs e)
        {
            lock (_lock)
            {
                if (SendPending)
                {
                    // for keep sent data safe, it should be copied to receive buffer.
                    var receiveBuffer = new byte[e.Count];
                    Array.Copy(e.Buffer, e.Offset, receiveBuffer, 0, e.Count);

                    _pendings.Add(succeeded =>
                    {
                        if (succeeded)
                        {
                            if (SendDoneAfterReceive)
                            {
                                Server.OnReceive(new ArraySegment<byte>(receiveBuffer));
                                e.InvokeSetResults(SocketError.Success, e.Count, SocketFlags.None);
                                e.InvokeOnCompleted();
                            }
                            else
                            {
                                e.InvokeSetResults(SocketError.Success, e.Count, SocketFlags.None);
                                e.InvokeOnCompleted();
                                Server.OnReceive(new ArraySegment<byte>(receiveBuffer));
                            }
                        }
                        else
                        {
                            e.InvokeSetResults(SocketError.ConnectionReset, 0, SocketFlags.None);
                            e.InvokeOnCompleted();
                        }
                    });

                    return true;
                }
                else
                {
                    e.InvokeSetResults(SocketError.Success, e.Count, SocketFlags.None);
                    Server.OnReceive(new ArraySegment<byte>(e.Buffer, e.Offset, e.Count));
                    return false;
                }
            }
        }