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); } }
public static void Alive(byte[] data, IPEndPoint ip, Server server) { string message = ""; BasicReqPacket aPacket = Packet.Unpack <BasicReqPacket>(data); server.DebugInfo("Alive inf recieved."); server.DebugInfo("AliveInf Packet: " + aPacket.ToString()); ClientStatus current = server.GetClient(aPacket.Username); if (ClientsManagement.CheckBasics(current, ClientStatus.Status.Disconnected, aPacket.Alea, out message)) { // Save the last alive current.LastAlive = DateTime.Now; // Send ack server.Udp.SendMessage(new AckErrorPacket(PacketTypes.AliveAck, "Alive correct").Pack(), ip); } else { server.DebugInfo("Alive: Incorrect alive from " + aPacket.Username); server.DebugInfo(aPacket.Username + " now is disconnected."); if (server.ClientsOnChat.Any(d => d.Key == current.Client.Username)) { server.ClientsOnChat.Remove(current.Client.Username); } // Disconnect the client current.Disconnect(); // Send error server.Udp.SendError(message, ip); } }
public static void DeleteLinkSocialNet(byte[] data, IPEndPoint ip, Server server) { try { Packets.LinkSocialNetworkPacket packet = Packets.Packet.Unpack <Packets.LinkSocialNetworkPacket>(data); server.DebugInfo("Social Network Link Delete Request: Request recieved"); server.DebugInfo("Packet DeleteLinkSocialNetworkReq: " + packet.ToString()); // Who is making the query ClientLogic.ClientStatus client = server.GetClient(packet.ClientUsername); // Check the basics of connection if (ClientLogic.ClientsManagement.CheckBasics(client, ClientLogic.ClientStatus.Status.Disconnected, packet.Alea, out string message)) { // Check if the link already exists using (var db = new Models.ServerDatabase()) { if (db.LinkedSocialNetworks .Any(l => l.Client.Username.Equals(packet.ClientUsername) && l.SocialNetwork.Name.Equals(packet.SocialNetName))) { // If exist delete db.LinkedSocialNetworks.DeleteOnSubmit( db.LinkedSocialNetworks .Single(l => l.Client.Username.Equals(packet.ClientUsername) && l.SocialNetwork.Name.Equals(packet.SocialNetName))); server.DebugInfo("Delete Social Network Link: The link has been deleted."); // Submit changes db.SubmitChanges(); // Send the ack server.Udp.SendMessage(new Packets.AckErrorPacket(Packets.PacketTypes.DeleteLinkSocialNetAck, "Social net deleted correctly.").Pack(), ip); return; } else { server.DebugInfo("Delete Social Network Link: Social net doesn't exist, can't be deleted."); // Send the ack server.Udp.SendMessage(new Packets.AckErrorPacket(Packets.PacketTypes.Error, "Social net doesn't exist.").Pack(), ip); } } } else { server.DebugInfo("Delete Social Network Link: " + message); server.Udp.SendError(message, ip); } } catch (SqlException) { server.DebugInfo("Delete Social Network Link: Database error."); server.Udp.SendError("Database error. Probably no social net found on database.", ip); return; } }
public static void Logout(byte[] data, IPEndPoint ip, Server server) { string message = ""; BasicReqPacket logoutPacket = Packet.Unpack <BasicReqPacket>(data); server.DebugInfo("Logut request recieved."); server.DebugInfo("LogoutReq Packet: " + logoutPacket.ToString()); ClientStatus current = server.GetClient(logoutPacket.Username); if (ClientsManagement.CheckBasics(current, ClientStatus.Status.Disconnected, logoutPacket.Alea, out message)) { server.DebugInfo("Logout: Correct logut from " + logoutPacket.Username); server.DebugInfo(logoutPacket.Username + " now is disconnected."); if (server.ClientsOnChat.Any(d => d.Key == current.Client.Username)) { server.ClientsOnChat.Remove(current.Client.Username); } current.Disconnect(); // Return ack server.Udp.SendMessage(new AckErrorPacket(PacketTypes.LogoutAck, "Logged out correctly.").Pack(), ip); } else { server.DebugInfo("Logout: Incorrect logut."); // Send error message = "Logout error: " + message; server.Udp.SendError(message, ip); } }
public static void DeleteAccount(byte[] data, IPEndPoint ip, Server server) { string message = ""; BasicReqPacket delPacket = Packet.Unpack <BasicReqPacket>(data); server.DebugInfo("Delete account request recieved."); server.DebugInfo("DeleteAccountReq Packet: " + delPacket.ToString()); ClientStatus current = server.GetClient(delPacket.Username); if (ClientsManagement.CheckBasics(current, ClientStatus.Status.Disconnected, delPacket.Alea, out message)) { server.DebugInfo("Delete Account: Account was " + delPacket.Username + " correctly deleted."); // Send the ack first server.Udp.SendMessage(new AckErrorPacket(PacketTypes.DeleteAccountAck, "Account deleted correctly.").Pack(), ip); // Delete client from database server.DeleteCLientFromDataBase(delPacket.Username); } else { server.DebugInfo("Delete account: Acount can't be deleted."); // Send error message = "Delete error: " + message; server.Udp.SendError(message, ip); } }
public static void SendContactRequests(byte[] data, Server server, IPEndPoint ip) { string message = ""; // Unpack the petition BasicReqPacket cPacket = Packet.Unpack <BasicReqPacket>(data); List <Models.ContactRequest> sent = null; List <Models.ContactRequest> recieved = null; server.DebugInfo("Contact requests list requested by " + cPacket.Username); server.DebugInfo(cPacket.ToString()); if (ClientsManagement.ContactRequestsList(cPacket, server.GetClient(cPacket.Username), ref sent, ref recieved, out message)) { // List filled correctly server.Udp.SendMessage(new ContactReqListPacket(PacketTypes.ContactAck, cPacket.Alea, sent, recieved).Pack(), ip); server.DebugInfo("Contact list requests sended correctly to + " + cPacket.Username); } else { server.DebugInfo("Contact requests list error: " + message); server.Udp.SendError(message, ip); } }
public static void ClientsQuery(byte[] data, Server server, IPEndPoint ip) { string message = ""; // Unpack the petition Packets.ClientQueryPacket cPacket = Packet.Unpack <ClientQueryPacket>(data); List <Models.Client> queryResult = null; server.DebugInfo("Client query list requested by " + cPacket.Username); server.DebugInfo(cPacket.ToString()); if (ClientsManagement.GetClientsQueryResult(cPacket, server.GetClient(cPacket.Username), ref queryResult, cPacket.Query, out message)) { // List filled correctly server.Udp.SendMessage(new ClientQueryPacket(PacketTypes.ClientsQueryAck, cPacket.Alea, queryResult).Pack(), ip); server.DebugInfo("Client list query requests sended correctly to + " + cPacket.Username); } else { server.DebugInfo("Client list query requests error: " + message); server.Udp.SendError(message, ip); } }
public static void AnswerContactRequest(byte[] data, Server server, IPEndPoint ip, bool ack) { string message = ""; // Unpack the petition ContactReqPacket cPacket = Packet.Unpack <ContactReqPacket>(data); ClientStatus current = server.GetClient(cPacket.To); if (ClientsManagement.AckOrRegContactReq(cPacket, current, ack, out message)) { server.DebugInfo("Answer Contact request: " + (ack ? "Request accepted correctly." : "Request refused correctly.")); if (ack) { server.DebugInfo("New Contact added: " + cPacket.From + " - " + cPacket.To); } // Send the ack server.Udp.SendMessage(new AckErrorPacket(PacketTypes.ContactAck, "Answered correctly").Pack(), ip); } else { server.DebugInfo("Answer Contact request: Incorrect contact request."); // Send error server.Udp.SendError(message, ip); } }
public static void ReadyChatClient(byte[] data, TcpClient client, Server server) { string message = ""; // Unpack the data BasicReqPacket packet = Packet.Unpack <BasicReqPacket>(data); // Get the client who is requesting ClientStatus c = server.GetClient(packet.Username); // Check the basics if (ClientsManagement.CheckBasics(c, ClientStatus.Status.Disconnected, packet.Alea, out message)) { // Add client to dicctionary with his respective tcpclient server.ClientsOnChat.Add(c.Client.Username, client); server.DebugInfo("Client Ready for chat: " + c.Client.Username + " is ready for chatting."); // Send ack server.Tcp.SendMessage(new AckErrorPacket(PacketTypes.ReadyChatAck, "Now you are ready for chat").Pack(), client); } else { server.DebugInfo("Client Ready for chat error: " + message); server.Tcp.SendError(message, client); } }
public static void ProfileUpdate(byte[] data, IPEndPoint ip, Server server) { ProfilePacket pPacket = Packet.Unpack <ProfilePacket>(data); server.DebugInfo("Update profile recieved."); server.DebugInfo("Update profile Packet: " + pPacket.ToString()); ClientStatus current = server.GetClient(pPacket.Username); if (ClientsManagement.UpdateProfile(pPacket, ref current, out string message)) { // Profile updated server.DebugInfo("Update Profile: " + pPacket.Username + "'s profile updated."); // Return user profile with the ack server.Udp.SendMessage(new ProfilePacket( PacketTypes.ProfileUpdateAck, current.Alea, // New alea generated current.Client.FirstName, current.Client.LastName, current.Client.Age, current.Client.PhoneNumber, current.Client.Gender, current.Client.Username, current.Client.Password, current.Client.Email, server.GetContacts(current), server.GetUnreadMessages(current), server.GetAgendaEvents(current.Client.Username), server.GetSocialNetworks(current.Client.Username) ).Pack(), ip); server.MarkReadMessages(current); } else { server.DebugInfo("Profile Update: Request not accepted"); server.Udp.SendError(message, ip); } }
public static void NewContactRequest(byte[] data, Server server, IPEndPoint ip) { string message = ""; // Unpack the petition ContactReqPacket cPacket = Packet.Unpack <ContactReqPacket>(data); ClientStatus current = server.GetClient(cPacket.From); if (ClientsManagement.NewContactRequest(cPacket, current, out message)) { server.DebugInfo(cPacket.From + " made a contact request to " + cPacket.To); // Send the ack server.Udp.SendMessage(new AckErrorPacket(PacketTypes.ContactAck, "New contact request added to database").Pack(), ip); } else { server.DebugInfo("New Contact Request: Incorrect contact request."); // Send error server.Udp.SendError(message, ip); } }
public static void AgendaEvent(byte[] data, Server server, bool delete, IPEndPoint ip) { Console.WriteLine(Encoding.ASCII.GetString(data)); try { string message = ""; // Unpack the data Packets.AgendaEventPacket packet = Packets.Packet.Unpack <Packets.AgendaEventPacket>(data); server.DebugInfo("Agenda Event Req: " + packet.ToString()); // Get the client who is making the request ClientLogic.ClientStatus current = server.GetClient(packet.Username); // Check the basics if (ClientLogic.ClientsManagement.CheckBasics(current, ClientLogic.ClientStatus.Status.Disconnected, packet.Alea, out message)) { if (!delete) { AddEvent(packet, server, ip); } else { DeleteEvent(packet, server, ip); } } else { server.DebugInfo("Adding agenda event error: " + message); server.Udp.SendError(message, ip); } } catch (SqlException) { server.DebugInfo("Add new agenda event database error"); server.Udp.SendError("Unexpected database error", ip); } }
public static void LinkSocialNet(byte[] data, IPEndPoint ip, Server server) { try { Packets.LinkSocialNetworkPacket packet = Packets.Packet.Unpack <Packets.LinkSocialNetworkPacket>(data); server.DebugInfo("Social Network Link: Request recieved"); server.DebugInfo("Packet LinkSocialNetworkReq: " + packet.ToString()); // Who is making the query ClientLogic.ClientStatus client = server.GetClient(packet.ClientUsername); // Check the basics of connection if (ClientLogic.ClientsManagement.CheckBasics(client, ClientLogic.ClientStatus.Status.Disconnected, packet.Alea, out string message)) { // Check if the link already exists using (var db = new Models.ServerDatabase()) { if (db.LinkedSocialNetworks .Any(l => l.Client.Username.Equals(packet.ClientUsername) && l.SocialNetwork.Name.Equals(packet.SocialNetName))) { // Link exists on database -> Override it var sn = db.LinkedSocialNetworks.Single(l => l.Client.Username.Equals(packet.ClientUsername) && l.SocialNetwork.Name.Equals(packet.SocialNetName)); sn.Username = packet.SocialNetUsername; sn.Password = packet.SocialNetPassword; server.DebugInfo("Social Network Link: The link already exists, overriding it."); return; } else { server.DebugInfo("Social Network Link: Social net linked correctly."); db.LinkedSocialNetworks.InsertOnSubmit(new Models.LinkedSocialNetwork() { Client = db.Clients.Single(c => c.Username.Equals(packet.ClientUsername)), SocialNetwork = db.SocialNetworks.Single(c => c.Name.Equals(packet.SocialNetName)), Username = packet.SocialNetUsername, Password = packet.SocialNetPassword }); } // Submit changes db.SubmitChanges(); // Send the ack server.Udp.SendMessage(new Packets.AckErrorPacket(Packets.PacketTypes.LinkSocialNetworkAck, "Social net linked correctly.").Pack(), ip); } } else { server.DebugInfo("Social Network Link: " + message); server.Udp.SendError(message, ip); } } catch (SqlException e) { server.DebugInfo("Social Network Link: Database error."); server.Udp.SendError("Database error. Probably no social net found on database.", ip); return; } }
public static void SendMessage(byte[] data, TcpClient client, Server server) { try { string message = ""; // Unpack the data MessagePacket packet = Packet.Unpack <MessagePacket>(data); server.DebugInfo("SendMessageReq Recieved: " + packet.ToString()); // Who is making the query ClientStatus c = server.GetClient(packet.From.Username); // Check te basics again and again if (ClientsManagement.CheckBasics(c, ClientStatus.Status.Disconnected, packet.Alea, out message)) { // Check if destination exists if (!server.Clients.Any(d => d.Client.Username == packet.To.Username)) { server.DebugInfo("Send Message Error: " + "Destination doesn't exist"); server.Tcp.SendError("Destination doesn't exist", client); return; } // Store the message on database using (var db = new Models.ServerDatabase()) { // Create the message Models.Message m = new Models.Message() { From = db.Clients.Single(n => n.Username == c.Client.Username), To = db.Clients.Single(n => n.Username == packet.To.Username), Date = packet.Date, Content = packet.Content }; // Check if destination is on chat if (server.ClientsOnChat.Any(d => d.Key.Equals(packet.To.Username))) { // If exist save the message and send him as well as readed message m.Read = true; server.DebugInfo("Message from : " + packet.From.Username + " sended to: " + packet.To.Username); // Send the message to destination changing the type of package packet.Type = (byte)PacketTypes.SendMessageAck; server.Tcp.SendMessage(packet.Pack(), server.ClientsOnChat[packet.To.Username]); } else { m.Read = false; } db.Messages.InsertOnSubmit(m); db.SubmitChanges(); } } else { server.DebugInfo("Send Message Error: " + message); server.Tcp.SendError(message, client); } } catch (SqlException) { server.DebugInfo("Send Message Error: database error."); server.Tcp.SendError("Database error", client); } }