/// <summary> /// 处理客户端消息 /// </summary> /// <param name="head">消息头</param> /// <param name="data">消息数据</param> protected virtual void DoClientMessage(MessageHead head, byte[] data) { try { byte[] temp = data; if (head.Encryption > 0) { //解密数据 if (EncryptObject == null) { OnDebug(LogMode.Error, string.Format("EncryptObject is null.")); return; } temp = EncryptObject.DecryptBytes(data, head.Encryption); } if (head.Type == (int)MessageType.Request) { string strMsg = Encoding.UTF8.GetString(temp); DoClientMessage(head, strMsg); } } catch (Exception ex) { OnDebug(LogMode.Error, string.Format("DoClientMessage fail.\t{0}", ex.Message)); } }
/// <summary> /// 生成消息头 /// </summary> /// <returns></returns> public MessageHead GetMessageHead() { MessageHead head = new MessageHead(); head.Flag = "US"; head.Encoding = mMsgEncoding; head.Encryption = mEncryption; head.State = (int)MessageState.LastPacket; return(head); }
private void OnMessageReceived(MessageHead head, byte[] data) { if (MessageReceived != null) { MessageReceivedEventArgs args = new MessageReceivedEventArgs(); args.Name = mSessionID; args.Head = head; args.Data = data; MessageReceived(this, args); } }
/// <summary> /// 发送字节流数据 /// </summary> /// <param name="head">消息头</param> /// <param name="data">消息数据</param> /// <returns></returns> public OperationReturn SendMessage(MessageHead head, byte[] data) { OperationReturn optReturn = new OperationReturn(); optReturn.Result = true; optReturn.Code = 0; try { if (mTcpClient.Connected) { byte[] temp = data; if (mEncryption > 0) { //数据加密 if (EncryptObject == null) { optReturn.Result = false; optReturn.Code = Defines.RET_OBJECT_NULL; optReturn.Message = string.Format("EncryptObject is null"); return(optReturn); } temp = EncryptObject.EncryptBytes(data, mEncryption); } int size = temp.Length; head.Size = size; byte[] bufferHead = Converter.Struct2Bytes(head); lock (mWriteLocker) { mStream.Write(bufferHead, 0, bufferHead.Length); mStream.Write(temp, 0, temp.Length); mStream.Flush(); } mLastActiveTime = DateTime.Now; } else { optReturn.Result = false; optReturn.Code = Defines.RET_NOT_CONNECTED; optReturn.Message = string.Format("TcpClient not connected"); } } 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; optReturn.Exception = ex; } return(optReturn); }
private void DoServerMessage(MessageHead head, byte[] data) { byte[] temp = data; if (head.Encryption > 0) { //解密数据 if (EncryptObject == null) { OnDebug(LogMode.Error, string.Format("EncryptObject is null.")); return; } temp = EncryptObject.DecryptBytes(data, head.Encryption); } OnMessageReceived(head, temp); if (head.Type == (int)MessageType.Response || head.Type == (int)MessageType.Notify) { string strMsg = Encoding.UTF8.GetString(temp); DoServerMessage(head, strMsg); } }
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)); } }
/// <summary> /// 向服务器发送请求消息 /// </summary> /// <param name="head">消息头</param> /// <param name="message">消息对象</param> public OperationReturn SendMessage(MessageHead head, RequestMessage message) { OperationReturn optReturn = new OperationReturn(); optReturn.Result = true; optReturn.Code = 0; try { message.SessionID = mSessionID; head.Type = (int)MessageType.Request; head.Command = message.Command; switch (head.Encoding) { case (int)MessageEncoding.None: case (int)MessageEncoding.UTF8String: var request = message; string strMessage = string.Empty; //Command strMessage += string.Format("{0}{1}", request.Command, ConstValue.SPLITER_CHAR); //SessionID strMessage += string.Format("{0}{1}", request.SessionID, ConstValue.SPLITER_CHAR); //Data strMessage += string.Format("{0}{1}", request.Data, ConstValue.SPLITER_CHAR); //ListData string strListData = string.Empty; for (int i = 0; i < request.ListData.Count; i++) { string msg = request.ListData[i]; if (i < request.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: OnDebug(LogMode.Error, string.Format("MessageEncoding type not support.\t{0}", head.Encoding)); 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); }
/// <summary> /// 向服务器发送消息 /// </summary> /// <param name="head">消息头</param> /// <param name="message">消息内容</param> public OperationReturn SendMessage(MessageHead head, string message) { byte[] data = Encoding.UTF8.GetBytes(message); return(SendMessage(head, data)); }
/// <summary> /// 处理客户端消息 /// </summary> /// <param name="head">消息头</param> /// <param name="strMessage">消息内容</param> protected virtual void DoClientMessage(MessageHead head, string strMessage) { try { int encoding = head.Encoding; RequestMessage request; OperationReturn optReturn; int intValue; if (head.Type == (int)MessageType.Request) { switch (encoding) { case (int)MessageEncoding.None: case (int)MessageEncoding.UTF8String: request = new RequestMessage(); string[] list = strMessage.Split(new[] { ConstValue.SPLITER_CHAR }, StringSplitOptions.None); if (list.Length > 0) { if (!int.TryParse(list[0], out intValue)) { OnDebug(LogMode.Error, string.Format("RequestMessage command invalid.\t{0}", list[0])); return; } request.Command = intValue; } if (list.Length > 1) { request.SessionID = list[1]; } if (list.Length > 2) { request.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++) { request.ListData.Add(listData[i]); } } break; case (int)MessageEncoding.UTF8XML: optReturn = XMLHelper.DeserializeObject <RequestMessage>(strMessage); if (!optReturn.Result) { OnDebug(LogMode.Error, string.Format("Deserialize RequestMessage fail.\t{0}\t{1}", optReturn.Code, optReturn.Message)); return; } request = optReturn.Data as RequestMessage; if (request == null) { OnDebug(LogMode.Error, string.Format("RequestMessage is null")); return; } break; default: OnDebug(LogMode.Error, string.Format("Encoding invalid.\t{0}", mMsgEncoding)); return; } OnDebug(LogMode.Info, string.Format("Receive command:{0}", ParseCommand(request.Command))); DoClientCommand(request); } } catch (Exception ex) { OnDebug(LogMode.Error, string.Format("DoClientMessage fail.\t{0}", ex.Message)); } }