Пример #1
0
        public Client()
        {
            // get data from connection.cfg
            XDocument doc = XDocument.Load("connection.config");

            var _ip = doc.Descendants("ip");

            ip = "";
            foreach (var i in _ip)
            {
                ip = i.Value;
            }

            var _port = doc.Descendants("port");

            port = 0;
            foreach (var i in _port)
            {
                port = int.Parse(i.Value);
            }

            OnlineFriendsList   = null;
            OfflineFriendsList  = null;
            OnlineUsersList     = null;
            OfflineUsersList    = null;
            IncomeRequestsList  = null;
            OutcomeRequestsList = null;

            Name          = null;
            client        = new MpClient();
            Conversations = new Conversations();

            ConnectionBreaks += delegate { isLoggedIn = false; };
        }
Пример #2
0
        /// <summary>
        /// Logout from server.
        /// </summary>
        public void Logout()
        {
            isLoggedIn = false;

            Name = null;
            OnlineFriendsList   = null;
            OfflineFriendsList  = null;
            OnlineUsersList     = null;
            OfflineUsersList    = null;
            IncomeRequestsList  = null;
            OutcomeRequestsList = null;
            Conversations       = new Conversations();

            try
            {
                client.Close();
            }
            catch (ConnectionInterruptedException)
            {
                // dont mind because we exit
            }
        }
Пример #3
0
		/// <summary>
		/// Logout from server.
		/// </summary>
		public void Logout()
		{
			isLoggedIn = false;

			Name = null;
			OnlineFriendsList = null;
			OfflineFriendsList = null;
			OnlineUsersList = null;
			OfflineUsersList = null;
			IncomeRequestsList = null;
			OutcomeRequestsList = null;
			Conversations = new Conversations();

			try
			{
				client.SendMessage(new LogoutRequestMessage());
			}
			catch (ConnectionInterruptedException)
			{
				// dont mind because we exit
			}
			finally
			{
				try
				{
					client.Close();
				}
				catch (ConnectionInterruptedException)
				{
					// dont mind because we exit
				}
			}
		}
Пример #4
0
		public Client()
		{
			// get data from connection.cfg
			XDocument doc = XDocument.Load("connection.config");
			
			var _ip = doc.Descendants("ip");
			ip = "";
			foreach (var i in _ip) ip = i.Value;

			var _port = doc.Descendants("port");
			port = 0;
			foreach (var i in _port) port = int.Parse(i.Value);

			OnlineFriendsList = null;
			OfflineFriendsList = null;
			OnlineUsersList = null;
			OfflineUsersList = null;
			IncomeRequestsList = null;
			OutcomeRequestsList = null;

			Name = null;
			client = new MpClient();
			Conversations = new Conversations();

			ConnectionBreaks += delegate { isLoggedIn = false; };
		}
Пример #5
0
        /// <summary>
        /// Listen for messages from server.
        /// </summary>
        public async void Listen()
        {
            if (!isLoggedIn)
            {
                return;
            }

            // message from server
            Message message;

            while (true)
            {
                try
                {
                    // wait for message
                    message = await client.ReceiveMessageAsync();
                }
                catch (ConnectionInterruptedException)
                {
                    if (isLoggedIn)
                    {
                        ConnectionBreaks?.Invoke();
                    }

                    return;
                }

                // handle message
                if (message is AllUsersMessage)
                {
                    OnlineUsersList  = new List <string>(((AllUsersMessage)message).OnlineUsers);
                    OfflineUsersList = new List <string>(((AllUsersMessage)message).OfflineUsers);
                }
                else if (message is FriendsMessage)
                {
                    OnlineFriendsList  = new List <string>(((FriendsMessage)message).OnlineFriends);
                    OfflineFriendsList = new List <string>(((FriendsMessage)message).OfflineFriends);
                }
                else if (message is IncomeFriendshipRequestsMessage)
                {
                    IncomeRequestsList = new List <string>(((IncomeFriendshipRequestsMessage)message).Logins);
                }
                else if (message is OutcomeFriendshipRequestsMessage)
                {
                    OutcomeRequestsList = new List <string>(((OutcomeFriendshipRequestsMessage)message).Logins);
                }
                else if (message is UserActionMessage)
                {
                    UserActionMessage msg = (UserActionMessage)message;

                    switch (msg.Action)
                    {
                    case UserActions.GoOnline:
                        FriendGoOnline?.Invoke(msg.UserLogin);
                        break;

                    case UserActions.GoOffline:
                        FriendGoOffline?.Invoke(msg.UserLogin);
                        break;

                    case UserActions.AcceptFriendship:
                        FriendshipAccepted?.Invoke(msg.UserLogin, msg.Time);
                        OutcomeRequestsList?.Remove(msg.UserLogin);
                        if (OnlineFriendsList != null && !OnlineFriendsList.Contains(msg.UserLogin))
                        {
                            OnlineFriendsList.Add(msg.UserLogin);
                        }
                        break;

                    case UserActions.RejectFriendship:
                        FriendshipRejected?.Invoke(msg.UserLogin, msg.Time);
                        OutcomeRequestsList?.Remove(msg.UserLogin);
                        break;

                    case UserActions.SendFriendshipRequest:
                        NewFriendshipRequest?.Invoke(msg.UserLogin, msg.Time);
                        if (IncomeRequestsList != null && !IncomeRequestsList.Contains(msg.UserLogin))
                        {
                            IncomeRequestsList.Add(msg.UserLogin);
                        }
                        break;

                    case UserActions.CancelFriendshipRequest:
                        FriendshipRequestCancelled?.Invoke(msg.UserLogin, msg.Time);
                        IncomeRequestsList?.Remove(msg.UserLogin);
                        break;

                    case UserActions.RemoveFromFriends:
                        RemovedFromeFriends?.Invoke(msg.UserLogin, msg.Time);
                        OnlineFriendsList?.Remove(msg.UserLogin);
                        OfflineFriendsList?.Remove(msg.UserLogin);
                        break;

                    case UserActions.MessageSended:
                        MessageSended?.Invoke(msg.UserLogin, msg.Time);
                        break;

                    default:
                        break;
                    }
                }
                else if (message is NewReplyMessage)
                {
                    Conversations.AddReply(
                        ((NewReplyMessage)message).Interlocutor,
                        new ConversationReply
                    {
                        Author = ((NewReplyMessage)message).Author,
                        Time   = ((NewReplyMessage)message).Time,
                        Text   = ((NewReplyMessage)message).Text
                    }
                        );
                    NewReplyComes?.Invoke(
                        ((NewReplyMessage)message).Interlocutor,
                        new ConversationReply
                    {
                        Author = ((NewReplyMessage)message).Author,
                        Time   = ((NewReplyMessage)message).Time,
                        Text   = ((NewReplyMessage)message).Text
                    });
                }
                else if (message is OldReplyMessage)
                {
                    Conversations.InsertReplyToTop(
                        ((OldReplyMessage)message).Interlocutor,
                        new ConversationReply
                    {
                        Author = ((OldReplyMessage)message).Author,
                        Time   = ((OldReplyMessage)message).Time,
                        Text   = ((OldReplyMessage)message).Text
                    }
                        );
                    OldReplyComes?.Invoke(
                        ((OldReplyMessage)message).Interlocutor,
                        new ConversationReply
                    {
                        Author = ((OldReplyMessage)message).Author,
                        Time   = ((OldReplyMessage)message).Time,
                        Text   = ((OldReplyMessage)message).Text
                    });
                }
            }
        }