/// <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)); } }
private string GenerateAuthCode() { string strSession = mSessionID; if (EncryptObject == null) { OnDebug(LogMode.Error, string.Format("No EncryptObject, can not generate AuthCode.")); return(string.Empty); } return(EncryptObject.EncryptString(strSession, (int)EncryptionMode.SHA256V00Hex)); }
/// <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); } }