private void Send() { var guid = Guid.NewGuid(); lock (Global.OnLineUserList) { if (clbOnlineList.CheckedItems.Count > 0) { var toips = string.Empty; var selected = new string[clbOnlineList.CheckedItems.Count]; clbOnlineList.CheckedItems.CopyTo(selected, 0); var selectitem = from item in selected where item != "All/None" select item; if (selectitem.Count() > 0) { var message = tbSend.Text; var o = new List <string>(); o.Add(guid.ToString()); o.Add(message); for (var i = 0; i < selectitem.Count(); i++) { SendContent sendContent = new SendContent(); sendContent.Id = guid; sendContent.Mode = SendMode.send.ToString(); sendContent.From = _client.ClientIP.ToString(); sendContent.To = selectitem.ToArray()[i].ToString(); sendContent.Message = message; toips += selectitem.ToArray()[i].ToString() + ","; o.Add(sendContent.To); var clientto = from user in Global.OnLineUserList where user.IP == sendContent.To select user; foreach (var cli in clientto) { cli.SendList.Add(sendContent); } } toips = toips.TrimEnd(','); WriteMessage(DateTime.Now.ToString() + " To[" + toips + "]:" + message); _client.SendContent(BuildContent(guid, SendMode.send, _client.ClientIP.ToString(), string.Join(",", selectitem), tbSend.Text)); tbSend.Text = string.Empty; var parStart = new ParameterizedThreadStart(CHandle); var recieveThread = new Thread(parStart); recieveThread.IsBackground = true; recieveThread.Start(o); } } } }
private void btnSend_Click(object sender, EventArgs e) { if (tbSend.Text.Trim().Length == 0) { return; } lock (Global.OnLineUserList) { if (clbOnlineList.CheckedItems.Count > 0) { var toips = string.Empty; var selected = new string[clbOnlineList.CheckedItems.Count]; clbOnlineList.CheckedItems.CopyTo(selected, 0); var selectitem = from item in selected where item != "All/None" select item; if (selectitem.Count() > 0) { var cguid = Guid.NewGuid(); var message = tbSend.Text; var o = new List <string>(); o.Add(cguid.ToString()); o.Add(message); for (var i = 0; i < selectitem.Count(); i++) { SendContent sendContent = new SendContent(); sendContent.Id = cguid; sendContent.Mode = SendMode.send.ToString(); sendContent.From = "server"; sendContent.To = selectitem.ToArray()[i].ToString(); sendContent.Message = message; sendContent.Sent = false; toips += selectitem.ToArray()[i].ToString() + ","; o.Add(sendContent.To); var clientto = from user in Global.OnLineUserList where user.IP.ToString() == sendContent.To select user; foreach (var cli in clientto) { cli.SendList.Add(sendContent); } } toips = toips.TrimEnd(','); WriteMessage(DateTime.Now.ToString() + " To[" + toips + "]:" + message); tbSend.Text = string.Empty; var parStart = new ParameterizedThreadStart(CHandle); var recieveThread = new Thread(parStart); recieveThread.IsBackground = true; recieveThread.Start(o); } } } }
private void ReceiveCallback(IPAddress ip, string content) { try { var contents = content.Split(' '); var id = contents[0]; var mode = contents[1]; var fromip = contents[2]; var toip = contents[3]; var message = string.Empty; var messageindex = content.IndexOf(contents[3]) + contents[3].Length; if (messageindex < content.Length) { message = content.Substring(messageindex + 1); } lock (Global.OnLineUserList) { var client = from user in Global.OnLineUserList where user.IP == ip.ToString() select user; SendContent sendContent; var tos = toip.Split(','); foreach (var rto in tos) { switch ((SendMode)Enum.Parse(typeof(SendMode), mode)) { case SendMode.send: sendContent = new SendContent(); if (rto == "server") { WriteToAccess(fromip, toip, message); var appendmessage = ip.ToString() + ":"; appendmessage += message; WriteMessage(appendmessage); sendContent.Id = Guid.Parse(id); sendContent.Mode = SendMode.request.ToString(); sendContent.From = fromip; sendContent.To = rto; sendContent.Sent = false; foreach (var cli in client) { cli.SendList.Add(sendContent); } } else { var clientto = from user in Global.OnLineUserList where user.IP == rto select user; sendContent.Id = Guid.Parse(id); sendContent.Mode = SendMode.send.ToString(); sendContent.From = fromip; sendContent.To = rto; sendContent.Message = message; sendContent.Sent = false; foreach (var cli in clientto) { cli.SendList.Add(sendContent); } } break; case SendMode.request: sendContent = new SendContent(); foreach (var cli in client) { lock (cli.SendList) { var list = from row in cli.SendList where row.Id.Equals(Guid.Parse(id)) && row.To == ip.ToString() select row; foreach (var li in list) { message = li.Message; } cli.SendList.RemoveAll(delegate(SendContent cont) { return(cont.Id.Equals(Guid.Parse(id)) && cont.To == ip.ToString()); }); } } WriteToAccess(fromip, rto, message); if (fromip != "server") { var clientto = from user in Global.OnLineUserList where user.IP == fromip select user; sendContent.Id = Guid.Parse(id); sendContent.Mode = SendMode.request.ToString(); sendContent.From = fromip; sendContent.To = toip; foreach (var cli in clientto) { cli.SendList.Add(sendContent); } } break; case SendMode.getuser: sendContent = new SendContent(); var users = "server,"; foreach (var user in Global.OnLineUserList) { users += user.IP + ","; } users = users.TrimEnd(','); foreach (var cli in client) { sendContent.Id = Guid.Parse(id); sendContent.Mode = SendMode.getuser.ToString(); sendContent.From = fromip; sendContent.To = toip; sendContent.Message = users; cli.SendList.Add(sendContent); } break; } } } } catch (Exception ex) { WriteMessage(ex.Message); } }