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; }
private void buttonCopySelection_Click(object sender, EventArgs e) { if (!processRunningInBackground()) { UserInRoomInfo c = listBox1.SelectedItem as UserInRoomInfo; if (c != null) { if (c.CanBeFollowed && c.SelectionCount > 0) { //send request _currentCopySelectionRequestID = Guid.NewGuid().ToString("N"); ChatMessage msg = new ChatMessage(); msg.Name = "reqsel"; msg.Parameters.Add("reqid", _currentCopySelectionRequestID); msg.Parameters.Add("clientid", c.ID); sendMessage(msg); _copySelectionRequestStarted = DateTime.Now; int secs = (int)_copySelectionRequestTimeout.TotalSeconds; this.toolStripStatusLabelRequestSelection.Text = string.Format("{0} {1}", Utils.LanguageSupport.Instance.GetTranslation(STR_REQUESTINGSELECTION), secs); this.toolStripStatusLabelRequestSelection.Visible = true; timerCopySelection.Enabled = true; } } } }
private void buttonCreateJoinRoom_Click(object sender, EventArgs e) { _room = textBoxRoomName.Text.Trim(); ChatMessage msg = new ChatMessage(); msg.Name = "room"; sendMessage(msg); labelActiveRoom.Text = _room; }
private void checkBoxCanFollow_CheckedChanged(object sender, EventArgs e) { ChatMessage msg = new ChatMessage(); msg.Name = "follow"; msg.Parameters.Add("canfollow", checkBoxCanFollow.Checked.ToString()); msg.Parameters.Add("cache", checkBoxCanFollow.Checked ? Core.ActiveGeocache == null ?"": Core.ActiveGeocache.Code : ""); msg.Parameters.Add("selected", checkBoxCanFollow.Checked ? (from Framework.Data.Geocache g in Core.Geocaches where g.Selected select g).Count().ToString() : ""); sendMessage(msg); }
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '\r') { string s = textBox1.Text.Trim(); if (s.Length > 0) { ChatMessage msg = new ChatMessage(); msg.Name = "txt"; msg.Parameters.Add("msg", s); msg.Parameters.Add("color", _txtColor.ToArgb().ToString()); if (sendMessage(msg)) { textBox1.Text = ""; } e.Handled = true; } } }
private bool sendMessage(ChatMessage msg) { bool result = false; msg.ID = _id; msg.Room = _room; try { if (_serverConnection != null) { _serverConnection.SendData(msg.ChatMessageData); result = true; } } catch { CloseConnection(); } return result; }
void _serverConnection_DataReceived(object sender, EventArgs e) { _context.Post(new SendOrPostCallback(delegate(object state) { try { if (_serverConnection != null) { byte[] data = _serverConnection.ReadData(); while (data != null) { //parse data ChatMessage msg = ChatMessage.Parse(data); if (msg != null) { //handle data if (msg.Name == "signinfailed") { CloseConnection(); break; } else if (msg.Name == "signinsuccess") { _retryCount = 0; panel1.Enabled = true; splitContainer1.Enabled = true; toolStripStatusLabelConnectionStatus.Text = Utils.LanguageSupport.Instance.GetTranslation(STR_CONNECTED); } else if (msg.Name == "txt") { AddMessage(msg.ID, msg.Parameters["msg"], Color.FromArgb(int.Parse(msg.Parameters["color"]))); if (checkBoxPlaySound.Checked && _sndWelcome != null) { _sndMessage.Play(); //System.Media.SystemSounds.Beep.Play(); } } else if (msg.Name == "usersinroom") { bool newUser = false; int i = 0; UserInRoomInfo[] curUsr = (from UserInRoomInfo a in listBox1.Items select a as UserInRoomInfo).ToArray(); foreach (var x in curUsr) { x.present = false; } bool wasempty = curUsr.Length == 0; while (msg.Parameters.ContainsKey(string.Format("name{0}", i))) { string id = msg.Parameters[string.Format("id{0}", i)]; UserInRoomInfo c = (from x in curUsr where x.ID == id select x).FirstOrDefault(); if (c == null) { c = new UserInRoomInfo(); c.ID = id; c.Username = msg.Parameters[string.Format("name{0}", i)]; c.present = true; c.FollowThisUser = false; c.SelectionCount = -1; if (msg.Parameters.ContainsKey(string.Format("sc{0}", i))) { string selCount = msg.Parameters[string.Format("sc{0}", i)]; if (!string.IsNullOrEmpty(selCount)) { c.SelectionCount = int.Parse(selCount); } } c.CanBeFollowed = bool.Parse(msg.Parameters[string.Format("cbf{0}", i)]); c.ActiveGeocache = msg.Parameters[string.Format("agc{0}", i)]; listBox1.Items.Add(c); if (!wasempty) { AddMessage("-1", string.Format("{0} {1}", c.Username, Utils.LanguageSupport.Instance.GetTranslation(STR_JOINED)), Color.Black); } newUser = true; } else { c.CanBeFollowed = bool.Parse(msg.Parameters[string.Format("cbf{0}", i)]); c.ActiveGeocache = msg.Parameters[string.Format("agc{0}", i)]; c.present = true; } i++; } foreach (var x in curUsr) { if (!x.present) { listBox1.Items.Remove(x); } } typeof(ListBox).InvokeMember("RefreshItems", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, listBox1, new object[] { }); listBox1_SelectedIndexChanged(this, EventArgs.Empty); if (checkBoxPlaySound.Checked && newUser && _sndWelcome != null) { _sndWelcome.Play(); //System.Media.SystemSounds.Beep.Play(); } } else if (msg.Name == "follow") { UserInRoomInfo[] curUsr = (from UserInRoomInfo a in listBox1.Items select a as UserInRoomInfo).ToArray(); UserInRoomInfo c = (from x in curUsr where x.ID == msg.ID select x).FirstOrDefault(); if (c != null) { c.CanBeFollowed = bool.Parse(msg.Parameters["canfollow"]); c.ActiveGeocache = msg.Parameters["cache"]; c.SelectionCount = -1; if (msg.Parameters.ContainsKey("selected")) { string selCount = msg.Parameters["selected"]; if (!string.IsNullOrEmpty(selCount)) { c.SelectionCount = int.Parse(selCount); } } if (c.FollowThisUser) { if (!c.CanBeFollowed) { c.FollowThisUser = false; linkLabelUnavailableCache.Visible = false; } else { if (!string.IsNullOrEmpty(c.ActiveGeocache)) { Framework.Data.Geocache gc = Utils.DataAccess.GetGeocache(Core.Geocaches, c.ActiveGeocache); if (gc != null) { Core.ActiveGeocache = gc; linkLabelUnavailableCache.Visible = false; } else if (c.ActiveGeocache.StartsWith("GC") && Core.GeocachingComAccount.MemberTypeId > 1) { //offer to download linkLabelUnavailableCache.Links.Clear(); linkLabelUnavailableCache.Text = string.Format("{0}: {1}", Utils.LanguageSupport.Instance.GetTranslation(STR_MISSING), c.ActiveGeocache); linkLabelUnavailableCache.Links.Add(linkLabelUnavailableCache.Text.IndexOf(':') + 2, c.ActiveGeocache.Length, c.ActiveGeocache); linkLabelUnavailableCache.Visible = true; } else { linkLabelUnavailableCache.Visible = false; } } else { linkLabelUnavailableCache.Visible = false; } } } } typeof(ListBox).InvokeMember("RefreshItems", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, listBox1, new object[] { }); listBox1_SelectedIndexChanged(this, EventArgs.Empty); } else if (msg.Name == "rooms") { RoomInfo[] curRms = (from RoomInfo a in listBox2.Items select a as RoomInfo).ToArray(); foreach (var x in curRms) { x.present = false; } int i = 0; while (msg.Parameters.ContainsKey(string.Format("name{0}", i))) { string name = msg.Parameters[string.Format("name{0}", i)]; RoomInfo c = (from x in curRms where x.Name == name select x).FirstOrDefault(); if (c == null) { c = new RoomInfo(); c.present = true; c.Name = name; listBox2.Items.Add(c); } else { c.present = true; } i++; } foreach (var x in curRms) { if (!x.present) { listBox2.Items.Remove(x); } } typeof(ListBox).InvokeMember("RefreshItems", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, listBox2, new object[] { }); } else if (msg.Name == "reqsel") { StringBuilder sb = new StringBuilder(); UserInRoomInfo[] curUsr = (from UserInRoomInfo a in listBox1.Items select a as UserInRoomInfo).ToArray(); UserInRoomInfo c = (from x in curUsr where x.ID == msg.ID select x).FirstOrDefault(); if (c != null) { AddMessage("-1", string.Format("{0} {1}", c.Username, Utils.LanguageSupport.Instance.GetTranslation(STR_REQUESTEDSELECTION)), Color.Black); var gsel = from Framework.Data.Geocache g in Core.Geocaches where g.Selected select g; foreach (var g in gsel) { sb.AppendFormat("{0},", g.Code); } } ChatMessage bmsg = new ChatMessage(); bmsg.Name = "reqselresp"; bmsg.Parameters.Add("reqid", msg.Parameters["reqid"]); bmsg.Parameters.Add("clientid", msg.ID); bmsg.Parameters.Add("selection", sb.ToString()); sendMessage(bmsg); } else if (msg.Name == "reqselresp") { if (msg.Parameters["reqid"] == _currentCopySelectionRequestID) { //handle response string[] gcCodes = msg.Parameters["selection"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); Core.Geocaches.BeginUpdate(); foreach (Framework.Data.Geocache g in Core.Geocaches) { g.Selected = gcCodes.Contains(g.Code); } Core.Geocaches.EndUpdate(); //are we missing geocaches? _importGeocaches.Clear(); if (Core.GeocachingComAccount.MemberTypeId > 1) { foreach (string s in gcCodes) { if (Utils.DataAccess.GetGeocache(Core.Geocaches, s) == null && s.StartsWith("GC")) { _importGeocaches.Add(s); } } if (_importGeocaches.Count > 0) { _frameworkUpdater = new Utils.FrameworkDataUpdater(Core); _getGeocacheThread = new Thread(new ThreadStart(getCopiedSelectionGeocacheInbackgroundMethod)); _getGeocacheThread.IsBackground = true; _getGeocacheThread.Start(); } } //reset request prop. timerCopySelection.Enabled = false; _currentCopySelectionRequestID = null; _copySelectionRequestStarted = DateTime.MinValue; toolStripStatusLabelRequestSelection.Visible = false; } } } data = _serverConnection.ReadData(); } } } catch { CloseConnection(); } }), null); }
public void UpdateView() { if (_serverConnection == null) { try { panel1.Enabled = false; splitContainer1.Enabled = false; toolStripStatusLabelConnectionStatus.Text = Utils.LanguageSupport.Instance.GetTranslation(STR_CONNECTING); TcpClient tcpClient = new TcpClient(); //then intialize _serverConnection = new ServerConnection(tcpClient); _serverConnection.ConnectionClosed += new EventHandler<EventArgs>(_serverConnection_ConnectionClosed); _serverConnection.DataReceived += new EventHandler<EventArgs>(_serverConnection_DataReceived); _serverConnection.Start(); ChatMessage msg = new ChatMessage(); msg.ID = _id; msg.Name = "signon"; msg.Room = _room; msg.Parameters.Add("username", Core.GeocachingComAccount.AccountName); msg.Parameters.Add("token", Core.GeocachingComAccount.APIToken); _serverConnection.SendData(msg.ChatMessageData); } catch { CloseConnection(); } } }