Exemplo n.º 1
0
        private void HeartbeatWorker()
        {
            while (true)
            {
                try
                {
                    if (mIsConnected)
                    {
                        JsonObject json = new JsonObject();
                        json[LicDefines.KEYWORD_MSG_COMMON_CLASSID] =
                            new JsonProperty(string.Format("{0}", LicDefines.LICENSE_MSG_CLASS_CONNECTION));
                        json[LicDefines.KEYWORD_MSG_COMMON_CLASSDESC] =
                            new JsonProperty(string.Format("\"{0}\"",
                                                           LicUtils.GetClassDesc(LicDefines.LICENSE_MSG_CLASS_CONNECTION)));
                        json[LicDefines.KEYWORD_MSG_COMMON_MESSAGEID] =
                            new JsonProperty(string.Format("{0}", LicDefines.LICENSE_MSG_CONNECTION_HEARTBEAT));
                        json[LicDefines.KEYWORD_MSG_COMMON_MESSAGEDESC] =
                            new JsonProperty(string.Format("\"{0}\"",
                                                           LicUtils.GetMessageDesc(LicDefines.LICENSE_MSG_CLASS_CONNECTION,
                                                                                   LicDefines.LICENSE_MSG_CONNECTION_HEARTBEAT)));
                        json[LicDefines.KEYWORD_MSG_COMMON_CURRENTTIME] =
                            new JsonProperty(string.Format("\"{0}\"",
                                                           DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss")));
                        SendMessage(json.ToString());

                        OnDebug(LogMode.Info, string.Format("Send heartbeat end"));
                    }
                }
                catch (Exception ex)
                {
                    OnDebug(LogMode.Error, string.Format("Send heartbeat fail.\t{0}", ex.Message));
                }

                Thread.Sleep(mHeartbeatNum / 2 * 1000);
            }
        }
Exemplo n.º 2
0
        private void DealMessage(NetPacketHeader header, string strMsg)
        {
            try
            {
                JsonObject json      = new JsonObject(strMsg);
                int        classid   = (int)json[LicDefines.KEYWORD_MSG_COMMON_CLASSID].Number;
                int        messageid = (int)json[LicDefines.KEYWORD_MSG_COMMON_MESSAGEID].Number;
                JsonObject jsonReturn;
                string     strSession;
                string     strVerify;
                switch (classid)
                {
                case LicDefines.LICENSE_MSG_CLASS_AUTHENTICATE:
                    switch (messageid)
                    {
                    case LicDefines.LICENSE_MSG_AUTH_WELCOME:
                        bool isEncrypt = (header.State & 4) > 0;
                        mIsMsgEncrypt = isEncrypt;
                        strSession    =
                            json[LicDefines.KEYWORD_MSG_COMMON_DATA][LicDefines.KEYWORD_MSG_APPINFO_SESSION]
                            .Value;
                        OnDebug(LogMode.Info, string.Format("Session code:\t{0}", strSession));
                        strVerify = LicUtils.GetVerificationCode(strSession);
                        OnDebug(LogMode.Info, string.Format("Verification code:\t{0}", strVerify));
                        jsonReturn = new JsonObject();
                        jsonReturn[LicDefines.KEYWORD_MSG_COMMON_CLASSID] =
                            new JsonProperty(string.Format("{0}", LicDefines.LICENSE_MSG_CLASS_AUTHENTICATE));
                        jsonReturn[LicDefines.KEYWORD_MSG_COMMON_CLASSDESC] =
                            new JsonProperty(string.Format("\"{0}\"",
                                                           LicUtils.GetClassDesc(LicDefines.LICENSE_MSG_CLASS_AUTHENTICATE)));
                        jsonReturn[LicDefines.KEYWORD_MSG_COMMON_MESSAGEID] =
                            new JsonProperty(string.Format("{0}", LicDefines.LICENSE_MSG_AUTH_LOGON));
                        jsonReturn[LicDefines.KEYWORD_MSG_COMMON_MESSAGEDESC] =
                            new JsonProperty(string.Format("\"{0}\"",
                                                           LicUtils.GetMessageDesc(LicDefines.LICENSE_MSG_CLASS_AUTHENTICATE,
                                                                                   LicDefines.LICENSE_MSG_AUTH_LOGON)));
                        jsonReturn[LicDefines.KEYWORD_MSG_COMMON_CURRENTTIME] =
                            new JsonProperty(string.Format("\"{0}\"",
                                                           DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss")));
                        jsonReturn[LicDefines.KEYWORD_MSG_COMMON_DATA] = new JsonProperty(new JsonObject());
                        jsonReturn[LicDefines.KEYWORD_MSG_COMMON_DATA][LicDefines.KEYWORD_MSG_CONNTION_HEARTBEAT
                        ] = new JsonProperty(string.Format("{0}", mHeartbeatNum));
                        jsonReturn[LicDefines.KEYWORD_MSG_COMMON_DATA][LicDefines.KEYWORD_MSG_APPINFO_MODULENAME
                        ] = new JsonProperty(string.Format("\"{0}\"", mClient));
                        jsonReturn[LicDefines.KEYWORD_MSG_COMMON_DATA][LicDefines.KEYWORD_MSG_APPINFO_MODULENUMBER
                        ] = new JsonProperty(string.Format("{0}", mModuleNumber));
                        jsonReturn[LicDefines.KEYWORD_MSG_COMMON_DATA][LicDefines.KEYWORD_MSG_APPINFO_MODULETYPEID
                        ] = new JsonProperty(string.Format("{0}", mModuleTypeID));
                        jsonReturn[LicDefines.KEYWORD_MSG_COMMON_DATA][LicDefines.KEYWORD_MSG_AUTH_PROTOCOL
                        ] = new JsonProperty(string.Format("\"{0}\"", mProtocolVersion));
                        jsonReturn[LicDefines.KEYWORD_MSG_COMMON_DATA][LicDefines.KEYWORD_MSG_APPINFO_SESSION
                        ] = new JsonProperty(string.Format("\"{0}\"", strSession));
                        jsonReturn[LicDefines.KEYWORD_MSG_COMMON_DATA][LicDefines.KEYWORD_MSG_AUTH_VERIFICATION
                        ] = new JsonProperty(string.Format("\"{0}\"", strVerify));
                        SendMessage(jsonReturn.ToString());
                        break;

                    case LicDefines.LICENSE_MSG_AUTH_SUCCESS:
                        strVerify =
                            json[LicDefines.KEYWORD_MSG_COMMON_DATA][LicDefines.KEYWORD_MSG_AUTH_VERIFICATION]
                            .Value;
                        mIsLogined = true;
                        //登录成功
                        OnDebug(LogMode.Info, string.Format("Logon successful.\t{0}", strVerify));
                        //触发登录成功事件
                        OnServerConnectionEvent(Defines.EVT_NET_AUTHED, strVerify);
                        //启动心跳线程
                        CreateHeartbeatThread();
                        break;

                    case LicDefines.LICENSE_MSG_AUTH_FAILED:
                    case LicDefines.LICENSE_MSG_AUTH_DENY_CLIENT_TYPE:
                    case LicDefines.LICENSE_MSG_AUTH_PROTOCOL_CLIENT:
                    case LicDefines.LICENSE_MSG_AUTH_PROTOCOL_SERVER:
                    case LicDefines.LICENSE_MSG_AUTH_WRONGFUL_SERVER:
                    case LicDefines.LICENSE_MSG_AUTH_TIME_NOT_SYNC:
                        //登录失败
                        OnDebug(LogMode.Info, string.Format("Logon failed.\t{0}", messageid));
                        mIsLogined = false;
                        break;
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                OnDebug(LogMode.Error, string.Format("DealMessage fail.\t{0}", ex.Message));
            }
        }