Пример #1
0
 public void Recycle()
 {
     BytesPool.Recycle(m_buffer);
     m_bitsPointer = 0;
     m_buffer      = null;
     Pool.Recycle(this);
 }
Пример #2
0
        /// <summary>
        /// Returns an instance of NetworkBuffer, from the pool.
        /// </summary>
        /// <param name="size">The size of the underlayer array.</param>
        /// <returns></returns>
        public static Buffer Create(int size)
        {
            var buffer = Pool.Rent();

            buffer.m_buffer = BytesPool.Get(size);

            return(buffer);
        }
Пример #3
0
        public static byte[] SerializeObject <T>(T value, MessagePackFormatterOptions options)
        {
            var hGBytes = BytesPool.Rent();

            SerializeObject(value, hGBytes, options);

            var ret = hGBytes.ToArray();

            BytesPool.Return(hGBytes);

            return(ret);
        }
Пример #4
0
        SerializeAsync <T>(T value, Stream stream)
        {
            var hGBytes = BytesPool.Rent();

            Serialize(value, hGBytes);

            var bytes = hGBytes.ToArray();

            BytesPool.Return(hGBytes);

            await stream.WriteAsync(bytes, 0, bytes.Length);
        }
Пример #5
0
    public void RentAndDisposeTest()
    {
        var random    = new Random();
        var bytesPool = BytesPool.Create();

        for (int i = 0; i < 32; i++)
        {
            int size = random.Next(1, 1024 * 1024 * 32);
            using var buffer = bytesPool.Memory.Rent(size);
            Assert.True(buffer.Memory.Length >= size);
        }
    }
Пример #6
0
        SerializeObjectAsync <T>(T value, Stream stream, MessagePackFormatterOptions options)
        {
            var hGBytes = BytesPool.Rent();

            SerializeObject(value, hGBytes, options);

            var bytes = hGBytes.ToArray();

            BytesPool.Return(hGBytes);

            await stream.WriteAsync(bytes, 0, bytes.Length);
        }
Пример #7
0
        public static byte[] SerializeObject <T>(T value)
        {
            var hGBytes = BytesPool.Rent();

            SerializeObject(value, hGBytes);

            var ret = hGBytes.ToArray();

            BytesPool.Return(hGBytes);

            return(ret);
        }
Пример #8
0
        private void EnsureBufferSpace(int additionalSpaceInBits)
        {
            var length = m_buffer.Length;

            if (m_bitsPointer + additionalSpaceInBits > length << 3)
            {
                var tmpBuffer = m_buffer;
                var newBuffer = BytesPool.Get((int)(length * GrowingFactor) + 1);
                System.Buffer.BlockCopy(tmpBuffer, 0, newBuffer, 0, tmpBuffer.Length);
                BytesPool.Recycle(m_buffer);
                m_buffer = newBuffer;
            }
        }
Пример #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PixelArea{TColor}"/> class.
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="componentOrder">The component order.</param>
        /// <param name="padding">The number of bytes to pad each row.</param>
        public PixelArea(int width, int height, ComponentOrder componentOrder, int padding)
        {
            this.Width          = width;
            this.Height         = height;
            this.ComponentOrder = componentOrder;
            this.RowStride      = (width * GetComponentCount(componentOrder)) + padding;
            this.Length         = this.RowStride * height;
            this.Bytes          = BytesPool.Rent(this.Length);
            this.isBufferRented = true;
            this.pixelsHandle   = GCHandle.Alloc(this.Bytes, GCHandleType.Pinned);

            // TODO: Why is Resharper warning us about an impure method call?
            this.dataPointer = this.pixelsHandle.AddrOfPinnedObject();
            this.PixelBase   = (byte *)this.dataPointer.ToPointer();
        }
Пример #10
0
        /// <summary>
        /// Returns an instance of NetworkBuffer, from the pool.
        /// It is initialized with the passed data.
        /// </summary>
        /// <param name="data">The data to initialize the buffer.</param>
        /// <param name="createCopy">If true, data will be copied in a new array; else data will be used as internal buffer.</param>
        /// <returns></returns>
        public static Buffer Create(byte[] data, bool createCopy = true)
        {
            var buffer = Pool.Rent();

            if (createCopy)
            {
                buffer.m_buffer = BytesPool.Get(data.Length);
                System.Buffer.BlockCopy(data, 0, buffer.m_buffer, 0, data.Length);
            }
            else
            {
                buffer.m_buffer = data;
            }

            return(buffer);
        }
Пример #11
0
    public virtual void notifyConstructDone()
    {
        mGameFramework     = GameFramework.mGameFramework;
        mCommandSystem     = mGameFramework.getSystem(Typeof <CommandSystem>()) as CommandSystem;
        mAudioManager      = mGameFramework.getSystem(Typeof <AudioManager>()) as AudioManager;
        mGameSceneManager  = mGameFramework.getSystem(Typeof <GameSceneManager>()) as GameSceneManager;
        mCharacterManager  = mGameFramework.getSystem(Typeof <CharacterManager>()) as CharacterManager;
        mLayoutManager     = mGameFramework.getSystem(Typeof <LayoutManager>()) as LayoutManager;
        mKeyFrameManager   = mGameFramework.getSystem(Typeof <KeyFrameManager>()) as KeyFrameManager;
        mGlobalTouchSystem = mGameFramework.getSystem(Typeof <GlobalTouchSystem>()) as GlobalTouchSystem;
        mShaderManager     = mGameFramework.getSystem(Typeof <ShaderManager>()) as ShaderManager;
#if !UNITY_IOS && !NO_SQLITE
        mSQLite = mGameFramework.getSystem(Typeof <SQLite>()) as SQLite;
#endif
        mDataBase                = mGameFramework.getSystem(Typeof <DataBase>()) as DataBase;
        mCameraManager           = mGameFramework.getSystem(Typeof <CameraManager>()) as CameraManager;
        mResourceManager         = mGameFramework.getSystem(Typeof <ResourceManager>()) as ResourceManager;
        mApplicationConfig       = mGameFramework.getSystem(Typeof <ApplicationConfig>()) as ApplicationConfig;
        mFrameConfig             = mGameFramework.getSystem(Typeof <FrameConfig>()) as FrameConfig;
        mObjectPool              = mGameFramework.getSystem(Typeof <ObjectPool>()) as ObjectPool;
        mInputManager            = mGameFramework.getSystem(Typeof <InputManager>()) as InputManager;
        mSceneSystem             = mGameFramework.getSystem(Typeof <SceneSystem>()) as SceneSystem;
        mClassPool               = mGameFramework.getSystem(Typeof <ClassPool>()) as ClassPool;
        mClassPoolThread         = mGameFramework.getSystem(Typeof <ClassPoolThread>()) as ClassPoolThread;
        mListPool                = mGameFramework.getSystem(Typeof <ListPool>()) as ListPool;
        mListPoolThread          = mGameFramework.getSystem(Typeof <ListPoolThread>()) as ListPoolThread;
        mDictionaryPool          = mGameFramework.getSystem(Typeof <DictionaryPool>()) as DictionaryPool;
        mDictionaryPoolThread    = mGameFramework.getSystem(Typeof <DictionaryPoolThread>()) as DictionaryPoolThread;
        mBytesPool               = mGameFramework.getSystem(Typeof <BytesPool>()) as BytesPool;
        mBytesPoolThread         = mGameFramework.getSystem(Typeof <BytesPoolThread>()) as BytesPoolThread;
        mAndroidPluginManager    = mGameFramework.getSystem(Typeof <AndroidPluginManager>()) as AndroidPluginManager;
        mAndroidAssetLoader      = mGameFramework.getSystem(Typeof <AndroidAssetLoader>()) as AndroidAssetLoader;
        mHeadTextureManager      = mGameFramework.getSystem(Typeof <HeadTextureManager>()) as HeadTextureManager;
        mTimeManager             = mGameFramework.getSystem(Typeof <TimeManager>()) as TimeManager;
        mMovableObjectManager    = mGameFramework.getSystem(Typeof <MovableObjectManager>()) as MovableObjectManager;
        mEffectManager           = mGameFramework.getSystem(Typeof <EffectManager>()) as EffectManager;
        mTPSpriteManager         = mGameFramework.getSystem(Typeof <TPSpriteManager>()) as TPSpriteManager;
        mSocketFactory           = mGameFramework.getSystem(Typeof <SocketFactory>()) as SocketFactory;
        mSocketFactoryThread     = mGameFramework.getSystem(Typeof <SocketFactoryThread>()) as SocketFactoryThread;
        mPathKeyframeManager     = mGameFramework.getSystem(Typeof <PathKeyframeManager>()) as PathKeyframeManager;
        mEventSystem             = mGameFramework.getSystem(Typeof <EventSystem>()) as EventSystem;
        mStringBuilderPool       = mGameFramework.getSystem(Typeof <StringBuilderPool>()) as StringBuilderPool;
        mStringBuilderPoolThread = mGameFramework.getSystem(Typeof <StringBuilderPoolThread>()) as StringBuilderPoolThread;
#if USE_ILRUNTIME
        mILRSystem = mGameFramework.getSystem(Typeof <ILRSystem>()) as ILRSystem;
#endif
    }
Пример #12
0
        public static void SerializeObject <T>(T value, Stream stream, MessagePackFormatterOptions options)
        {
            var hGBytes = BytesPool.Rent();

            if ((options & ReferenceSerializerOptions) != 0)
            {
                SerializeObject <T, MessagePackSerializeModes.ReferenceMode>(value, hGBytes, options);
            }
            else
            {
                SerializeObject <T, MessagePackSerializeModes.StandardMode>(value, hGBytes, options);
            }

            hGBytes.WriteTo(stream);

            BytesPool.Return(hGBytes);
        }
        /// <summary>
        /// 以前 4 个字节作为头部协议号
        /// </summary>
        /// <param name="message"></param>
        /// <param name="msgId"></param>
        /// <returns></returns>
        public static byte[] GetSendBytes(object message, int msgId)
        {
            byte[] messageBytes = serializer.Serialize(message);
            var    tempBytes    = BitConverter.GetBytes(msgId);

            byte[] msgIdBytes = tempBytes.GetHeadBytes();
            byte[] finalBytes = BytesPool.Take(4 + messageBytes.Length);
            for (int i = 0; i < 4; i++)
            {
                finalBytes[i] = msgIdBytes[i];
            }

            for (int i = 0; i < messageBytes.Length; i++)
            {
                finalBytes[i + 4] = messageBytes[i];
            }

            return(finalBytes);
        }
        public byte[] Handle(byte[] receiveBytes)
        {
            // todo
            var bytes = BytesPool.Take(receiveBytes.Length + 4);
            //var bytes = new byte[receiveBytes.Length + 4];

            var lengthBytes = BitConverter.GetBytes(receiveBytes.Length);

            for (int i = 0; i < 4; i++)
            {
                bytes[i] = lengthBytes[3 - i];
            }

            for (int i = 0; i < receiveBytes.Length; i++)
            {
                bytes[i + 4] = receiveBytes[i];
            }

            return(bytes);
        }
        /// <summary>
        /// 调用消息对应的消息处理器
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="message"></param>
        private void InvokeRespHandler(int messageId, byte[] message)
        {
            var respHandler = respHandlerDic[messageId];

            var redicretId = -1;

            redicretId = RedirectResp.ContainsKey(messageId) ? RedirectResp[messageId] : messageId;

            if (!respHandlerDic.ContainsKey(redicretId))
            {
#if DEBUG
                Debug.LogError($"消息编号{messageId}的消息没有对应的答复处理器存在!");
#endif
                return;
            }

            var handler = respHandlerDic[redicretId];
            handler.ReceiveMessage(message);
            BytesPool.Restore(message);
        }
        /// <summary>
        /// 接收消息分发方法,在 Unity FixedUpdate 中调用
        /// </summary>
        private void MessageDispatch()
        {
            foreach (var channel in netModule.ChannelDic.Values)
            {
                if (channel.MessageStorage.ReceiveCount == 0)
                {
                    continue;
                }

                for (int i = 0; i < channel.MessageStorage.ReceiveCount; i++)
                {
                    var bytes             = channel.MessageStorage.GetReceiveMessage();
                    var messageIdBytes    = bytes.GetHeadBytes();
                    var messageId         = BitConverter.ToInt32(messageIdBytes, 0);
                    var messageBodyLength = bytes.Length - 4;
                    var finalBytes        = BytesPool.Take(messageBodyLength);
                    Buffer.BlockCopy(bytes, 4, finalBytes, 0, messageBodyLength);
                    BytesPool.Restore(bytes);
                    InvokeRespHandler(messageId, finalBytes);
                }
            }
        }
Пример #17
0
        /// <summary>
        /// 异步将 Stream 的内容缓存到 HGlobalCache 中。
        /// </summary>
        /// <param name="hGCache">HGlobalCache</param>
        /// <param name="stream">Stream</param>
        /// <param name="encoding">编码</param>
        /// <returns>返回缓冲的长度</returns>
        public static async Task ReadFromAsync(this HGlobalCache <char> hGCache, Stream stream, Encoding encoding)
        {
            var hGBytes = BytesPool.Rent();

            await hGBytes.ReadFromAsync(stream);

            var maxCharsCount = encoding.GetMaxCharCount(hGBytes.Count);

            if (maxCharsCount > hGCache.Capacity)
            {
                hGCache.Expand(maxCharsCount - hGCache.Capacity);
            }

            unsafe
            {
                hGCache.Count = encoding.GetChars(
                    hGBytes.GetPointer(),
                    hGBytes.Count,
                    hGCache.GetPointer(),
                    hGCache.Capacity);
            }

            BytesPool.Return(hGBytes);
        }
Пример #18
0
        /// <summary>
        /// 异步将 HGlobalCache 中的内容写入到流中。
        /// </summary>
        /// <param name="hGCache">HGlobalCache</param>
        /// <param name="stream">流</param>
        /// <param name="encoding">编码</param>
        public static async Task WriteToAsync(this HGlobalCache <char> hGCache, Stream stream, Encoding encoding)
        {
            var hGBytes = BytesPool.Rent();

            var maxBytesCount = encoding.GetMaxByteCount(hGCache.Count);

            if (maxBytesCount > hGBytes.Capacity)
            {
                hGBytes.Expand(maxBytesCount - hGBytes.Capacity);
            }

            unsafe
            {
                hGBytes.Count = encoding.GetBytes(
                    hGCache.GetPointer(),
                    hGCache.Count,
                    hGBytes.GetPointer(),
                    hGBytes.Capacity);
            }

            await hGBytes.WriteToAsync(stream);

            BytesPool.Return(hGBytes);
        }