// 弹出消息 private bool PopMsgFrmRcvBuffer() { if (IsReadClose()) { return(true); } byte[] readBuffer = null; Int32 readOffset = 0; Int32 readLength = 0; UInt16 msgCommand = 0; do { // 获得可读数据 if (!rcvDataBuffer.GetRcvMessage(ref msgCommand, ref readBuffer, ref readOffset, ref readLength, ref setSecurityPolicy)) { return(false); } if (readBuffer != null && readLength > 0) { // 从流中读取数据写入消息对象 MsgBufferMgr.GetInstance().PushNetMessage(msgCommand, readBuffer, readOffset, readLength, (INetSession)this); // 移动读取位置(数据头 + 数据长度) rcvDataBuffer.ReadData(null, readLength + NetGlobalData.GetInstance().GetMsgHeadSize()); } else { return(true); } }while(true); }
// 压入数据 public bool PushSendData(UInt16 msgCommand, byte[] dataBuffer, Int32 bufferOffset, UInt16 bufferLength) { if (GetFreeSpace() < (bufferLength + NetGlobalData.GetInstance().GetMsgHeadSize())) { Trace.Assert(false, "PushSendData so long"); return(false); } //msgHead.msgCommand = msgCommand; msgHead.msgLength = bufferLength; //msgHead.msgUseType = (byte)NETMSGUSETYPE.appUseMsg; byte[] msgHeadBuffer = new byte[NetGlobalData.GetInstance().GetMsgHeadSize()]; if (msgHeadBuffer == null) { return(false); } /*if (!Utilitys.StructToBytes<NetMsgHead>(msgHead, msgHeadBuffer, NetGlobalData.GetInstance().GetMsgHeadSize())) * return false;*/ // 写入数据头 //if (!WriteData(msgHeadBuffer, 0, msgHeadBuffer.Length, ENCRYPTOPT.codeEncryptNull)) // return false; byte[] abyte0 = Utilitys.LittleEndianToBytes32Bit(dataBuffer.Length); //for(int i = 0; i<bs) if (!WriteData(abyte0, 0, abyte0.Length, ENCRYPTOPT.codeEncryptNull)) { return(false); } // 写入数据 if (!WriteData(dataBuffer, bufferOffset, bufferLength, ENCRYPTOPT.codeEncryptNull)) { return(false); } return(true); }
// 获得单个消息 // 发生错误时返回 false,上层需要处理 public bool GetRcvMessage(ref UInt16 msgCommand, ref byte[] msgBuffer, ref Int32 bufferOffset, ref Int32 msgLength, ref bool setSecurityPolicy) { msgCommand = 0; msgLength = 0; msgBuffer = null; bufferOffset = 0; // 判断是否设置安全策略 /*if (!setSecurityPolicy) * { * Int32 policyMsgLength = Encoding.ASCII.GetBytes(policySecurity).Length; * if (GetDataLength() > policyMsgLength) * { * Trace.Assert(false, "Security policy msg length err"); * return false; * } * * if (GetDataLength() < policyMsgLength) * return true; * else * { * byte[] policyMsg = new byte[GetDataLength()]; * Buffer.BlockCopy(GetDataBuffer(), GetReadPosition(), policyMsg, 0, GetDataLength()); * if (Encoding.Default.GetString(policyMsg) == policySecurity) * { * setSecurityPolicy = true; * return true; * } * else * return false; * } * }*/ if (GetDataLength() <= NetGlobalData.GetInstance().GetMsgHeadSize()) { return(true); } if (!ReadyReadData(NetGlobalData.GetInstance().GetMsgHeadSize())) { Trace.Assert(false, "ReadyReadData failed"); return(false); } msgHead.Reset(); /*if (!Utilitys.BytesToStruct<NetMsgHead>(GetDataBuffer(), GetReadPosition(), NetGlobalData.GetInstance().GetMsgHeadSize(), ref msgHead)) * { * Trace.Assert(false, "BytesToStruct failed"); * return false; * }*/ if (!Utilitys.BytesToStruct <NetMsgHead2>(GetDataBuffer(), GetReadPosition(), NetGlobalData.GetInstance().GetMsgHeadSize(), ref msgHead)) { Trace.Assert(false, "BytesToStruct failed"); return(false); } //is zip if (msgHead.msgCompressed == 0x01) { msgCommand = 1; } //byte[] bytes = GetDataBuffer() ; msgHead.msgLength = Utilitys.BytesToBigEndian32Bit(GetDataBuffer(), GetReadPosition()); // 检查消息的长度有效性 if (msgHead.msgLength > (UInt16)NETGLOBALDATA.maxSendPackLength) { Trace.Assert(false, "msgHead.msgLength is too long"); return(false); } // TODO<szk>:添加检查效验值 // 检查数据是否足够长 if ((msgHead.msgLength + NetGlobalData.GetInstance().GetMsgHeadSize()) > GetDataLength()) { return(true); } // 准备读取消息+头 if (!ReadyReadData(msgHead.msgLength + NetGlobalData.GetInstance().GetMsgHeadSize())) { Trace.Assert(false, "ReadyReadData failed"); return(false); } // TODO<szk>:添加解密 Trace.Assert(GetReadPosition() >= 0, "Read Position is err"); msgBuffer = GetDataBuffer(); bufferOffset = GetReadPosition() + NetGlobalData.GetInstance().GetMsgHeadSize(); msgLength = msgHead.msgLength; //msgCommand = msgHead.msgCommand; return(true); }
// 压入发送数据 public bool PushSendData(UInt16 msgCommand, byte[] dataBuffer, Int32 bufferOffset, UInt16 bufferLength) { SendDataBuffer sendBuffer = null; lock (sendBufferListLock) { if (sendBufferList.Count() > 0) { sendBuffer = sendBufferList[sendBufferList.Count() - 1]; if (sendBuffer != null && sendBuffer.GetFreeSpace() < (bufferLength + NetGlobalData.GetInstance().GetMsgHeadSize())) { sendBuffer = null; } } if (sendBuffer == null) { sendBuffer = DataBufferPool.GetInstance().MallocSendBuffer(); if (sendBuffer == null) { return(false); } // 添加新的 Buffer sendBufferList.Add(sendBuffer); } return(sendBuffer.PushSendData(msgCommand, dataBuffer, bufferOffset, bufferLength)); } }