/* The GetFriendList MessageHandler * It handles messages of GET_ALL_FRIENDSHIPS_REQUEST type. */ private static void GetAllFriendships(RMessage message, TcpClient connection) { Console.WriteLine("GetAllFriendships"); List <FriendshipData> friendlist = FriendshipConnector.GetFriendshipList(ServerCore.GetIdByConnection(connection)); RMessage replyMessage = new RMessage(MessageType.GET_ALL_FRIENDSHIPS_REPLY, friendlist); ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage); }
/* The GetFriendList MessageHandler * It handles messages of ADD_FRIEND_REQ type. */ private static void AddFriend(RMessage message, TcpClient connection) { Console.WriteLine("AddFriend"); uint thisUserId = ServerCore.GetIdByConnection(connection); UserData friend = UserConnector.GetUser((String)message.Data); UserData thisUser = UserConnector.GetUser(thisUserId); if (friend == null) { return; } if (friend.Id == thisUserId) { return; } List <FriendshipData> friendships = FriendshipConnector.GetFriendshipList(thisUserId); foreach (FriendshipData friendshipIt in friendships) { if (friendshipIt.Former == friend.Id || friendshipIt.Latter == friend.Id) { return; } } FriendshipData friendship = new FriendshipData(thisUserId, friend.Id, false); FriendshipConnector.AddFriendship(friendship); ClientWorker formerWorker = ServerCore.GetWorkerById(friendship.Former); ClientWorker latterWorker = ServerCore.GetWorkerById(friendship.Latter); RMessage replyMessageFormer = new RMessage(MessageType.ADD_FRIEND_REPLY, friend); RMessage replyMessageLatter = new RMessage(MessageType.ADD_FRIEND_REPLY, thisUser); RMessage replyMessage = new RMessage(MessageType.ADD_FRIENDSHIP_REPLY, friendship); if (formerWorker != null) { formerWorker.SendMessage(replyMessageFormer); formerWorker.SendMessage(replyMessage); } if (latterWorker != null) { latterWorker.SendMessage(replyMessageLatter); latterWorker.SendMessage(replyMessage); } }