Esempio n. 1
0
 /// <summary>
 /// Obsluga wydarzenia przyjscia wiadomosci
 /// </summary>
 /// <param name="e"></param>
 private void MsgCame(MsgEvent e)
 {
     if (msgSignal != null)
     {
         msgSignal(this, e);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Metoda do obslugi wiadomosci miedzy klientami
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MsgService(object sender, MsgEvent e)
        {
            StringBuilder sb      = new StringBuilder();
            Decoder       decoder = Encoding.UTF8.GetDecoder();

            char[] chars = new char[decoder.GetCharCount(e.msg, 0, e.msg.Length)];
            decoder.GetChars(e.msg, 0, e.msg.Length, chars, 0);
            sb.Append(chars);

            string message = sb.ToString();

            if (String.Compare("ONL", 0, message, 0, 3) == 0)
            {
                string result = null;
                foreach (Client c in database)
                {
                    if (c.isRunning && !c.isConnected)
                    {
                        result += c.login + " ";
                    }
                }
                byte[] msg;
                if (result == null)
                {
                    msg = Encoding.UTF8.GetBytes("ONLRNikt nie jest online");
                }
                else
                {
                    msg = Encoding.UTF8.GetBytes("ONLR" + result);
                }
                foreach (Client c in database)
                {
                    if (c.login == e.login)
                    {
                        c.Send(msg);
                    }
                }
                return;
            }
            else if (String.Compare("ISO", 0, message, 0, 3) == 0)
            {
                string login = message.Substring(3);
                bool   isOn  = false;
                string result;
                foreach (Client c in database)
                {
                    if (c.login == login && c.isRunning && !c.isConnected)
                    {
                        isOn = true;
                        break;
                    }
                }
                if (isOn)
                {
                    result = "ISORTrue";
                    foreach (Client c in database)
                    {
                        if (c.login == e.login)
                        {
                            c.connectedTo = login;
                            break;
                        }
                    }
                }
                else
                {
                    result = "ISORFalse";
                }

                byte[] msg = Encoding.UTF8.GetBytes(result);
                foreach (Client c in database)
                {
                    if (c.login == e.login)
                    {
                        c.Send(msg);
                    }
                }
                return;
            }
            else if (String.Compare("GCR", 0, message, 0, 3) == 0)
            {
                X509Certificate2 cerAlice = null;
                X509Certificate2 cerBob   = null;
                string           alice    = e.login;
                string           bob      = null;
                foreach (Client c in database)
                {
                    if (c.login == alice)
                    {
                        bob = c.connectedTo;
                        break;
                    }
                }
                foreach (Client c in database)
                {
                    if (c.login == bob)
                    {
                        c.connectedTo = alice;
                        break;
                    }
                }
                Console.WriteLine("ALICE is {0}", alice);
                Console.WriteLine("BOB is {0}", bob);
                //Pobranie odpowiednich certyfikatow
                foreach (Client c in database)
                {
                    if (c.login == alice)
                    {
                        cerAlice = c.certificate;
                    }
                    else if (c.login == bob)
                    {
                        cerBob = c.certificate;
                    }
                }
                //Przygotowanie wiadomości dla Boba
                //sb.Clear();
                string prefix = "GCRB";
                int    bytes  = Encoding.UTF8.GetByteCount(prefix);
                //sb.Append(prefix).Append(bytes);
                //prefix = sb.ToString();
                //bytes = Encoding.UTF8.GetByteCount(prefix);
                byte[] pre     = Encoding.UTF8.GetBytes(prefix);
                byte[] rawData = CryptoModule.PreparePublicCertToSend(cerAlice);
                byte[] bobMsg  = new byte[rawData.Length + bytes];
                pre.CopyTo(bobMsg, 0);
                rawData.CopyTo(bobMsg, bytes);

                //Przygotowanie wiadomości dla Alice
                //sb.Clear();
                prefix = "GCRA";
                bytes  = Encoding.UTF8.GetByteCount(prefix);
                //sb.Append(prefix).Append(bytes);
                //prefix = sb.ToString();
                //bytes = Encoding.UTF8.GetByteCount(prefix);
                byte[] preA     = Encoding.UTF8.GetBytes(prefix);
                byte[] rawDataA = CryptoModule.PreparePublicCertToSend(cerBob);
                byte[] aliceMsg = new byte[rawDataA.Length + bytes];
                preA.CopyTo(aliceMsg, 0);
                rawDataA.CopyTo(aliceMsg, bytes);

                //Wyslanie wiadomosci
                foreach (Client c in database)
                {
                    if (c.login == alice)
                    {
                        c.Send(aliceMsg);
                        c.isConnected = true;
                        break;
                    }
                }
                Console.WriteLine("BOB certificate send to ALICE");
                foreach (Client c in database)
                {
                    if (c.login == bob)
                    {
                        c.Send(bobMsg);
                        c.isConnected = true;
                        break;
                    }
                }
                Console.WriteLine("ALICE certificate send to BOB");
                return;
            }
            else if (String.Compare("DIS", 0, message, 0, 3) == 0)
            {
                Console.WriteLine("Logging {0} out", e.login);
                string con    = null;
                bool   conect = false;
                foreach (Client c in database)
                {
                    if (c.login == e.login)
                    {
                        if (c.isConnected)
                        {
                            con    = c.connectedTo;
                            conect = true;
                        }
                        c.Stop();
                        Console.WriteLine("Client {0} disconnected", c.login);
                        break;
                    }
                }
                if (conect)
                {
                    foreach (Client c in database)
                    {
                        if (c.login == con)
                        {
                            c.Send(Encoding.UTF8.GetBytes("DIC"));
                            c.isConnected = false;
                            c.connectedTo = null;
                            break;
                        }
                    }
                }
            }
            else if (String.Compare("DIC", 0, message, 0, 3) == 0)
            {
                string log = null;
                foreach (Client c in database)
                {
                    if (c.login == e.login)
                    {
                        log           = c.connectedTo;
                        c.isConnected = false;
                        c.connectedTo = null;
                        break;
                    }
                }
                foreach (Client c in database)
                {
                    if (c.login == log)
                    {
                        c.Send(e.msg);
                        c.isConnected = false;
                        c.connectedTo = null;
                        break;
                    }
                }
            }
            else if (String.Compare("MSG", 0, message, 0, 3) == 0)
            {
                Console.WriteLine("Message from {0}", e.login);
                string log = null;
                foreach (Client c in database)
                {
                    if (c.login == e.login)
                    {
                        log = c.connectedTo;
                        break;
                    }
                }
                foreach (Client c in database)
                {
                    if (c.login == log)
                    {
                        c.Send(e.msg);
                        break;
                    }
                }
                Console.WriteLine("Message sent to {0}", log);
            }
        }