예제 #1
0
 public void SendMessage(NetPacketHeader header, byte[] data)
 {
     try
     {
         bool isEncrypt = (header.State & 4) > 0;
         if (isEncrypt &&
             mEncryptObject != null)
         {
             data = mEncryptObject.EncryptBytes(data, (int)EncryptionMode.AES256V13Hex);
         }
         header.Size = (uint)data.Length;
         if (!mIsConnected ||
             mNetStream == null)
         {
             OnDebug(LogMode.Error, string.Format("Server not connected or stream is null"));
             return;
         }
         byte[] byteHeader = Converter.Struct2Bytes(header);
         mNetStream.Write(byteHeader, 0, byteHeader.Length);
         mNetStream.Write(data, 0, data.Length);
         mNetStream.Flush();
     }
     catch (Exception ex)
     {
         OnDebug(LogMode.Error, string.Format("SendMessage fail.\t{0}", ex.Message));
     }
 }
예제 #2
0
        public void SendMessage(string msg)
        {
            NetPacketHeader header = new NetPacketHeader();

            header.Flag   = new[] { (byte)'L', (byte)'M' };
            header.Format = LicDefines.DH_FORMAT_JSON;
            header.State  = (ushort)(mIsMsgEncrypt ? 4 : 0);
            SendMessage(header, msg);
        }
예제 #3
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));
            }
        }
예제 #4
0
 public void SendMessage(NetPacketHeader header, string msg)
 {
     OnDebug(LogMode.Debug, string.Format("Send:\t{0}", msg));
     SendMessage(header, Encoding.ASCII.GetBytes(msg));
 }