Пример #1
0
        public void SendWhisper(string from, string to, string text)
        {
            Session sender    = null;
            Session recipient = null;

            if (users.TryGetValue(from, out sender))
            {
                if (from != to)
                {
                    if (users.TryGetValue(to, out recipient))
                    {
                        pw = new ProtocolWriter();
                        pw.Write(8);
                        pw.Write(1);
                        pw.Write(1);
                        pw.Write(from);
                        pw.Write(to);
                        pw.Write(text);

                        sender.SendMessage(pw);

                        pw = new ProtocolWriter();
                        pw.Write(8);
                        pw.Write(1);
                        pw.Write(0);
                        pw.Write(from);
                        pw.Write(text);

                        recipient.SendMessage(pw);
                    }
                    else
                    {
                        pw = new ProtocolWriter();
                        pw.Write(8);
                        pw.Write(3);
                        pw.Write("User : "******" is offline.");

                        sender.SendMessage(pw);
                    }
                }
                else
                {
                    pw = new ProtocolWriter();
                    pw.Write(8);
                    pw.Write(3);
                    pw.Write("You can not whisper to yourself.");

                    sender.SendMessage(pw);
                }
            }
            else
            {
                pw = new ProtocolWriter();
                pw.Write(8);
                pw.Write(3);
                pw.Write("To write a message you must join the room.");

                sender.SendMessage(pw);
            }
        }
Пример #2
0
        public void Join(User user)
        {
            string key = user.Name;

            Session temp = Server.GetInstance().GetSessions().Find(x => x.User.Id == user.Id);

            if (!users.ContainsKey(key))
            {
                if (users.Count >= maxUsers)
                {
                    pw = new ProtocolWriter();
                    pw.Write(8);
                    pw.Write(3);
                    pw.Write("There are too many users in this room.");

                    temp.SendMessage(pw);
                }
                else
                {
                    user.SetLevel(LevelType.Room);
                    users.Add(user.Name, temp);
                }
            }
            else
            {
                pw = new ProtocolWriter();
                pw.Write(8);
                pw.Write(3);
                pw.Write("You are already in this room.");

                temp.SendMessage(pw);
            }
        }
Пример #3
0
        public void SystemSays(string text)
        {
            pw = new ProtocolWriter();
            pw.Write(8);
            pw.Write(3);
            pw.Write(text);

            foreach (Session session in users.Values)
            {
                session.SendMessage(pw);
            }
        }
Пример #4
0
        public void Reset()
        {
            Server.GetInstance().Room.Leave(user);

            user.Name     = "";
            user.RoleType = RoleType.None;

            ProtocolWriter pw = new ProtocolWriter();

            pw.Write((int)Reply.Reset);
            SendMessage(pw);
        }
Пример #5
0
        public void ModeratorSays(string from, string text)
        {
            pw = new ProtocolWriter();
            pw.Write(8);
            pw.Write(2);
            pw.Write(from);
            pw.Write(text);

            foreach (Session session in users.Values)
            {
                session.SendMessage(pw);
            }
        }
Пример #6
0
        public void SendMessage(ProtocolWriter protocolWriter)
        {
            try
            {
                int    packetLenght = protocolWriter.GetLenght();
                byte[] buffer       = new byte[4 + packetLenght];
                byte[] temp         = BitConverter.GetBytes(packetLenght);
                Array.Copy(temp, 0, buffer, 0, temp.Length);
                Array.Copy(protocolWriter.GetBuffer(), 0, buffer, 4, protocolWriter.GetLenght());

                socket.Send(buffer, 0, buffer.Length, SocketFlags.None);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
Пример #7
0
        private void OnParseMessage(ProtocolReader protocolReader)
        {
            ProtocolWriter pw = null;

            Request request = (Request)protocolReader.ReadInt32();

            Console.WriteLine(request);

            switch (request)
            {
            case Request.Debug:
                break;

            case Request.CheckNameAvailability:

                string temp = protocolReader.ReadString();

                bool isAvailable = Server.GetInstance().CheckNameAvailable(temp);

                if (user.CurrentLevel == LevelType.Starter)
                {
                    if (isAvailable)
                    {
                        pw = new ProtocolWriter();
                        pw.Write((int)Reply.NameAvailable);
                        pw.Write(true);

                        SendMessage(pw);
                    }
                    else
                    {
                        pw = new ProtocolWriter();
                        pw.Write((int)Reply.NameNotAvailable);
                        pw.Write(false);

                        SendMessage(pw);

                        pw = new ProtocolWriter();
                        pw.Write((int)Reply.Message);
                        pw.Write((int)MessageType.System);
                        pw.Write("The username is not available.");

                        SendMessage(pw);
                    }
                }

                break;

            case Request.ChooseName:

                string temp2 = protocolReader.ReadString();

                bool canChoose = Server.GetInstance().CheckNameAvailable(temp2);

                if (user.CurrentLevel == LevelType.Starter)
                {
                    if (canChoose)
                    {
                        user.SetLevel(LevelType.Test);
                        user.Name = temp2;

                        pw = new ProtocolWriter();
                        pw.Write((int)Reply.Name);
                        pw.Write(temp2);

                        SendMessage(pw);
                    }
                    else
                    {
                        pw = new ProtocolWriter();
                        pw.Write((int)Reply.NameNotAvailable);
                        pw.Write(false);

                        SendMessage(pw);

                        pw = new ProtocolWriter();
                        pw.Write((int)Reply.Message);
                        pw.Write((int)MessageType.System);
                        pw.Write("The username is not available.");

                        SendMessage(pw);
                    }
                }

                break;

            case Request.Join:

                if (user.CurrentLevel != LevelType.Starter)
                {
                    Server.GetInstance().Room.Join(user);

                    if (user.CurrentLevel == LevelType.Room)
                    {
                        pw = new ProtocolWriter();
                        pw.Write((int)Reply.Joined);
                        pw.Write(true);

                        SendMessage(pw);
                    }
                }

                break;

            case Request.Leave:

                if (user.CurrentLevel != LevelType.Starter)
                {
                    Server.GetInstance().Room.Leave(user);
                }

                break;

            case Request.SendText:

                if (user.CurrentLevel == LevelType.Room)
                {
                    string text = protocolReader.ReadString();
                    string from = user.Name;

                    Server.GetInstance().Room.SendText(from, text);
                }
                else
                {
                    pw = new ProtocolWriter();
                    pw.Write(5);
                    pw.Write(3);
                    pw.Write("To write a message you must join the room.");

                    SendMessage(pw);
                }

                break;

            case Request.SendWhisper:

                if (user.CurrentLevel == LevelType.Room)
                {
                    string from = user.Name;
                    string to   = protocolReader.ReadString();
                    string text = protocolReader.ReadString();

                    Server.GetInstance().Room.SendWhisper(from, to, text);
                }
                else
                {
                    pw = new ProtocolWriter();
                    pw.Write(5);
                    pw.Write(3);
                    pw.Write("To write a message you must join the room.");

                    SendMessage(pw);
                }

                break;
            }
        }