Exemplo n.º 1
0
        internal static unsafe JsonMessage DecodeJsonMessage(byte[] data)
        {
            JsonMessage msg = null;

            if (s_Inited)
            {
                try
                {
                    string 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);
                        if (ix2 > 0)
                        {
                            string jsonStr  = msgStr.Substring(ix + 1, ix2 - ix - 1);
                            string protoStr = msgStr.Substring(ix2 + 1);
                            int    strLen   = protoStr.Length;
                            Type   type     = GetMessageType(id);
                            msg = JsonConvert.DeserializeObject(jsonStr, type) as JsonMessage;
                            Type t = s_MessageHandlers[id].m_ProtoType;
                            if (null != t && strLen > 0)
                            {
                                byte[] bytes = null;
                                if (protoStr[strLen - 1] == '\0')
                                {
                                    bytes = Convert.FromBase64String(protoStr.Substring(0, strLen - 1));
                                }
                                else
                                {
                                    bytes = Convert.FromBase64String(protoStr);
                                }
                                msg.m_ProtoData = Encoding.Decode(t, bytes);
                            }
                        }
                        else
                        {
                            string jsonStr = msgStr.Substring(ix + 1);
                            int    strLen  = jsonStr.Length;
                            Type   type    = GetMessageType(id);
                            if (strLen > 0 && jsonStr[strLen - 1] == '\0')
                            {
                                msg = JsonConvert.DeserializeObject(jsonStr.Substring(0, strLen - 1), type) as JsonMessage;
                            }
                            else
                            {
                                msg = JsonConvert.DeserializeObject(jsonStr, type) as JsonMessage;
                            }
                        }
                        if (msg != null)
                        {
                            msg.m_ID = id;
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogSys.Log(LOG_TYPE.ERROR, "[Exception] DecodeJsonMessage:{0}\n{1}", ex.Message, ex.StackTrace);
                }
            }
            return(msg);
        }