示例#1
0
 public bool UF_SetBodyBuffer(byte[] buffer, int size)
 {
     if (buffer == null)
     {
         return(false);
     }
     m_BodyBuffer.UF_clear();
     m_BodyBuffer.UF_write(buffer, size);
     return(true);
 }
示例#2
0
 private int UF_ReceiveBuffer(CBytesBuffer readbuffer)
 {
     //采用Poll 检测,非阻塞
     if (readbuffer != null && m_State == NETCONNECT_STATE.OPENED && m_Socket.UF_Poll(SelectMode.SelectRead))
     {
         try{
             int readsize = m_Socket.UF_Receive(m_ReceiveBytes);
             //	UnityEngine.Debug.Log (string.Format ("CORE--->> Receive Socket Buffer: {0}", readsize));
             if (readsize > 0)
             {
                 //写入到readbuffer中
                 readbuffer.UF_write(m_ReceiveBytes, readsize);
                 return(readsize);
             }
             else
             {
                 UF_ChangeConnectState(NETCONNECT_STATE.CLOSED);
                 Debugger.UF_Error(string.Format("Socket readable size is zero,check if socket is break| {0}", this.hostPort));
             }
         }catch (SocketException se) {
             UF_ChangeConnectState(NETCONNECT_STATE.CLOSED);
             Debugger.UF_Error(string.Format("SocketException At HostPort:  {0}", this.hostPort));
             Debugger.UF_Exception(se);
         }
     }
     return(0);
 }
示例#3
0
        public static int UF_writeuint32(CBytesBuffer buffer, uint u32)
        {
            byte[] _tmp = BitConverter.GetBytes(u32);
#if BIG_ENDIAN
            UF_Reverse(_tmp);
                        #endif
            return(buffer.UF_write(_tmp, 4));
        }
示例#4
0
 public static int UF_writeuint8(CBytesBuffer buffer, byte u8)
 {
     if (buffer == null)
     {
         throw new Exception("writing buffer is null");
     }
     return(buffer.UF_write(u8));
 }
示例#5
0
        public static int UF_writeuint16(CBytesBuffer buffer, ushort u16)
        {
            byte[] _tmp = BitConverter.GetBytes(u16);
#if BIG_ENDIAN
            UF_Reverse(_tmp);
                        #endif
            return(buffer.UF_write(_tmp, 2));
        }
示例#6
0
        public static int UF_writenumber(CBytesBuffer buffer, ulong data)
        {
            //			if (data == 0)
            //			{
            //				byte type = BIT_0;
            //				return buffer.write(0);
            //			}
            if (data < VAL_8_L)
            {
                //奇怪的处理方式。。。。
                if (data < VAL_8_S)
                {
                    buffer.UF_write((byte)data);
                    return(1);
                }
                else
                {
                    buffer.UF_write(BIT_8);
                    buffer.UF_write((byte)data);
                    return(2);
                }
                //				return buffer.write(BitConverter.GetBytes(data), 1);
            }
            else if (data < VAL_16)
            {
                buffer.UF_write(BIT_16);
                byte[] _tmp = BitConverter.GetBytes((ushort)data);
#if BIG_ENDIAN
                UF_Reverse(_tmp);
                                #endif
                return(buffer.UF_write(_tmp, 2));
            }
            else if (data < VAL_32)
            {
                buffer.UF_write(BIT_32);
                byte[] _tmp = BitConverter.GetBytes((uint)data);
#if BIG_ENDIAN
                UF_Reverse(_tmp);
                                #endif
                return(buffer.UF_write(_tmp, 4));
            }
            else
            {
                buffer.UF_write(BIT_64);
                byte[] _tmp = BitConverter.GetBytes(data);
#if BIG_ENDIAN
                UF_Reverse(_tmp);
                                #endif
                return(buffer.UF_write(_tmp, 8));
            }
        }
示例#7
0
        /// <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);
        }
示例#8
0
        public static int writeuint64(CBytesBuffer buffer, string u64)
        {
            UInt64 data = Convert.ToUInt64(u64);

            byte[] _tmp = BitConverter.GetBytes(data);
#if BIG_ENDIAN
            UF_Reverse(_tmp);
                        #endif
            return(buffer.UF_write(_tmp, 8));
        }
示例#9
0
 public static int UF_writesize(CBytesBuffer buffer, int size)
 {
     if (buffer == null)
     {
         throw new Exception("writing buffer is null");
     }
     if (size < 0xff)
     {
         return(buffer.UF_write((byte)size));
     }
     else if (size < 0xffff)
     {
         buffer.UF_write(255);
         buffer.UF_write(BitConverter.GetBytes((ushort)size), 2);
         return(3);
     }
     else
     {
         buffer.UF_write(new byte[] { 255, 255, 255 }, 3);
         buffer.UF_write(BitConverter.GetBytes((uint)size), 4);
         return(7);
     }
 }
示例#10
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);
        }
示例#11
0
        public static int UF_writestring(CBytesBuffer buffer, string str)
        {
            if (str == null)
            {
                str = string.Empty;
            }
            //first:write in the size type;
            if (buffer == null)
            {
                throw new Exception("writing buffer is null");
            }
            byte[] _tmp_str = Encoding.UTF8.GetBytes(str);
            int    _len     = _tmp_str.Length;

            //			writesize(buffer,_len);
            UF_writenumber(buffer, (ulong)_len);
            //second:write int the string data

            return(buffer.UF_write(_tmp_str, _len));
        }
示例#12
0
 public static void UF_writebytes(CBytesBuffer buffer, byte[] bytes)
 {
     //writenumber (buffer, bytes.Length);
     buffer.UF_write(bytes, bytes.Length);
 }