Exemplo n.º 1
0
        internal void Completed(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError != SocketError.Success)
            {
                Disconnect("ClientSocket.Completed() e.SocketError != SocketError.Success");
                return;
            }

            switch (e.LastOperation)
            {
            case SocketAsyncOperation.Receive:
                ReceiveQueue.Add(this);
                break;

            case SocketAsyncOperation.Send:
                SendSync.Set();
                break;

            case SocketAsyncOperation.Connect:
                IsConnected = true;
                OnConnected?.Invoke();
                Receive();
                break;
            }
        }
        internal static void Received(object sender, SocketAsyncEventArgs e)
        {
Start:
            var token = (ClientSocket)e.UserToken;

            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                try
                {
                    ReceiveQueue.Add(e);
                    token.ReceiveSync.WaitOne();
                    if (!token.Socket.ReceiveAsync(e))
                    {
                        goto Start;
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    CloseClientSocket(e);
                }
            }
            else
            {
                CloseClientSocket(e);
            }
        }
Exemplo n.º 3
0
        private void Received(object sender, SocketAsyncEventArgs e)
        {
            if (e.BytesTransferred <= 0 || e.SocketError != SocketError.Success)
            {
                Disconnect();
                return;
            }

            try
            {
                ReceiveQueue.Add(e);
                ReceiveSync.WaitOne();
                if (!Socket.ReceiveAsync(e))
                {
                    Received(null, e);
                }
            }
            catch
            {
                Disconnect();
            }
        }
Exemplo n.º 4
0
        private void Received(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError != SocketError.Success || e.BytesTransferred == 0)
            {
                Disconnect("ClientSocket.Received() if (e.SocketError != SocketError.Success || e.BytesTransferred == 0)");
                return;
            }

            try
            {
                ReceiveQueue.Add(e);
                ReceiveSync.WaitOne();
                if (!Socket.ReceiveAsync(e))
                {
                    Received(null, e);
                }
            }
            catch (Exception ex)
            {
                Disconnect("ClientSocket.Received() Catch: " + ex.Message + " #### " + ex.StackTrace);
            }
        }