Example #1
0
    /// <summary>
    /// 解析协议数据,将收到的网络数据组装成一个协议数据
    /// </summary>
    /// <param name="unpackFunc">用来解析数据的代码</param>
    /// <returns></returns>
    public System.Object ParseProtocolData(ref UnpackDelegate unpackFunc)
    {
        if (mSocket == null)
        {
            return(null);
        }

        if (mRecvBytes == mRecvBufferLen)
        {//缓冲区已经满了,自增长
            mRecvBufferLen += TdrPackageBufferLength;
            byte[] new_buffer = new byte[mRecvBufferLen];
            System.Buffer.BlockCopy(mRecvBuffer, 0, new_buffer, 0, mRecvBytes);

            mRecvBuffer = new_buffer;
            Debuger.Log("RecvBuffer auto grow : " + mRecvBufferLen);
        }

        int iRead = mSocket.Recv(mRecvBuffer, mRecvBytes, mRecvBufferLen - mRecvBytes);

        if (iRead == 0 && mRecvBytes == 0)
        {
            return(null);
        }

        mRecvBytes += iRead;

        System.Object obj = null;

        int iPkgLen = 0;

        obj = unpackFunc(mRecvBuffer, mRecvBytes, ref iPkgLen);

        if (iPkgLen > 0)
        {
            System.Buffer.BlockCopy(mRecvBuffer, iPkgLen, mRecvBuffer, 0, mRecvBufferLen - iPkgLen);
            mRecvBytes -= iPkgLen;
        }

        return(obj);
    }
        public static void Initialize()
        {
            if (initialized)
            {
                return;
            }
            isInitializing = true;
            int            maxBits        = 0;
            var            pObjAttr       = (typeof(emotitron.TestPackObject).GetCustomAttributes(typeof(PackObjectAttribute), false)[0] as PackObjectAttribute);
            var            defaultKeyRate = pObjAttr.defaultKeyRate;
            FastBitMask128 defReadyMask   = new FastBitMask128(TOTAL_FIELDS);
            int            fieldindex     = 0;

            SyncHalfFloatAttribute rotationPackAttr = (SyncHalfFloatAttribute)(typeof(emotitron.TestPackObject).GetField("rotation").GetCustomAttributes(typeof(SyncVarBaseAttribute), false)[0] as SyncHalfFloatAttribute);

            rotationPacker   = (rotationPackAttr as IPackSingle).Pack;
            rotationUnpacker = (rotationPackAttr as IPackSingle).Unpack;
            rotationPackAttr.Initialize(typeof(System.Single));
            if (rotationPackAttr.keyRate == KeyRate.UseDefault)
            {
                rotationPackAttr.keyRate = (KeyRate)defaultKeyRate;
            }
            if (rotationPackAttr.syncAs == SyncAs.Auto)
            {
                rotationPackAttr.syncAs = pObjAttr.syncAs;
            }
            if (rotationPackAttr.syncAs == SyncAs.Auto)
            {
                rotationPackAttr.syncAs = SyncAs.State;
            }
            if (rotationPackAttr.syncAs == SyncAs.Trigger)
            {
                defReadyMask[fieldindex] = true;
            }
            maxBits += 16; fieldindex++;

            SyncRangedIntAttribute intorobotoPackAttr = (SyncRangedIntAttribute)(typeof(emotitron.TestPackObject).GetField("intoroboto").GetCustomAttributes(typeof(SyncVarBaseAttribute), false)[0] as SyncRangedIntAttribute);

            intorobotoPacker   = (intorobotoPackAttr as IPackInt32).Pack;
            intorobotoUnpacker = (intorobotoPackAttr as IPackInt32).Unpack;
            intorobotoPackAttr.Initialize(typeof(System.Int32));
            if (intorobotoPackAttr.keyRate == KeyRate.UseDefault)
            {
                intorobotoPackAttr.keyRate = (KeyRate)defaultKeyRate;
            }
            if (intorobotoPackAttr.syncAs == SyncAs.Auto)
            {
                intorobotoPackAttr.syncAs = pObjAttr.syncAs;
            }
            if (intorobotoPackAttr.syncAs == SyncAs.Auto)
            {
                intorobotoPackAttr.syncAs = SyncAs.State;
            }
            if (intorobotoPackAttr.syncAs == SyncAs.Trigger)
            {
                defReadyMask[fieldindex] = true;
            }
            maxBits += 2; fieldindex++;

            packObjInfo = new PackObjectDatabase.PackObjectInfo(defReadyMask, Pack, Pack, Unpack, maxBits, PackFrame_TestPackObject.Factory, PackFrame_TestPackObject.Factory, PackFrame_TestPackObject.Apply, PackFrame_TestPackObject.Capture, PackFrame_TestPackObject.Copy, PackFrame_TestPackObject.SnapshotCallback, PackFrame_TestPackObject.Interpolate, PackFrame_TestPackObject.Interpolate, TOTAL_FIELDS);
            PackObjectDatabase.packObjInfoLookup.Add(typeof(emotitron.TestPackObject), packObjInfo);
            isInitializing = false;
            initialized    = true;
        }