예제 #1
0
        private void DoServerMessage(MessageHead head, string strMessage)
        {
            try
            {
                MessageEncoding msgEncoding = head.Encoding;
                ReturnMessage   retMessage  = null;
                NotifyMessage   norMessage  = null;
                OperationReturn optReturn;
                int             intValue;
                string[]        list;
                switch (msgEncoding)
                {
                case MessageEncoding.None:
                case MessageEncoding.UTF8String:
                    switch (head.Type)
                    {
                    case MessageType.Response:
                        retMessage = new ReturnMessage();
                        list       = strMessage.Split(new[] { ConstValue.SPLITER_CHAR }, StringSplitOptions.None);
                        if (list.Length > 0)
                        {
                            retMessage.Result = list[0] == "1";
                        }
                        if (list.Length > 1)
                        {
                            if (int.TryParse(list[1], out intValue))
                            {
                                retMessage.Code = intValue;
                            }
                        }
                        if (list.Length > 2)
                        {
                            retMessage.SessionID = list[2];
                        }
                        if (list.Length > 3)
                        {
                            if (int.TryParse(list[3], out intValue))
                            {
                                retMessage.Command = intValue;
                            }
                        }
                        if (list.Length > 4)
                        {
                            retMessage.Message = list[4];
                        }
                        if (list.Length > 5)
                        {
                            retMessage.Data = list[5];
                        }
                        if (list.Length > 6)
                        {
                            string   strListData = list[6];
                            string[] listData    = strListData.Split(new[] { ConstValue.SPLITER_CHAR_2 },
                                                                     StringSplitOptions.None);
                            for (int i = 0; i < listData.Length; i++)
                            {
                                retMessage.ListData.Add(listData[i]);
                            }
                        }
                        break;

                    case MessageType.Notify:
                        norMessage = new NotifyMessage();
                        list       = strMessage.Split(new[] { ConstValue.SPLITER_CHAR }, StringSplitOptions.None);
                        if (list.Length > 0)
                        {
                            norMessage.SessionID = list[0];
                        }
                        if (list.Length > 1)
                        {
                            if (int.TryParse(list[1], out intValue))
                            {
                                norMessage.Command = intValue;
                            }
                        }
                        if (list.Length > 2)
                        {
                            norMessage.Data = list[2];
                        }
                        if (list.Length > 3)
                        {
                            string   strListData = list[3];
                            string[] listData    = strListData.Split(new[] { ConstValue.SPLITER_CHAR_2 },
                                                                     StringSplitOptions.None);
                            for (int i = 0; i < listData.Length; i++)
                            {
                                norMessage.ListData.Add(listData[i]);
                            }
                        }
                        break;
                    }
                    break;

                case MessageEncoding.UTF8XML:
                    switch (head.Type)
                    {
                    case MessageType.Response:
                        optReturn = XMLHelper.DeserializeObject <ReturnMessage>(strMessage);
                        if (!optReturn.Result)
                        {
                            OnDebug(LogMode.Error,
                                    string.Format("Deserialize ReturnMessage fail.\t{0}\t{1}", optReturn.Code,
                                                  optReturn.Message));
                            return;
                        }
                        retMessage = optReturn.Data as ReturnMessage;
                        if (retMessage == null)
                        {
                            OnDebug(LogMode.Error, string.Format("ReturnMessage is null"));
                            return;
                        }
                        break;

                    case MessageType.Notify:
                        optReturn = XMLHelper.DeserializeObject <NotifyMessage>(strMessage);
                        if (!optReturn.Result)
                        {
                            OnDebug(LogMode.Error,
                                    string.Format("Deserialize NotifyMessage fail.\t{0}\t{1}", optReturn.Code,
                                                  optReturn.Message));
                            return;
                        }
                        norMessage = optReturn.Data as NotifyMessage;
                        if (norMessage == null)
                        {
                            OnDebug(LogMode.Error, string.Format("NotifyMessage is null"));
                            return;
                        }
                        break;
                    }
                    break;

                default:
                    OnDebug(LogMode.Error, string.Format("Encoding invalid.\t{0}", mMsgEncoding));
                    return;
                }
                string msg;
                switch (head.Type)
                {
                case MessageType.Response:
                    if (retMessage != null)
                    {
                        switch (retMessage.Command)
                        {
                        case (int)RequestCode.NCWelcome:
                            msg = retMessage.LogInfo();
                            OnDebug(LogMode.Info, string.Format("WelcomeMessage received.\t{0}", msg));
                            break;
                        }
                        OnReturnMessageReceived(retMessage);
                    }
                    break;

                case MessageType.Notify:
                    if (norMessage != null)
                    {
                        OnNotifyMessageReceived(norMessage);
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                OnDebug(LogMode.Error, string.Format("DoServerMessage fail.\t{0}", ex.Message));
            }
        }