コード例 #1
0
        /// <summary>
        /// Accept friendship request.
        /// </summary>
        /// <param name="user">user.</param>
        /// <param name="friendLogin">friend's login.</param>
        /// <exception cref="ConnectionInterruptedException"></exception>
        private void AcceptFriendshipRequest(OnlineUser user, string friendLogin)
        {
            int friendId = DBoperations.GetUserId(friendLogin);

            if (friendId == 0)
            {
                return;
            }

            if (DBoperations.SetFriendship(friendId, user.Id, true))
            {
                // send new data to user one
                SendIncomeFriendshipRequests(user);
                SendFriends(user);

                OnlineUser friend = GetOnlineUser(friendLogin);
                // send notification to user two if online
                if (friend != null)
                {
                    friend.Client.SendMessage(new UserActionMessage
                    {
                        UserLogin = user.Login,
                        Time      = DateTime.Now,
                        Action    = UserActions.AcceptFriendship
                    });
                }
                // write to db
                else
                {
                    DBoperations.AddNotification(friendId, user.Id, UserActions.AcceptFriendship, DateTime.Now);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Remove user from friends.
        /// </summary>
        /// <param name="user">user.</param>
        /// <param name="friendLogin">friend's login.</param>
        /// <exception cref="ConnectionInterruptedException"></exception>
        private void RemoveFriend(OnlineUser user, string friendLogin)
        {
            int friendId = DBoperations.GetUserId(friendLogin);

            if (friendId == 0)
            {
                return;
            }

            if (DBoperations.RemoveFriend(user.Id, friendId))
            {
                // send new data to user one
                SendFriends(user);

                OnlineUser friend = GetOnlineUser(friendLogin);
                // send notification to user two if online
                if (friend != null)
                {
                    friend.Client.SendMessage(new UserActionMessage
                    {
                        UserLogin = user.Login,
                        Time      = DateTime.Now,
                        Action    = UserActions.RemoveFromFriends
                    });
                }
                // write to db
                else
                {
                    DBoperations.AddNotification(friendId, user.Id, UserActions.RemoveFromFriends, DateTime.Now);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Send conversation to user.
        /// </summary>
        /// <param name="user">user.</param>
        /// <param name="interlocutor">interlocutor of user in conversation.</param>
        /// <exception cref="ConnectionInterruptedException"></exception>
        private void SendConversation(OnlineUser user, string interlocutor)
        {
            int interlocutorId = DBoperations.GetUserId(interlocutor);

            if (interlocutorId == 0)
            {
                return;
            }

            // get replies from db
            ConversationReply[] replies = DBoperations.GetConversation(user.Id, interlocutorId);

            if (replies != null)
            {
                foreach (var r in replies)
                {
                    user.Client.SendMessage(new OldReplyMessage
                    {
                        Interlocutor = interlocutor,
                        Author       = DBoperations.GetUserLogin(r.user_id),
                        Time         = r.time,
                        Text         = r.reply
                    });
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Handle new reply in conversation.
        /// </summary>
        /// <param name="user">user.</param>
        /// <param name="interlocutor">interlocutor.</param>
        /// <param name="text">text of reply.</param>
        /// <exception cref="ConnectionInterruptedException"></exception>
        private void HandleReply(OnlineUser user, string interlocutor, string text)
        {
            int interlocutorId = DBoperations.GetUserId(interlocutor);

            if (interlocutorId == 0)
            {
                return;
            }

            // time of reply
            DateTime time = DateTime.Now;

            if (DBoperations.AddNewReply(user.Id, interlocutorId, text, time))
            {
                // send reply to user one
                user.Client.SendMessage(new NewReplyMessage
                {
                    Interlocutor = interlocutor,
                    Author       = user.Login,
                    Time         = time,
                    Text         = text
                });

                // send reply to user two if online
                OnlineUser friend = GetOnlineUser(interlocutor);

                if (friend != null)
                {
                    friend.Client.SendMessage(new NewReplyMessage
                    {
                        Interlocutor = user.Login,
                        Author       = user.Login,
                        Time         = time,
                        Text         = text
                    });
                }
                // write notification to db
                else
                {
                    DBoperations.AddNotification(interlocutorId, user.Id, UserActions.MessageSended, DateTime.Now);
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Start listen to clients and process connected clients.
        /// </summary>
        public async void Start()
        {
#if DEBUG
            DBoperations.Register("13", "13");
            for (int i = 0; i < 10; i++)
            {
                string login = "******" + i.ToString();
                DBoperations.Register(login, login);
            }
            for (int i = 10; i < 20; i++)
            {
                string login = "******" + i.ToString();
                DBoperations.Register(login, login);
                DBoperations.SetFriendship(DBoperations.GetUserId(login), DBoperations.GetUserId("13"), false);
            }
            for (int i = 20; i < 30; i++)
            {
                string login = "******" + i.ToString();
                DBoperations.Register(login, login);
                DBoperations.SetFriendship(DBoperations.GetUserId("13"), DBoperations.GetUserId(login), false);
            }
            for (int i = 30; i < 40; i++)
            {
                string login = "******" + i.ToString();
                DBoperations.Register(login, login);
                DBoperations.SetFriendship(DBoperations.GetUserId("13"), DBoperations.GetUserId(login), true);
            }
#endif

            if (isStarted)
            {
                return;
            }
            if (certificate == null)
            {
                return;
            }

            mpListener = new MpListener(port, certificate);
            mpListener.Start();
            isStarted = true;

            log.Info("Server is now listening.");

            while (true)
            {
                try
                {
                    MpClient client = await mpListener.AcceptMpClientAsync();

                    Task t = HandleClient(client);
                    activeTasks.Add(t);

                    // remove completed tasks
                    activeTasks.RemoveAll(x => x.IsCompleted);
                }
                catch (ConnectionInterruptedException e)
                {
                    // connection with client broke
                    // nothing critical, just continue
                    log.Error("Connection with client broke.", e);
                    continue;
                }
                catch (SocketException e)
                {
                    log.Error("Socket exception. Trying to restart listening.", e);

                    mpListener.Stop();
                    mpListener = null;
                    mpListener = new MpListener(port, certificate);
                    mpListener.Start();

                    continue;
                }
                catch (ObjectDisposedException)
                {
                    // the way we stop server
                    break;
                }
            }

            // wait for finishing process clients
            Task.WaitAll(activeTasks.ToArray());

            log.Info("Server is now stopped listening.");
            isStarted = false;
        }