Exemple #1
0
 /// <summary>
 /// 向客户端发送回复消息
 /// </summary>
 /// <param name="message">回复消息</param>
 /// <returns></returns>
 public virtual OperationReturn SendMessage(ReturnMessage message)
 {
     return(SendMessage(GetMessageHead(), message));
 }
Exemple #2
0
        private void DoServerMessage(MessageHead head, string strMessage)
        {
            try
            {
                int             msgEncoding = head.Encoding;
                ReturnMessage   retMessage  = null;
                NotifyMessage   norMessage  = null;
                OperationReturn optReturn;
                int             intValue;
                string[]        list;
                switch (msgEncoding)
                {
                case (int)MessageEncoding.None:
                case (int)MessageEncoding.UTF8String:
                    switch (head.Type)
                    {
                    case (int)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 (int)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 (int)MessageEncoding.UTF8XML:
                    switch (head.Type)
                    {
                    case (int)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 (int)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;
                }
                switch (head.Type)
                {
                case (int)MessageType.Response:
                    if (retMessage != null)
                    {
                        OnReturnMessageReceived(retMessage);
                    }
                    break;

                case (int)MessageType.Notify:
                    if (norMessage != null)
                    {
                        OnNotifyMessageReceived(norMessage);
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                OnDebug(LogMode.Error, string.Format("DoServerMessage fail.\t{0}", ex.Message));
            }
        }
Exemple #3
0
        /// <summary>
        /// 向客户端发送回复消息
        /// </summary>
        /// <param name="head">消息头</param>
        /// <param name="message">回复消息</param>
        public virtual OperationReturn SendMessage(MessageHead head, ReturnMessage message)
        {
            OperationReturn optReturn = new OperationReturn();

            optReturn.Result = true;
            optReturn.Code   = 0;
            try
            {
                message.SessionID = mSessionID;
                head.Type         = (int)MessageType.Response;
                head.Command      = message.Command;
                switch (head.Encoding)
                {
                case (int)MessageEncoding.None:
                case (int)MessageEncoding.UTF8String:
                    var    ret        = message;
                    string strMessage = string.Empty;
                    //Result
                    strMessage += string.Format("{0}{1}", ret.Result ? "1" : "0", ConstValue.SPLITER_CHAR);
                    //Code
                    strMessage += string.Format("{0}{1}", ret.Code, ConstValue.SPLITER_CHAR);
                    //SessionID
                    strMessage += string.Format("{0}{1}", ret.SessionID, ConstValue.SPLITER_CHAR);
                    //Command
                    strMessage += string.Format("{0}{1}", ret.Command, ConstValue.SPLITER_CHAR);
                    //Message
                    strMessage += string.Format("{0}{1}", ret.Message, ConstValue.SPLITER_CHAR);
                    //Data
                    strMessage += string.Format("{0}{1}", ret.Data, ConstValue.SPLITER_CHAR);
                    //ListData
                    string strListData = string.Empty;
                    for (int i = 0; i < ret.ListData.Count; i++)
                    {
                        string msg = ret.ListData[i];
                        if (i < ret.ListData.Count - 1)
                        {
                            strListData += string.Format("{0}{1}", msg, ConstValue.SPLITER_CHAR_2);
                        }
                        else
                        {
                            strListData += string.Format("{0}", msg);
                        }
                    }
                    strMessage += string.Format("{0}", strListData);
                    SendMessage(head, strMessage);
                    break;

                case (int)MessageEncoding.UTF8XML:
                    optReturn = XMLHelper.SeriallizeObject(message);
                    if (!optReturn.Result)
                    {
                        OnDebug(LogMode.Error,
                                string.Format("Seralize message fail.\t{0}\t{1}", optReturn.Code, optReturn.Message));
                        return(optReturn);
                    }
                    SendMessage(head, optReturn.Data.ToString());
                    break;

                default:
                    string strLog = string.Format("MessageEncoding type not support.\t{0}", head.Encoding);
                    OnDebug(LogMode.Error, strLog);
                    optReturn.Result  = false;
                    optReturn.Code    = Defines.RET_NOT_IMPLIMENT;
                    optReturn.Message = strLog;
                    break;
                }
            }
            catch (Exception ex)
            {
                OnDebug(LogMode.Error, string.Format("SendMessage fail.\t{0}", ex.Message));
                optReturn.Result  = false;
                optReturn.Code    = Defines.RET_FAIL;
                optReturn.Message = ex.Message;
            }
            return(optReturn);
        }