// 获取数据,然后压缩加密 public void getSocketSendData() { //m_socketSendBA.m_startTest = false; m_socketSendBA.clear(); // 获取完数据,就解锁 #if NET_MULTHREAD using (MLock mlock = new MLock(m_writeMutex)) #endif { //m_socketSendBA.writeBytes(m_sendTmpBA.dynBuff.buff, 0, (uint)m_sendTmpBA.length); //m_sendTmpBA.clear(); // 一次全部取出来发送出去 //m_socketSendBA.writeBytes(m_sendTmpBuffer.circuleBuffer.buff, 0, (uint)m_sendTmpBuffer.circuleBuffer.size); //m_sendTmpBuffer.circuleBuffer.clear(); // 一次仅仅获取一个消息发送出去,因为每一个消息的长度要填写加密补位后的长度 if (m_sendTmpBuffer.popFront()) // 弹出一个消息,如果只有一个消息,内部会重置变量 { m_socketSendBA.writeBytes(m_sendTmpBuffer.headerBA.dynBuff.buff, 0, m_sendTmpBuffer.headerBA.length); // 写入头 m_socketSendBA.writeBytes(m_sendTmpBuffer.msgBodyBA.dynBuff.buff, 0, m_sendTmpBuffer.msgBodyBA.length); // 写入消息体 } } #if MSG_COMPRESS || MSG_ENCRIPT m_socketSendBA.setPos(0); CompressAndEncryptEveryOne(); // CompressAndEncryptAllInOne(); #endif m_socketSendBA.position = 0; // 设置指针 pos //m_socketSendBA.m_startTest = true; }
protected void UnCompressAndDecryptAllInOne() { #if MSG_ENCRIPT m_rawBuffer.msgBodyBA.decrypt(m_cryptContext, 0); #endif #if MSG_COMPRESS m_rawBuffer.headerBA.setPos(0); uint msglen = 0; m_rawBuffer.headerBA.readUnsignedInt32(ref msglen); if ((msglen & MsgCV.PACKET_ZIP) > 0) { m_rawBuffer.msgBodyBA.uncompress(); } #endif #if !MSG_COMPRESS && !MSG_ENCRIPT m_unCompressHeaderBA.clear(); m_unCompressHeaderBA.writeUnsignedInt32(m_rawBuffer.msgBodyBA.length); m_unCompressHeaderBA.position = 0; #endif #if NET_MULTHREAD using (MLock mlock = new MLock(m_readMutex)) #endif { #if !MSG_COMPRESS && !MSG_ENCRIPT m_msgBuffer.circuleBuffer.pushBackBA(m_unCompressHeaderBA); // 保存消息大小字段 #endif m_msgBuffer.circularBuffer.pushBackBA(m_rawBuffer.msgBodyBA); // 保存消息大小字段 } }
public void send(bool bnet = true) { m_tmpData.clear(); m_tmpData.writeUnsignedInt32(m_sendData.length); // 填充长度 if (bnet) // 从 socket 发送出去 { #if NET_MULTHREAD using (MLock mlock = new MLock(m_writeMutex)) #endif { //m_sendTmpBA.writeUnsignedInt(m_sendData.length); // 写入头部长度 //m_sendTmpBA.writeBytes(m_sendData.dynBuff.buff, 0, m_sendData.length); // 写入内容 m_sendTmpBuffer.circularBuffer.pushBackBA(m_tmpData); m_sendTmpBuffer.circularBuffer.pushBackBA(m_sendData); } } else // 直接放入接收消息缓冲区 { #if NET_MULTHREAD #endif { //m_tmpData.clear(); //m_tmpData.writeUnsignedInt(m_sendData.length); // 填充长度 m_msgBuffer.circularBuffer.pushBackBA(m_tmpData); // 保存消息大小字段 m_msgBuffer.circularBuffer.pushBackBA(m_sendData); // 保存消息大小字段 } } }
public void notifyAll() { using (MLock mlock = new MLock(m_pMMutex)) { if (m_canEnterWait) // 如果 m_canEnterWait == false,必然不能进入等待 { m_canEnterWait = false; m_pMEvent.Set(); // 唤醒线程 } } }
public ByteBuffer getMsg() { #if NET_MULTHREAD using (MLock mlock = new MLock(m_readMutex)) #endif { if (m_msgBuffer.popFront()) { return(m_msgBuffer.msgBodyBA); } } return(null); }
// 消息格式 // |------------- 加密的整个消息 -------------------------------------| // |----4 Header----|-压缩的 body----|----4 Header----|-压缩的 body----| // | | | | | protected void UnCompressAndDecryptEveryOne() { #if MSG_ENCRIPT m_rawBuffer.msgBodyBA.decrypt(m_cryptContext, 0); #endif //#if MSG_COMPRESS //m_rawBuffer.headerBA.setPos(0); // 这个头目前没有用,是客户端自己添加的,服务器发送一个包,就认为是一个完整的包 //m_rawBuffer.msgBodyBA.setPos(0); //uint msglen = m_rawBuffer.headerBA.readUnsignedInt(); //if ((msglen & DataCV.PACKET_ZIP) > 0) //{ // m_rawBuffer.msgBodyBA.uncompress(); //} //#endif m_rawBuffer.msgBodyBA.setPos(0); uint msglen = 0; while (m_rawBuffer.msgBodyBA.bytesAvailable >= 4) { m_rawBuffer.msgBodyBA.readUnsignedInt32(ref msglen); // 读取一个消息包头 if (msglen == 0) // 如果是 0 ,就说明最后是由于加密补齐的数据 { break; } #if MSG_COMPRESS if ((msglen & MsgCV.PACKET_ZIP) > 0) { msglen &= (~MsgCV.PACKET_ZIP); // 去掉压缩标志位 Ctx.m_instance.m_logSys.log(string.Format("消息需要解压缩,消息未解压长度 {0}", msglen)); msglen = m_rawBuffer.msgBodyBA.uncompress(msglen); Ctx.m_instance.m_logSys.log(string.Format("消息需要解压缩,消息解压后长度 {0}", msglen)); } else #endif { Ctx.m_instance.m_logSys.log(string.Format("消息不需要解压缩,消息原始长度 {0}", msglen)); m_rawBuffer.msgBodyBA.position += msglen; } m_unCompressHeaderBA.clear(); m_unCompressHeaderBA.writeUnsignedInt32(msglen); // 写入解压后的消息的长度,不要写入 msglen ,如果压缩,再加密,解密后,再解压后的长度才是真正的长度 m_unCompressHeaderBA.position = 0; #if NET_MULTHREAD using (MLock mlock = new MLock(m_readMutex)) #endif { m_msgBuffer.circularBuffer.pushBackBA(m_unCompressHeaderBA); // 保存消息大小字段 m_msgBuffer.circularBuffer.pushBackArr(m_rawBuffer.msgBodyBA.dynBuff.buff, m_rawBuffer.msgBodyBA.position - msglen, msglen); // 保存消息大小字段 } Ctx.m_instance.m_logSys.log(string.Format("解压解密后消息起始索引 {0}, 消息长度 {1}, 消息 position 位置 {2}, 消息 size {3}", m_rawBuffer.msgBodyBA.position - msglen, msglen, m_rawBuffer.msgBodyBA.position, m_rawBuffer.msgBodyBA.length)); Ctx.m_instance.m_netDispList.addOneRevMsg(); // Test 读取消息头 // ByteBuffer buff = getMsg(); // stNullUserCmd cmd = new stNullUserCmd(); // cmd.derialize(buff); // Ctx.m_instance.m_logSys.log(string.Format("测试打印消息: byCmd = {0}, byParam = {1}", cmd.byCmd, cmd.byParam)); } }
public ByteBuffer getMsg() { #if NET_MULTHREAD using (MLock mlock = new MLock(m_readMutex)) #endif { if (m_msgBuffer.popFront()) { return m_msgBuffer.msgBodyBA; } } return null; }