예제 #1
0
        private void TransferMemeberToRoom(Socket socket_member, QChatRoom room)
        {
            QMember member = m_socketMembers[socket_member];

            member.Room = room;
            room.AddMember(socket_member);
        }
예제 #2
0
        private void CheckLogin()
        {
            if (!m_socketMembers.ContainsKey(m_currentSocket))
            {
                throw new QNotLoginExeption();
            }

            m_currentMember = m_socketMembers[m_currentSocket];
        }
예제 #3
0
        private void Proccess(Socket socket, string socket_input)
        {
            try
            {
                string[] args;
                string   command;
                CommandSplit(socket_input, out command, out args);

                // Set context
                m_currentSocket = socket;
                m_currentMember = null;
                m_args          = args;

                if (m_commandProcessers.ContainsKey(command))
                {
                    var proccesser = m_commandProcessers[command];

                    try
                    {
                        string ret_message = proccesser.Invoke();
                        byte[] send_data   = GetBytes(ret_message);
                        socket.Send(send_data);
                    }
                    catch (QInvalidRoomExeption)
                    {
                        socket.Send(GetBytes("101 No such room."));
                    }
                    catch (QNoMoreRoomExeption)
                    {
                        socket.Send(GetBytes("102 No moor room, please try again later."));
                    }
                    catch (QNotLoginExeption)
                    {
                        socket.Send(GetBytes("103 Please use LOGIN to login first."));
                    }
                    catch (ArgumentException)
                    {
                        socket.Send(GetBytes("104 Arguments not satisfied."));
                    }
                    catch (QNotInRoomException)
                    {
                        socket.Send(GetBytes("105 You have to enter a room first."));
                    }
                }
                else
                {
                    // Not implement
                    Print(string.Format("未识别的指令:\"{0}\"", command));
                    socket.Send(GetBytes("100 Unrecognized command"));
                }
            }
            catch (Exception e)
            {
                Print(e.Message);
            }
        }
예제 #4
0
        private string Command_LOGIN()
        {
            CheckArgumentCount(1);

            m_socketTourists.Remove(m_currentSocket);
            QMember member = new QMember(m_args[0], null);

            m_socketMembers.Add(m_currentSocket, member);

            return("001 Login successful.");
        }
예제 #5
0
        private void TransferMemberOutofRoom(Socket socket_member)
        {
            QMember   member = m_socketMembers[socket_member];
            QChatRoom room   = member.Room;

            room.RemoveMember(socket_member);
            member.Room = null;

            if (room.Members.Count == 0)
            {
                m_chatRooms[room.RoomId] = null;
            }
        }
예제 #6
0
        private void SocketClose(Socket socket)
        {
            if (!m_socketTourists.Remove(socket))
            {
                QMember member = m_socketMembers[socket];
                if (member.Room != null)
                {
                    TransferMemberOutofRoom(socket);
                }

                m_socketMembers.Remove(socket);
            }
        }
예제 #7
0
        private string Command_ROOMS()
        {
            string result;

            int room_count = RoomCount;

            if (room_count == 0)
            {
                result = "003 There is currently no room in use.";
            }
            else
            {
                result = string.Format("003 There are total of {0} rooms\n", room_count);
                foreach (var room in m_chatRooms)
                {
                    if (room == null)
                    {
                        continue;
                    }

                    List <Socket> socket_members = room.Members;
                    string        room_info      = string.Format("Room Id:{0}, {1} People in the room\nRoom members:", room.RoomId, socket_members.Count);
                    foreach (Socket socket_member in socket_members)
                    {
                        QMember member = m_socketMembers[socket_member];

                        if (socket_member.GetHashCode() == socket_members.Last().GetHashCode())
                        {
                            room_info += string.Format("{0}.", member.Name);
                        }
                        else
                        {
                            room_info += string.Format("{0},", member.Name);
                        }
                    }
                    room_info += "\n";

                    result += room_info;
                }
            }

            return(result);
        }