Esempio n. 1
0
        public byte[] GetBytes()
        {
            var w = new ByteArrayWriter()
                    .Append((int)Code)
                    .Append(Length);

            if (Body != null)
            {
                w.Append(Body);
            }
            return(w.Array);
        }
Esempio n. 2
0
        public void Process()
        {
            try
            {
                while (_flag.Retrieve((x) => x))
                {
                    var len = _clients.Retrieve(x => x.Count);

                    for (int i = 0; i < len; i++)
                    {
                        var user = _clients.Retrieve(x => x[i]);
                        if (!user.Connected)
                        {
                            Console.WriteLine("Disconnected: " + user.Address);
                            _clients.Act(x => x[i] = null);
                        }

                        var msg = user.Recv();
                        if (msg == null)
                        {
                            continue;
                        }

                        if (msg.Code == MessageCode.GET_ROOMS)
                        {
                            var writer = new ByteArrayWriter();
                            _rooms.Act(x =>
                            {
                                foreach (var room in x.Values)
                                {
                                    writer.Append(room.PublicName);
                                }
                            });
                            user.Send(new Message {
                                Code = MessageCode.GET_ROOMS, Body = writer.Array
                            });
                        }
                        else if (msg.Code == MessageCode.ADD_ROOM)
                        {
                            var reader  = msg.GetReader();
                            var name    = reader.ReadString();
                            var pass    = reader.ReadString();
                            var newRoom = new Room(name, pass);
                            _rooms.Act(x =>
                            {
                                if (!x.ContainsKey(name))
                                {
                                    x[newRoom.PublicName] = newRoom;
                                }
                            });
                            Console.WriteLine("User {0} added a room: {1}({2})", user.ConnectedUser.Name, newRoom.Name, newRoom.Password);
                        }
                        else if (msg.Code == MessageCode.JOIN_ROOM)
                        {
                            var reader = msg.GetReader();
                            var name   = reader.ReadString();
                            var pass   = reader.ReadString();
                            _rooms.Act(x =>
                            {
                                if (x.ContainsKey(name))
                                {
                                    var room = x[name];
                                    if (Equals(room.Password, pass))
                                    {
                                        user.ConnectedUser.Id = _iDGenerator.GetID();
                                        room.AddUser(user.ConnectedUser);

                                        user.Send(new Message
                                        {
                                            Code = MessageCode.JOIN_ROOM,
                                            Body = new ByteArrayWriter()
                                                   .Append(user.ConnectedUser.Id).Array
                                        });

                                        Console.WriteLine("User {0} joined room {1} => {2}", user.ConnectedUser.Name, room.Name, user.ConnectedUser.Id);
                                    }
                                }
                            });
                        }
                    }

                    _clients.Act(x => x.RemoveAll(y => y == null));

                    Protocol.Yield();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }