예제 #1
0
        private void DoDisagreeJoinGroup(string formatString)
        {
            AnalysisStr analy = new AnalysisStr(formatString);
            DBFunction  dbf   = new DBFunction();

            dbf.DisagreeJoinGroup(int.Parse(analy.GetParaIndexOf(1)), analy.GetParaIndexOf(0));
            string msg = "Server#" + OpType.DISAGREE_JOIN_GROUP_RES + "#" + analy.GetParaIndexOf(0) + "#" + analy.GetParaIndexOf(1);

            if (OLList.IsOnLine(analy.GetParaIndexOf(0)))
            {
                SmallUser sUser  = OLList.GetUserByID(analy.GetParaIndexOf(0));
                CClient   client = new CClient(sUser.IPAddress, sUser.IPPort);
                try
                {
                    client.ConnectToServer();
                    client.GetNetworkStream();
                    client.Communicate(msg);
                    client.ReLeaseAll();
                }
                catch (Exception e)
                {
                    System.Console.WriteLine("无法连接用户");
                }
            }
        }
예제 #2
0
        //发送群组消息,如果在线则提示对方,如果不在线则送到离线数据库
        private void DoSendGroupMsg(string formatString)
        {
            AnalysisStr     analy = new AnalysisStr(formatString);
            DBFunction      dbf   = new DBFunction();
            List <UserInfo> list  = dbf.GetMembersInGroup(int.Parse(analy.GetParaIndexOf(1)));

            list.Add(dbf.GetUserInfo(dbf.GetGroupInfo(int.Parse(analy.GetParaIndexOf(1))).GroupCreator));
            string msg = "Server#" + OpType.SEND_GROUP_MSG_RES + "#" + analy.GetParaIndexOf(0) + "#" + dbf.GetUserInfo(analy.GetParaIndexOf(0)).UserName + "#" + analy.GetParaIndexOf(1) + "#" + analy.GetParaFromIndex(2);

            foreach (UserInfo user in list)
            {
                if (OLList.IsOnLine(user.UserID))
                {
                    SmallUser sUser  = OLList.GetUserByID(user.UserID);
                    CClient   client = new CClient(sUser.IPAddress, sUser.IPPort);
                    try
                    {
                        client.ConnectToServer();
                        client.GetNetworkStream();
                        client.Communicate(msg);
                        client.ReLeaseAll();
                    }
                    catch (Exception e)
                    {
                        System.Console.WriteLine("无法连接用户");
                    }
                }
                else
                {
                    dbf.SendOffLineMessage(user.UserID, analy.GetParaIndexOf(0), analy.GetParaFromIndex(2));
                }
            }
        }
예제 #3
0
        //应该确保不是加入自己创建的群组,以及应该没有已有的请求,否则返回空的想序列值
        private string DoJoinGroup(string formatString)
        {
            AnalysisStr analy = new AnalysisStr(formatString);
            DBFunction  dbf   = new DBFunction();
            string      msg   = "Server#" + OpType.JOIN_GROUP_RES;

            if (!dbf.GetGroupInfo(int.Parse(analy.GetParaIndexOf(1))).GroupCreator.Equals(analy.GetParaIndexOf(0)) && !dbf.EntityExists(int.Parse(analy.GetParaIndexOf(1)), analy.GetParaIndexOf(0)))
            {
                msg += "#" + dbf.RequestJoinGroup(int.Parse(analy.GetParaIndexOf(1)), analy.GetParaIndexOf(0));
                string notice = "Server#" + OpType.JOIN_GROUP_NOTE + "#" + analy.GetParaIndexOf(0) + "#" + analy.GetParaIndexOf(1);
                if (OLList.IsOnLine(dbf.GetGroupInfo(int.Parse(analy.GetParaIndexOf(1))).GroupCreator))
                {
                    SmallUser sUser  = OLList.GetUserByID(dbf.GetGroupInfo(int.Parse(analy.GetParaIndexOf(1))).GroupCreator);
                    CClient   client = new CClient(sUser.IPAddress, sUser.IPPort);
                    try
                    {
                        client.ConnectToServer();
                        client.GetNetworkStream();
                        client.Communicate(notice);
                        client.ReLeaseAll();
                    }
                    catch (Exception e)
                    {
                        System.Console.WriteLine("无法连接用户");
                    }
                }
            }
            return(msg);
        }
예제 #4
0
        private void DoAgreeFriendRequest(string formatString)
        {
            AnalysisStr analy = new AnalysisStr(formatString);
            DBFunction  dbf   = new DBFunction();

            dbf.AgreeFriendRequest(analy.GetParaIndexOf(0), analy.GetParaIndexOf(1));
            string msg = "Server#" + OpType.AGREE_FRIEND_REQUEST_RES + "#" + analy.GetParaIndexOf(0) + "#" + analy.GetParaIndexOf(1);

            if (OLList.IsOnLine(analy.GetParaIndexOf(1)))
            {
                SmallUser sUser  = OLList.GetUserByID(analy.GetParaIndexOf(1));
                CClient   client = new CClient(sUser.IPAddress, sUser.IPPort);
                try
                {
                    client.ConnectToServer();
                    client.GetNetworkStream();
                    client.Communicate(msg);
                    client.ReLeaseAll();
                }
                catch (Exception e)
                {
                    System.Console.WriteLine("无法连接用户");
                }
            }
        }
예제 #5
0
        //这里调用时应该注意应确保两者不是好友,且两者没有未处理的加好友请求,否则返回空的序列值
        private string DoSendFriendRequest(string formatString)
        {
            AnalysisStr analy = new AnalysisStr(formatString);
            DBFunction  dbf   = new DBFunction();
            string      msg   = "Server#" + OpType.SEND_FRIEND_REQUEST_RES;

            if (!dbf.IsFriend(analy.GetParaIndexOf(0), analy.GetParaIndexOf(1)) && !dbf.RequestExists(analy.GetParaIndexOf(0), analy.GetParaIndexOf(1)) && !dbf.RequestExists(analy.GetParaIndexOf(1), analy.GetParaIndexOf(0)) && !analy.GetParaIndexOf(0).Equals(analy.GetParaIndexOf(1)))
            {
                msg += "#" + dbf.RequestFriend(analy.GetParaIndexOf(0), analy.GetParaIndexOf(1));
                string notice = "Server#" + OpType.SEND_FRIEND_REQUEST_NOTE + "#" + analy.GetParaIndexOf(0) + "#" + analy.GetParaIndexOf(1);
                if (OLList.IsOnLine(analy.GetParaIndexOf(0)))
                {
                    SmallUser sUser  = OLList.GetUserByID(analy.GetParaIndexOf(0));
                    CClient   client = new CClient(sUser.IPAddress, sUser.IPPort);
                    try
                    {
                        client.ConnectToServer();
                        client.GetNetworkStream();
                        client.Communicate(notice);
                        client.ReLeaseAll();
                    }
                    catch (Exception e)
                    {
                        System.Console.WriteLine("无法连接用户");
                    }
                }
            }
            return(msg);
        }
예제 #6
0
        private void DoLogout(string formatString)
        {
            AnalysisStr analy = new AnalysisStr(formatString);
            DBFunction  dbf   = new DBFunction();

            OLList.OffLined(analy.GetParaIndexOf(0));
            string msg = "Server#" + OpType.LOGOUT_NOTE + "#" + analy.GetParaIndexOf(0);

            foreach (string userID in OLList.GetAllOLUsersID())
            {
                if (dbf.IsFriend(userID, analy.GetParaIndexOf(0)))
                {
                    SmallUser sUser  = OLList.GetUserByID(userID);
                    CClient   client = new CClient(sUser.IPAddress, sUser.IPPort);
                    try
                    {
                        client.ConnectToServer();
                        client.GetNetworkStream();
                        client.Communicate(msg);
                        client.ReLeaseAll();
                    }
                    catch (Exception e)
                    {
                        System.Console.WriteLine("无法连接用户");
                    }
                }
            }
        }
예제 #7
0
        private string DoLogin(string formatString)
        {
            AnalysisStr analy  = new AnalysisStr(formatString);
            DBFunction  dbf    = new DBFunction();
            string      format = "Server#" + OpType.LOGIN_RES + "#";

            format += (dbf.Login(analy.GetParaIndexOf(0), analy.GetParaIndexOf(1)) == true?1:0);
            if (dbf.Login(analy.GetParaIndexOf(0), analy.GetParaIndexOf(1)))
            {
                string IP   = endPoint.Address.ToString();
                int    Port = endPoint.Port;
                OLList.OnLined(new SmallUser(analy.GetUserID(), IP, Port));

                string msg = "Server#" + OpType.INFORM_FRIENDS + "#" + analy.GetParaIndexOf(0);
                foreach (string userID in OLList.GetAllOLUsersID())
                {
                    if (dbf.IsFriend(analy.GetParaIndexOf(0), userID))
                    {
                        SmallUser sUser  = OLList.GetUserByID(userID);
                        CClient   client = new CClient(sUser.IPAddress, sUser.IPPort);
                        client.ConnectToServer();
                        client.GetNetworkStream();
                        client.Communicate(msg);
                        client.ReLeaseAll();
                    }
                }
            }
            return(format);
        }
예제 #8
0
        public void Run()
        {
            StartListener();
            while (true)
            {
                GetNetworkStream();
                string format = Communicate();
                ReleaseAll();

                System.Console.WriteLine(format);

                string ret = serverFunc.DistributeTasks(format);

                System.Console.Write("在线用户:" + OLList.Length() + "位,分别为");
                foreach (string userID in OLList.GetAllOLUsersID())
                {
                    System.Console.Write("<" + userID + ">__");
                }
                System.Console.WriteLine();



                if (!ret.Equals("###"))
                {
                    CClient client = new  CClient(endPoint.Address.ToString(), endPoint.Port);

                    System.Console.WriteLine("Server发送时IP以及端口:" + endPoint.Address.ToString() + ":" + endPoint.Port);

                    try
                    {
                        client.ConnectToServer();
                        client.GetNetworkStream();
                        client.Communicate(ret);
                        client.ReLeaseAll();
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine("无法连接对方...");
                        foreach (string userID in OLList.GetAllOLUsersID())
                        {
                            if (OLList.GetUserByID(userID).IPAddress.Equals(endPoint.Address.ToString()) && OLList.GetUserByID(userID).IPPort == endPoint.Port)
                            {
                                OLList.OffLined(userID);
                                System.Console.WriteLine("检测到用户ID为:<" + userID + ">的用户异常关闭客户端...");
                            }
                        }
                        System.Console.WriteLine("此时在线用户:" + OLList.Length() + "位");
                    }
                }
            }
        }
예제 #9
0
        //检测函数,检测客户端是否关闭,检测异常关闭以及挂掉
        public void TrueOnLineUsers()
        {
            foreach (string userID in OLList.GetAllOLUsersID())
            {
                if (OLList.IsOnLine(userID))
                {
                    try
                    {
                        CClient client = new CClient(OLList.GetUserByID(userID).IPAddress, OLList.GetUserByID(userID).IPPort);
                        client.ConnectToServer();
                        client.GetNetworkStream();
                        client.Communicate("###");
                        client.ReLeaseAll();
                    }
                    catch (Exception e)
                    {
                        OLList.OffLined(userID);

                        System.Console.WriteLine(userID + "客户端异常关闭,现在在线用户数量:" + OLList.Length());
                    }
                }
            }
        }