internal static byte[] BuildNodeMessage(JsonMessage msg) { StringBuilder sb = new StringBuilder(); sb.Append(msg.m_ID); sb.Append('|'); sb.Append(JsonMapper.ToJson(msg.m_JsonData)); if (null != msg.m_ProtoData) { byte[] bytes = Encoding.Encode(msg.m_ProtoData); sb.Append('|'); sb.Append(Convert.ToBase64String(bytes)); } return System.Text.Encoding.UTF8.GetBytes(sb.ToString()); }
internal static JsonMessage DecodeJsonMessage(byte[] data) { JsonMessage msg = null; if (s_Inited) { string msgStr = null; try { msgStr = System.Text.Encoding.UTF8.GetString(data); int ix = msgStr.IndexOf('|'); if (ix > 0) { int id = int.Parse(msgStr.Substring(0, ix)); int ix2 = msgStr.IndexOf('|', ix + 1); msg = new JsonMessage(id); if (ix2 > 0) { string jsonStr = msgStr.Substring(ix + 1, ix2 - ix - 1); string protoStr = msgStr.Substring(ix2 + 1); msg.m_JsonData = JsonMapper.ToObject(jsonStr); Type t = s_MessageHandlers[id].m_ProtoType; if (null != t) { byte[] bytes = Convert.FromBase64String(protoStr); msg.m_ProtoData = Encoding.Decode(t, bytes); } } else { string jsonStr = msgStr.Substring(ix + 1); msg.m_JsonData = JsonMapper.ToObject(jsonStr); } } } catch (Exception ex) { if (null == msgStr) { LogSys.Log(LOG_TYPE.ERROR, "[Exception] DecodeJsonMessage:{0}\n{1}", ex.Message, ex.StackTrace); } else { LogSys.Log(LOG_TYPE.ERROR, "[Exception] DecodeJsonMessage:{0} {1}\n{2}", msgStr, ex.Message, ex.StackTrace); } } } return msg; }
// internal void GMLoadAccount(string gmAccount, string accountId, int nodeHandle) { Msg_LD_Load msg = new Msg_LD_Load(); msg.MsgId = (int)DataEnum.TableAccount; msg.PrimaryKeys.Add(accountId); Msg_LD_SingleLoadRequest reqAccount = new Msg_LD_SingleLoadRequest(); reqAccount.MsgId = (int)DataEnum.TableAccount; reqAccount.LoadType = Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadSingle; reqAccount.Keys.Add(accountId); msg.LoadRequests.Add(reqAccount); RequestLoad(msg, (ret) => { JsonMessage resultMsg = new JsonMessage(LobbyGmMessageDefine.Msg_CL_GmQueryAccount, gmAccount); GameFrameworkMessage.Msg_LC_GmQueryAccount protoData = new GameFrameworkMessage.Msg_LC_GmQueryAccount(); protoData.m_Result = GameFrameworkMessage.GmResultEnum.Failed; protoData.m_QueryAccount = accountId; protoData.m_AccountState = GameFrameworkMessage.GmStateEnum.Offline; resultMsg.m_ProtoData = protoData; KeyString primaryKey = KeyString.Wrap(ret.PrimaryKeys); if (Msg_DL_LoadResult.ErrorNoEnum.Success == ret.ErrorNo) { LogSys.Log(LOG_TYPE.INFO, ConsoleColor.Green, "DataCache Load Success: Msg:{0}, Key:{1}", "Account", primaryKey); TableAccount dataAccount = null; foreach (var result in ret.Results) { object _msg; if (DbDataSerializer.Decode(result.Data, DataEnum2Type.Query(result.MsgId), out _msg)) { DataEnum msgEnum = (DataEnum)result.MsgId; switch (msgEnum) { case DataEnum.TableAccount: dataAccount = _msg as TableAccount; break; default: LogSys.Log(LOG_TYPE.ERROR, ConsoleColor.Red, "Decode account data ERROR. Wrong message id. Account:{0}, WrongId:{1}", result.PrimaryKeys[0], msgEnum); break; } } } protoData.m_Result = GameFrameworkMessage.GmResultEnum.Success; protoData.m_QueryAccount = dataAccount.AccountId; protoData.m_AccountState = GameFrameworkMessage.GmStateEnum.Online; if (dataAccount.IsBanned) { protoData.m_AccountState = GameFrameworkMessage.GmStateEnum.Banned; } } else if (Msg_DL_LoadResult.ErrorNoEnum.NotFound == ret.ErrorNo) { LogSys.Log(LOG_TYPE.INFO, ConsoleColor.Yellow, "DataCache Load NotFound: Msg:{0}, Key:{1}", "Account", primaryKey); } else { LogSys.Log(LOG_TYPE.ERROR, ConsoleColor.Red, "DataCache Load Failed: Msg:{0}, Key:{1}, ERROR:{2}", "Account", primaryKey, ret.ErrorInfo); } resultMsg.m_ProtoData = protoData; JsonGmMessageDispatcher.SendNodeMessage(nodeHandle, resultMsg); }); }
internal static void SendNodeMessage(string name, JsonMessage msg) { if (s_Inited) { byte[] data = BuildNodeMessage(msg); if (null != name && null != data) { CenterHubApi.SendByName(s_WorldId, name, data, data.Length); } } }
internal static void SendNodeMessage(int handle, JsonMessage msg) { if (s_Inited) { byte[] data = BuildNodeMessage(msg); if (null != data) { CenterHubApi.SendByHandle(s_WorldId, handle, data, data.Length); } } }
internal static void HandleNodeMessage(JsonMessage msg, int handle, uint session) { if (s_Inited && msg != null) { //LogSys.Log(LOG_TYPE.DEBUG, "Handle Json Message:{0}={1}", msg.m_ID, msg.GetType().Name); JsonMessageHandlerInfo info = s_MessageHandlers[(int)msg.m_ID]; if (info != null && info.m_Handler != null) { info.m_Handler(msg, handle, session); } } }