Ejemplo n.º 1
0
    //-------------------------------------------------------------------------
    //  暂时放这里,应该放入独立线程(防止阻塞用户登陆).
    public override void update(float elapsed_tm)
    {
        List <string> del = new List <string>();

        foreach (var player in mLoginPlayerQueue)
        {
            ClientLoginInfo info = player.Value;
            if (null == info)
            {
                continue;
            }

            // 先做断线检测.
            if (info.state != eLogingState.loginError)
            {
                // todo,添加session是否处于连接状态的查询接口
                if (info.session == null)// || !info.session. .Connected)
                {
                    info.state  = eLogingState.loginError;
                    info.result = eLoginResult.disconnected;
                }
            }

            if (info.state == eLogingState.connect)
            {
                eLoginResult rtCode = eLoginResult.accountNotExists;
                string       sql    = string.Format("SELECT AccountName, Password , LoginStatus , AccountId FROM Account WHERE AccountName='{0}';", info.account);

                EbLog.Note("Login SQL STR :" + sql);

                MySqlCommand    cmd = new MySqlCommand(sql, connection);
                MySqlDataReader rdr = null;
                try
                {
                    rdr = cmd.ExecuteReader();
                    if (rdr.Read())
                    {
                        if ((string)rdr["Password"] != info.password)
                        {
                            rtCode = eLoginResult.wrongPassword;
                        }
                        else if ((string)rdr["LoginStatus"] != eLoginStatus.offline.ToString())
                        {
                            rtCode = eLoginResult.loginstatus;
                        }
                        else
                        {
                            // 登录成功.
                            rtCode         = eLoginResult.success;
                            info.tokenId   = generateId().ToString();
                            info.accountId = (long)rdr["AccountId"];
                        }
                        info.result = rtCode;
                    }

                    if (rdr != null)
                    {
                        rdr.Close();
                        rdr.Dispose();
                        rdr = null;
                    }

                    if (rtCode == eLoginResult.success)
                    {
                        // update status
                        sql = string.Format("UPDATE Account SET LoginStatus = '{0}' WHERE AccountName='{1}';", eLoginStatus.loging.ToString(), info.account);

                        EbLog.Note("Login SQL STR :" + sql);

                        MySqlCommand updateCmd = new MySqlCommand(sql, connection);
                        cmd.ExecuteNonQuery();
                    }
                }
                catch (Exception ex)
                {
                    EbLog.Error(ex.ToString());
                }
                finally
                {
                    cmd.Dispose();
                    cmd = null;

                    if (rdr != null)
                    {
                        if (rdr.IsClosed == false)
                        {
                            rdr.Close();
                        }
                        rdr.Dispose();
                        rdr = null;
                    }
                }

                if (rtCode == ((byte)eLoginResult.success))
                {
                    info.state = eLogingState.updateLoging2Db;
                }
            }

            if (info.state == eLogingState.updateLoging2Db)
            {
                foreach (var gate in mGateInfo)
                {
                    GateInfo ser = gate.Value;
                    if (!ser.bloginLock)
                    {
                        // 目前只有账号信息和当前longin id放入ZooKeeper.
                        string dt = info.account + "," + mCoApp.NodeIdStr + "," + info.tokenId.ToString() + "," + info.accountId.ToString();
                        info.gateId = ser.id;
                        mCoApp.getZk().awriteData(ser.loginNode, dt, null);
                        EbLog.Note("send to gate node :" + ser.loginNode + ",account:" + dt);
                        mCoApp.getZk().acreate(ser.loginLockNode, "", ZK_CONST.ZOO_EPHEMERAL, null);
                        EbLog.Note("set remote lock :" + ser.loginLockNode + ",account:" + dt);

                        ser.bloginLock = true;
                        info.state     = eLogingState.updateZk2Gate;
                        break;
                    }
                }
            }

            if (info.state == eLogingState.gateBackSuccess ||
                info.state == eLogingState.gateBackFailed ||
                info.state == eLogingState.loginError)
            {
                eLoginStatus curState = (info.state == eLogingState.gateBackSuccess) ? eLoginStatus.online : eLoginStatus.offline;
                string       sql      = string.Format("UPDATE Account SET LoginStatus = '{0}' WHERE AccountName='{1}';", curState, info.account);

                EbLog.Note("Login SQL STR :" + sql);

                MySqlCommand cmd = new MySqlCommand(sql, connection);
                try
                {
                    MySqlCommand updateCmd = new MySqlCommand(sql, connection);
                    cmd.ExecuteNonQuery();
                    info.state = eLogingState.updateOnline2Db;
                }
                catch (Exception ex)
                {
                    //mLog.ErrorFormat("accoundName:{0}, password:{1} ", info.account, info.password);
                    EbLog.Error(ex.ToString());
                }
                finally
                {
                    cmd.Dispose();
                    cmd = null;
                }

                if (info.state == eLogingState.updateOnline2Db)
                {
                    //反馈消息给client.
                    //if (info.peer.Connected)// todo,判定session是否处于连接状态
                    {
                        //Dictionary<byte, object> p = new Dictionary<byte, object>();
                        //p[0] = curState.ToString();
                        //if (curState == eLoginStatus.online)
                        //{
                        //    var list = mGateInfo.Where(gt => gt.Value.id.Equals(info.gateId));
                        //    p[1] = list.First().Value.ipport;
                        //    p[2] = info.tokenId;
                        //}
                        //else
                        //{
                        //    p[1] = info.result.ToString();
                        //}

                        // todo,添加session发送任意数据的方法
                        LoginUCenterSession <DefUCenterSession> se = info.session as LoginUCenterSession <DefUCenterSession>;
                        se.login2ClientLogin(curState.ToString(), info.tokenId, info.param);

                        //OperationResponse operation_response = new OperationResponse(1, p);
                        //SendResult r = info.peer.SendOperationResponse(operation_response, new SendParameters { ChannelId = 0 });
                        ////info.session.getRpcPeer().sendEntityRpcData()
                        //if (r != SendResult.Ok)
                        //{
                        //    // Error
                        //}

                        //if (info.peer.Connected)
                        {
                            // 应该断开与客户端的连接.
                            //info.peer.Disconnect();
                        }
                    }

                    info.state = eLogingState.backToClient;
                }

                if (info.state == eLogingState.backToClient)
                {
                    del.Add(info.account);
                }
            }
        }

        foreach (string account in del)
        {
            ClientLoginInfo delPlayer = null;
            mLoginPlayerQueue.TryRemove(account, out delPlayer);
            //delPlayer.peer.Dispose();// todo 连接断开管理
        }

        string offlineaccount = null;

        if (mofflineQueue.TryDequeue(out offlineaccount))
        {
            eLoginStatus curState = eLoginStatus.offline;
            string       sql      = string.Format("UPDATE Account SET LoginStatus = '{0}' WHERE AccountName='{1}';", curState, offlineaccount);

            EbLog.Error("player offline Login SQL STR :" + sql);
            MySqlCommand cmd = new MySqlCommand(sql, connection);
            try
            {
                MySqlCommand updateCmd = new MySqlCommand(sql, connection);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //mLog.ErrorFormat("accoundName:{0} offline failed ", offlineaccount);
                EbLog.Error(ex.ToString());
            }
            finally
            {
                cmd.Dispose();
                cmd = null;
            }
        }
    }
Ejemplo n.º 2
0
    // public void onLoginNodeInfo(int result, string data, string[] servers, Dictionary<string, object> param)
    //{
    //    mCoApp.OnGetNodeInfo(data);
    //}

    //-------------------------------------------------------------------------
    void _onOfflineNode(int result, string data, string[] servers, Dictionary <string, object> param)
    {
        if (result != 0 || data == null)
        {
            return;
        }
        GateInfo gt   = param["gateInfo"] as GateInfo;
        string   path = param["path"] as string;
        LoginNode <ComponentDef> info = param["LoginNode"] as LoginNode <ComponentDef>;

        if (gt.bofflineLock == false)
        {
            EbLog.Error("Error get unlock data: " + path);
        }
        string[] resul;
        char[]   charSeparators = new char[] { ',', ':' };
        resul = data.Split(charSeparators);
        int index = 0;

        while (index < resul.Length)
        {
            string account = resul[index++];
            info.onPlayerOffline(account);
        }
        // 通知gate已经处理了数据.
        mCoApp.getZk().sdelete(gt.offlineLock);
    }