/// <summary> /// 从缓存数据中读取包数据 /// 如果缓存数据中包含完整包数据,则从该缓存数据中取出,并读入到bodybuffer中 /// </summary> public bool UF_Read(CBytesBuffer rawBuffer) { m_TmpBufferToRead.UF_clear(); m_TmpBufferToRead.UF_write(rawBuffer); // CBytesBuffer tmpbuff = new CBytesBuffer(rawBuffer); CBytesBuffer tmpbuff = m_TmpBufferToRead; int packetsize = 0; if (tmpbuff.UF_getSize() < HEAD_SIZE) { //不是完整的协议数据包,等待下次读取 Debugger.UF_Warn(string.Format("NetPacket Buffer Size[{0}] not Long enought to read", tmpbuff.UF_getSize())); return(false); } //校验码比较 uint pMagic = CBytesConvert.UF_readuint32(tmpbuff); if (MAGIC != pMagic) { ///校验码不通过,丢弃该包缓存的全部数据 rawBuffer.UF_popBytes(rawBuffer.Length); Debugger.UF_Error(string.Format("discard package: magic<{0}> | MAGIC<{1}>,RawBuffer Clear", MAGIC, pMagic)); return(false); } //读出包头 this.id = (int)CBytesConvert.UF_readuint32(tmpbuff); this.retCode = (short)CBytesConvert.UF_readuint16(tmpbuff); this.corCode = (int)CBytesConvert.UF_readuint32(tmpbuff); this.size = (int)CBytesConvert.UF_readuint32(tmpbuff); packetsize += HEAD_SIZE; //包体不为0,读出包体 if (this.size > 0) { //buffer 比读出的size长 if (this.size > tmpbuff.UF_getSize()) { //不是完整的协议数据包,等待下次读取 return(false); } //写入到body中 this.m_BodyBuffer.UF_write(tmpbuff.Buffer, (int)this.size); packetsize += this.size; } //清空已经读取的RawBuffer rawBuffer.UF_popBytes(packetsize); return(true); }
public static long UF_readnumber(CBytesBuffer buffer) { if (buffer.UF_getSize() < 1) { return(0); } byte nNumberType; buffer.UF_read(out nNumberType); switch (nNumberType) { case BIT_8: { byte u8; buffer.UF_read(out u8); return((sbyte)u8); } case BIT_16: { byte[] u16; buffer.UF_read(out u16, 2); #if BIG_ENDIAN UF_Reverse(u16); #endif return(BitConverter.ToInt16(u16, 0)); } case BIT_32: { byte[] u32; buffer.UF_read(out u32, 4); #if BIG_ENDIAN UF_Reverse(u32); #endif return(BitConverter.ToInt32(u32, 0)); } case BIT_64: { byte[] u64; buffer.UF_read(out u64, 8); #if BIG_ENDIAN UF_Reverse(u64); #endif // return BitConverter.ToString(u64); return(BitConverter.ToInt64(u64, 0)); } default: { //返回自己 return((sbyte)nNumberType); } } }
private bool UF_SendBuffer(CBytesBuffer writeBuffer) { if (writeBuffer != null && m_State == NETCONNECT_STATE.OPENED && m_Socket.UF_Poll(SelectMode.SelectWrite)) { if (writeBuffer.UF_getSize() > 0) { //writeBuffer.reverse (); m_Socket.UF_Send(writeBuffer.Buffer, writeBuffer.UF_getSize()); return(true); } } else { UF_ChangeConnectState(NETCONNECT_STATE.CLOSED); Debugger.UF_Error(string.Format("Socket writable failed,check if socket is break| {0}", this.hostPort)); } return(false); }
static int UF_getCBytesBufferSize(IntPtr L) { CBytesBuffer buffer = UF_getCBytesBufferInPool(L); int size = buffer.UF_getSize(); LuaDLL.lua_pushnumber(L, size); return(1); }
/// <summary> /// 发送writebuffer数据到连接结点 /// </summary> private void UF_PollWriteBuffer() { if (m_WriteBuffer.UF_getSize() > 0) { if (UF_SendBuffer(this.m_WriteBuffer)) { this.m_WriteBuffer.UF_clear(); } } }
public int UF_write(CBytesBuffer cCBytesBuffer) { if (cCBytesBuffer != null) { m_Len = cCBytesBuffer.Length; mFreeLen = cCBytesBuffer.FreeLen; m_Buffer = new byte[m_Len]; cCBytesBuffer.Buffer.CopyTo(m_Buffer, 0); return(cCBytesBuffer.UF_getSize()); } else { ///抛出参数为空 throw new Exception("Parameter in Constructor is Null"); } // return 0; }
/// <summary> ///写入完整Raw协议数据,用于网络发送 ///处理大小包,小包<0xFF 大包>=0xFFFE ///如果包体body大于0xFFFE则分包 /// <returns>The raw buffer.</returns> /// <summary> public bool UF_Write(CBytesBuffer mRawBuffer) { if (mRawBuffer == null) { return(false); } size = m_BodyBuffer.UF_getSize(); CBytesConvert.UF_writeuint32(mRawBuffer, MAGIC); CBytesConvert.UF_writeuint32(mRawBuffer, (uint)id); CBytesConvert.UF_writeuint16(mRawBuffer, (ushort)retCode); CBytesConvert.UF_writeuint32(mRawBuffer, (uint)corCode); CBytesConvert.UF_writeuint32(mRawBuffer, (uint)size); //写入包体 mRawBuffer.UF_write(m_BodyBuffer.Buffer, size); return(true); }