public void Write(Message req) { object obj = req; Write(obj); }
private void Update(Wrapper w, Message m) { w.user.Messages_sent++; w.user.name = m.sender.name; if (m.chatroom == null) { m.chatroom = w.chatroom; } }
/// <summary> /// This method will broadcast a message to all clients in the chatroom /// </summary> /// <param name="text"></param> public override void Send(string text) { Message m = new Message(this.user, text, wrapper.chatroom, true); Broadcast(m); }
private void Broadcast(Message m,bool mindChatrooms=false) { List<Wrapper> recipient; if (!mindChatrooms) recipient = clients.Where(x => x.chatroom.uid == m.chatroom.uid).ToList(); else recipient = clients; for (int i = 0; i < recipient.Count; i++) { recipient[i].communicator.Write(m,recipient[i]); } }
internal void Send(Message obj) { this.communicator.Write(obj,this); }
internal void Send(string text) { Message m = new Message(this.user, text, this.chatroom, true); this.communicator.Write(m,this); }
public void Broadcast(Message m) { BinaryFormatter formatter = new BinaryFormatter(); using(MemoryStream ms = new MemoryStream()) { byte[] buffer = new byte[MAX_BUFFER]; formatter.Serialize(ms, m); buffer = ms.ToArray(); for (int i = 0; i < Chatrooms[m.chatroom].Clients.Count; i++) { Chatrooms[m.chatroom].Clients[i].socket.Send(buffer, 0, buffer.Length, SocketFlags.None); } } }
public void Receive(object boxed_tuple) { Tuple<Connection, Chatroom> tuple = boxed_tuple as Tuple<Connection, Chatroom>; Socket s = tuple.Item1.socket; Color color = tuple.Item1.color; NetworkStream ns = new NetworkStream(s); BinaryFormatter formatter = new BinaryFormatter(); byte[] buffer = new byte[MAX_BUFFER]; while (true) { Array.Clear(buffer, 0, buffer.Length); int received_bytes = ns.Read(buffer, 0, buffer.Length); using (MemoryStream ms = new MemoryStream(buffer, 0, received_bytes)) { Message message; try { message = (Message)formatter.Deserialize(ms); if (message.flags.isInformation && message.message != string.Empty) { if (message.flags.doBroadcast) //Admin message { Message m = new Message(admin,message.chatroom,message.flags.isInformation,message.flags.doBroadcast); m.color = Color.Red; m.message = message.message; Broadcast(m); } else //Print just locally { stdout(message.message, message.color); } } else if (message.flags.doBroadcast || message.flags.isNormalChatMessage) { //inject color message.color = color; Broadcast(message); } else if (message.flags.isInformation && message.message == string.Empty) throw new Exception("Your flags are not properly set"); } catch(SerializationException e) { Console.WriteLine("Request error"); } } } //ns.Close(); }