Ejemplo n.º 1
0
 void cc_AuthentificationFinished(object sender, EventArgs e)
 {
     _context.Post(new SendOrPostCallback(delegate(object state)
     {
         ClientConnection cc = sender as ClientConnection;
         if (_clientConnections.Contains(cc))
         {
             ChatMessage msg = new ChatMessage();
             msg.ID = cc.ID;
             if (cc.GeocachingComUserProfile == null)
             {
                 msg.Name = "signinfailed";
             }
             else
             {
                 cc.Username = cc.GeocachingComUserProfile.User.UserName;
                 msg.Name = "signinsuccess";
             }
             cc.SendData(msg.ChatMessageData);
             if (cc.GeocachingComUserProfile != null)
             {
                 if (availableRoomsChanged())
                 {
                     updateAvailableRooms(null);
                 }
                 else
                 {
                     updateAvailableRooms(cc);
                 }
                 updateActiveClientsForRoom(cc.Room);
             }
         }
     }), null);            
 }
Ejemplo n.º 2
0
        public static ChatMessage Parse(byte[] data)
        {
            ChatMessage result = null;
            if (data != null && data.Length > 0)
            {
                try
                {
                    string xmlFileContents;
                    using (MemoryStream ms = new MemoryStream(data))
                    using (StreamReader textStreamReader = new StreamReader(ms))
                    {
                        xmlFileContents = textStreamReader.ReadToEnd();
                    }

                    result = new ChatMessage();
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(xmlFileContents);
                    XmlElement root = doc.DocumentElement;
                    result.ID = root.Attributes["id"].InnerText;
                    result.Room = root.Attributes["room"].InnerText;
                    result.Name = root.Attributes["name"].InnerText;
 
                    XmlNodeList strngs = root.SelectNodes("parameter");
                    if (strngs != null)
                    {
                        foreach (XmlNode sn in strngs)
                        {
                            result.Parameters.Add(sn.Attributes["name"].InnerText, sn.Attributes["value"].InnerText);
                        }
                    }
                }
                catch
                {
                    result = null;
                }
            }
            return result;
        }
Ejemplo n.º 3
0
        void cc_DataReceived(object sender, EventArgs e)
        {
            _context.Post(new SendOrPostCallback(delegate(object state)
            {
                ClientConnection cc = sender as ClientConnection;
                if (_clientConnections.Contains(cc))
                {
                    try
                    {
                        byte[] data = cc.ReadData();
                        while (data != null && !cc.IsClosed)
                        {
                            //parse data
                            ChatMessage msg = ChatMessage.Parse(data);
                            if (msg != null)
                            {
                                //handle data
                                if (msg.Name == "signon" && !cc.AuthentificationChecked)
                                {
                                    //if there is a client with the same ID, then it is a reconnect
                                    var ccid = (from a in _clientConnections where a.ID == msg.ID select a).FirstOrDefault();
                                    if (ccid != null)
                                    {
                                        ccid.Close();
                                        _clientConnections.Remove(ccid);
                                    }

                                    cc.Room = msg.Room;
                                    cc.ID = msg.ID;
                                    cc.Token = msg.Parameters["token"];
                                    cc.Authenticate();
                                }
                                else if (cc.AuthentificationChecked && !string.IsNullOrEmpty(cc.Username))
                                {
                                    //general chat message, broadcast it
                                    if (msg.Name == "txt")
                                    {
                                        ClientConnection[] clients = GetClientsInRoom(cc.Room);
                                        ChatMessage bmsg = new ChatMessage();
                                        bmsg.ID = cc.ID;
                                        bmsg.Room = cc.Room;
                                        bmsg.Name = "txt";
                                        bmsg.Parameters.Add("msg", msg.Parameters["msg"]);
                                        bmsg.Parameters.Add("color", msg.Parameters["color"]);

                                        sendMessageToClients(clients, bmsg);
                                    }
                                    else if (msg.Name == "follow")
                                    {
                                        cc.ActiveGeocache = msg.Parameters["cache"];
                                        cc.CanBeFollowed = bool.Parse(msg.Parameters["canfollow"]);

                                        ClientConnection[] clients = GetClientsInRoom(cc.Room);
                                        ChatMessage bmsg = new ChatMessage();
                                        bmsg.ID = cc.ID;
                                        bmsg.Room = cc.Room;
                                        bmsg.Name = "follow";
                                        bmsg.Parameters.Add("canfollow", msg.Parameters["canfollow"]);
                                        bmsg.Parameters.Add("cache", msg.Parameters["cache"]);
                                        if (msg.Parameters.ContainsKey("selected"))
                                        {
                                            string s = msg.Parameters["selected"];
                                            if (string.IsNullOrEmpty(s))
                                            {
                                                s = "";
                                                cc.SelectionCount = -1;
                                            }
                                            else
                                            {
                                                int cnt;
                                                if (int.TryParse(s, out cnt))
                                                {
                                                    cc.SelectionCount = cnt;
                                                }
                                                else
                                                {
                                                    s = "";
                                                    cc.SelectionCount = -1;
                                                }
                                            }
                                            bmsg.Parameters.Add("selected", s);
                                        }
                                        else
                                        {
                                            cc.SelectionCount = -1;
                                            bmsg.Parameters.Add("selected", "");
                                        }
                                        sendMessageToClients(clients, bmsg);
                                    }
                                    else if (msg.Name == "room")
                                    {
                                        if (cc.Room!=msg.Room)
                                        {
                                            //leave old
                                            string prevRoom = cc.Room;
                                            cc.Room = msg.Room;
                                            updateActiveClientsForRoom(prevRoom);

                                            if (availableRoomsChanged())
                                            {
                                                updateAvailableRooms(null);
                                            }

                                            //enter new
                                            updateActiveClientsForRoom(cc.Room);
                                        }
                                    }
                                    else if (msg.Name == "byebye")
                                    {
                                        cc.Close();
                                        break;
                                    }
                                    else if (msg.Name == "reqsel")
                                    {
                                        ClientConnection[] clients = GetClientsInRoom(cc.Room);
                                        ClientConnection destClient = (from c in clients where c.ID == msg.Parameters["clientid"] select c).FirstOrDefault();
                                        if (destClient != null)
                                        {
                                            ChatMessage bmsg = new ChatMessage();
                                            bmsg.ID = cc.ID;
                                            bmsg.Room = cc.Room;
                                            bmsg.Name = "reqsel";
                                            bmsg.Parameters.Add("reqid", msg.Parameters["reqid"]);
                                            bmsg.Parameters.Add("clientid", msg.Parameters["clientid"]);

                                            sendMessageToClients(new ClientConnection[] { destClient }, bmsg);
                                        }
                                        else
                                        {
                                            ChatMessage bmsg = new ChatMessage();
                                            bmsg.ID = cc.ID;
                                            bmsg.Room = cc.Room;
                                            bmsg.Name = "reqselresp";
                                            bmsg.Parameters.Add("reqid", msg.Parameters["reqid"]);
                                            bmsg.Parameters.Add("clientid", msg.Parameters["clientid"]);
                                            bmsg.Parameters.Add("selection", "");

                                            sendMessageToClients(new ClientConnection[] { cc }, bmsg);
                                        }
                                    }
                                    else if (msg.Name == "reqselresp")
                                    {
                                        ClientConnection[] clients = GetClientsInRoom(cc.Room);
                                        ClientConnection destClient = (from c in clients where c.ID == msg.Parameters["clientid"] select c).FirstOrDefault();
                                        if (destClient != null)
                                        {
                                            ChatMessage bmsg = new ChatMessage();
                                            bmsg.ID = cc.ID;
                                            bmsg.Room = cc.Room;
                                            bmsg.Name = "reqselresp";
                                            bmsg.Parameters.Add("reqid", msg.Parameters["reqid"]);
                                            bmsg.Parameters.Add("clientid", msg.Parameters["clientid"]);
                                            bmsg.Parameters.Add("selection", msg.Parameters["selection"]);

                                            sendMessageToClients(new ClientConnection[] { destClient }, bmsg);
                                        }
                                    }
                                }
                            }
                            data = cc.ReadData();
                        }
                    }
                    catch
                    {
                        cc.Close();
                    }
                }
            }), null);            
        }
Ejemplo n.º 4
0
 private void updateActiveClientsForRoom(string room)
 {
     ClientConnection[] clients = GetClientsInRoom(room);
     ChatMessage msg = new ChatMessage();
     msg.ID = "";
     msg.Room = room;
     msg.Name = "usersinroom";
     for (int i = 0; i < clients.Length; i++)
     {
         msg.Parameters.Add(string.Format("name{0}", i), clients[i].Username);
         msg.Parameters.Add(string.Format("id{0}", i), clients[i].ID);
         msg.Parameters.Add(string.Format("cbf{0}", i), clients[i].CanBeFollowed.ToString());
         msg.Parameters.Add(string.Format("agc{0}", i), clients[i].ActiveGeocache);
         msg.Parameters.Add(string.Format("sc{0}", i), clients[i].SelectionCount >= 0 ? clients[i].SelectionCount.ToString() : "");
     }
     sendMessageToClients(clients, msg);
 }
Ejemplo n.º 5
0
 private void updateAvailableRooms(ClientConnection cc)
 {
     ClientConnection[] clients;
     if (cc == null)
     {
         clients = (from a in _clientConnections where a.AuthentificationChecked && !a.IsClosed && !string.IsNullOrEmpty(a.Username) select a).ToArray();
     }
     else
     {
         clients = new ClientConnection[] { cc };
     }
     ChatMessage msg = new ChatMessage();
     msg.ID = "";
     msg.Room = "";
     msg.Name = "rooms";
     msg.Parameters.Add(string.Format("name{0}", 0), "Lobby");
     for (int i = 0; i < _availableRooms.Count; i++)
     {
         msg.Parameters.Add(string.Format("name{0}", i+1), _availableRooms[i]);
     }
     sendMessageToClients(clients, msg);
 }
Ejemplo n.º 6
0
 private void sendMessageToClients(ClientConnection[] clients, ChatMessage msg)
 {
     byte[] data = msg.ChatMessageData;
     foreach (var a in clients)
     {
         byte[] b = new byte[data.Length];
         data.CopyTo(b, 0);
         a.SendData(b);
     }
 }