예제 #1
0
        public static void Login(byte[] data, IPEndPoint ip, Server server)
        {
            Models.Client current = null;   //< Who is doing the request

            // Unpack
            LoginReqPacket logPacket = Packet.Unpack <LoginReqPacket>(data);

            server.DebugInfo("Login request recieved.");
            server.DebugInfo("LoginReq Packet: " + logPacket.ToString());


            if (ClientsManagement.LoginClient(logPacket, ref current, out string message))
            {
                if (server.GetClient(current.Username).Stat == ClientStatus.Status.Logged)
                {
                    server.DebugInfo("Login: User is already logged");
                    message = "USer is already logged";
                    server.Udp.SendError(message, ip);
                    return;
                }

                // Client login
                server.DebugInfo("Client " + current.ToString() + " is now logged in.");
                string alea = Server.GenerateAlea();
                server.SetStatus(logPacket.Username, ClientStatus.Status.Logged, alea, DateTime.Now);

                // Send the profile info of the database to client
                server.DebugInfo("Sending profile info to " + logPacket.Username + ".");

                // Return user profile with the ack
                server.Udp.SendMessage(new ProfilePacket(
                                           PacketTypes.LoginAck,
                                           alea,  // New alea generated
                                           current.FirstName,
                                           current.LastName,
                                           current.Age,
                                           current.PhoneNumber,
                                           current.Gender,
                                           current.Username,
                                           current.Password,
                                           current.Email,
                                           server.GetContacts(server.GetClient(current.Username)),
                                           server.GetUnreadMessages(server.GetClient(current.Username)),
                                           server.GetAgendaEvents(current.Username),
                                           server.GetSocialNetworks(current.Username)
                                           ).Pack(), ip);

                // Mark messages as read
                server.MarkReadMessages(server.GetClient(current.Username));
            }
            else
            {
                server.DebugInfo("Login: Request not accepted");
                server.Udp.SendError(message, ip);
            }
        }