Ejemplo n.º 1
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);
            }
        }