示例#1
0
文件: cClient.cs 项目: kper/chat
 public void Write(Message req)
 {
     object obj = req;
     Write(obj);
 }
示例#2
0
文件: Server.cs 项目: kper/chat
        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;
            }
        }
示例#3
0
文件: Server.cs 项目: kper/chat
 /// <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);
 }
示例#4
0
文件: Server.cs 项目: kper/chat
        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]);
            }
        }
示例#5
0
文件: Wrapper.cs 项目: kper/chat
 internal void Send(Message obj)
 {
     this.communicator.Write(obj,this);
 }
示例#6
0
文件: Wrapper.cs 项目: kper/chat
 internal void Send(string text)
 {
     Message m = new Message(this.user, text, this.chatroom, true);
     this.communicator.Write(m,this);
 }
示例#7
0
文件: cServer.cs 项目: kper/chat
        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);
                }
            }
        }
示例#8
0
文件: cServer.cs 项目: kper/chat
        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();
        }