static void AddMessages(string message, string user, NetworkStream netStream) { string responce = null; try { string time = DateTime.Now.ToShortTimeString(); Message msg = new Message(time, user, message); locker.EnterReadLock(); int mCnt = messages.Count; locker.ExitReadLock(); if (mCnt >= 1000) { locker.EnterWriteLock(); messages.RemoveRange(0, 100); locker.ExitWriteLock(); } locker.EnterWriteLock(); messages.Add(msg); locker.ExitWriteLock(); responce = "OK" + separator + "Сообщение добавлено"; } catch { responce = "ERR" + separator + "Не удалось добавить сообщение"; } finally { SendResponceToClient(responce, netStream); } }
private static void ChatReceived(Client client, Message message) { var text = new ChatMessage { Text = "Client " + client.EndPoint + ": " + ((ChatMessage)message).Text }; Console.WriteLine(text.Text); foreach (var other in server.clients) if (other != client) other.Send(text); }
public string verifyMessage(String msg, String user, String dialogue, DateTime connTime) { lock (usersOnline) { usersOnline[user] = connTime; } Regex regex1 = new Regex("з[^а-я]*а[^а-я]*м"); Regex regex2 = new Regex("д[^а-я]*а[^а-я]*н"); if (msg.Length > 100) { Console.WriteLine("The user's message wasn't accepted (>100 symbols)."); return "LONGMESSAGE"; } else if (regex1.IsMatch(msg) || regex2.IsMatch(msg)) { Console.WriteLine("The user's message wasn't accepted (banned word)."); return "BANNEDWORD"; } else { using (MessageContext db = new MessageContext()) { //if (db.Messages.Count() != 0) // messages.Add(messages.ElementAt(messages.Count - 1).Key + 1, dialogue + "|" + user + " " + connTime.ToShortTimeString() + " : " + msg); //else // messages.Add(0, dialogue + "|" + user + " " + connTime.ToShortTimeString() + " : " + msg); Message newMsg = new Message { loginSender = user, loginAddressee = dialogue, message = msg, time = connTime.ToShortTimeString() }; db.Messages.Add(newMsg); db.SaveChanges(); } Console.WriteLine("The user's message was accepted."); return "MESSAGEACCEPTED"; } }