Ejemplo n.º 1
0
        protected override void OnMessage(MessageEventArgs e)
        {
            if (e.Data == "Handshake to server")
            {
                Console.WriteLine(e.Data);
                Console.WriteLine(Context.UserEndPoint.Address.ToString());
                //Console.WriteLine(Context.UserEndPoint.Port.ToString());
                Send("Handshake to client");
                try
                {
                    Program.genericGUIForm.richTextBoxStatusUpdates.AppendText(Context.UserEndPoint.Address.ToString() + " is connected to you!" + Environment.NewLine);
                }
                catch (Exception f)
                {
                    if (f is InvalidOperationException)
                    {
                        Console.WriteLine("Threading error: " + f);
                    }
                    else
                    {
                        Console.WriteLine("Unexpected error: " + f);
                    }
                }
            }
            if (e.IsPing)
            {
                Console.WriteLine("You've just been pinged");
                Send("Connection live");
            }
            else
            {
                try
                {
                    Chain newChain = JsonConvert.DeserializeObject <Chain>(e.Data);
                    Console.WriteLine("Creating new chain from server");
                    if (newChain.CheckIntegrity() && newChain.ChainList.Count > Program.ProjectD.ChainList.Count)
                    {
                        List <Message> messagesToAdd = new List <Message>();
                        messagesToAdd.AddRange(newChain.MessageQueue);
                        messagesToAdd.AddRange(Program.ProjectD.MessageQueue);

                        newChain.MessageQueue = messagesToAdd;
                        Program.ProjectD      = newChain;
                    }

                    if (!Synchronized)
                    {
                        Send(JsonConvert.SerializeObject(Program.ProjectD));
                        Synchronized = true;
                    }
                }
                catch (Exception g)
                {
                    if (g is JsonReaderException)
                    {
                        if (!e.Data.IsNullOrEmpty())
                        {
                            Console.WriteLine(e.Data.ToString() + " received as data");
                        }
                    }
                    else
                    {
                        Console.WriteLine(g);
                    }
                }
            }
        }