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);
            }
        }
Example #2
0
        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);
            }
        }
Example #3
0
        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);
            }
        }
Example #4
0
        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);
            }
        }
Example #5
0
        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);
            }
        }