예제 #1
0
        public static SocketAsyncEventArgs AcquireSocketArg()
        {
            SocketAsyncEventArgs socketAsyncEventArgs = ObjectPoolMgr.ObtainObject <SocketAsyncEventArgs>();

            CleanSocketArg(socketAsyncEventArgs);
            return(socketAsyncEventArgs);
        }
예제 #2
0
        public static void ReleaseSocketArg(SocketAsyncEventArgs arg)
        {
            //Interlocked.Increment(ref s_ReleasedArgs);
            //Interlocked.Decrement(ref s_OutstandingArgs);
            //Console.WriteLine("Releasing SocketAsyncEventArg {0}:{1}", s_OutstandingArgs, s_ReleasedArgs);

            ObjectPoolMgr.ReleaseObject <SocketAsyncEventArgs>(arg);
        }
예제 #3
0
        //static int s_AcquiredArgs;
        //static int s_ReleasedArgs;
        //static int s_OutstandingArgs;

        static SocketHelpers()
        {
            if (!ObjectPoolMgr.ContainsType <SocketAsyncEventArgs>())
            {
                ObjectPoolMgr.RegisterType <SocketAsyncEventArgs>(CreateSocketArg);

                // TODO: have the minimum value set in config or find an appropriate minimum.
                ObjectPoolMgr.SetMinimumSize <SocketAsyncEventArgs>(100);
            }
        }
예제 #4
0
        /// <summary>
        /// Sets the minimum number of hard references to be contained in the specified object pool.
        /// </summary>
        /// <param name="minSize">The minimum number of hard references to be contained in the specified object pool.</param>
        public static void SetMinimumSize <T>(int minSize) where T : class
        {
            long typePointer = ObjectPoolMgr.GetTypePointer <T>();

            if (!ObjectPoolMgr.Pools.ContainsKey(typePointer))
            {
                return;
            }
            ((ObjectPool <T>)ObjectPoolMgr.Pools[typePointer]).MinimumSize = minSize;
        }
예제 #5
0
 static SocketHelpers()
 {
     if (ObjectPoolMgr.ContainsType <SocketAsyncEventArgs>())
     {
         return;
     }
     ObjectPoolMgr.RegisterType(
         CreateSocketArg);
     ObjectPoolMgr.SetMinimumSize <SocketAsyncEventArgs>(100);
 }
예제 #6
0
        public static SocketAsyncEventArgs AcquireSocketArg()
        {
            //Interlocked.Increment(ref s_OutstandingArgs);
            //Interlocked.Increment(ref s_AcquiredArgs);
            //Console.WriteLine("Acquiring SocketAsyncEventArg {0}:{1}", s_OutstandingArgs, s_AcquiredArgs);
            SocketAsyncEventArgs args = ObjectPoolMgr.ObtainObject <SocketAsyncEventArgs>();

            CleanSocketArg(args);

            return(args);
        }
예제 #7
0
        /// <summary>Gets information about the specified object pool.</summary>
        /// <returns>An object of type <see cref="T:WCell.Util.ObjectPools.ObjectPoolInfo" /> if the function succeeded, otherwise an object with all values equal to 0 is returned.</returns>
        public static ObjectPoolInfo GetPoolInfo <T>() where T : class
        {
            long        typePointer = ObjectPoolMgr.GetTypePointer <T>();
            IObjectPool objectPool;

            if (ObjectPoolMgr.Pools.TryGetValue(typePointer, out objectPool))
            {
                return(((ObjectPool <T>)objectPool).Info);
            }
            return(new ObjectPoolInfo(0, 0));
        }
예제 #8
0
        /// <summary>Obtains an object from the specified object pool.</summary>
        /// <returns>If a lock could not be aquired on the object pool null is returned. Otherwise a hard reference to the object requested is returned.</returns>
        public static T ObtainObject <T>() where T : class
        {
            long        typePointer = ObjectPoolMgr.GetTypePointer <T>();
            IObjectPool objectPool;

            if (ObjectPoolMgr.Pools.TryGetValue(typePointer, out objectPool))
            {
                return(((ObjectPool <T>)objectPool).Obtain());
            }
            return(default(T));
        }
예제 #9
0
        /// <summary>Releases an object back into the object pool.</summary>
        /// <param name="obj">The object to be released.</param>
        public static void ReleaseObject <T>(T obj) where T : class
        {
            long        typePointer = ObjectPoolMgr.GetTypePointer <T>();
            IObjectPool objectPool;

            if (!ObjectPoolMgr.Pools.TryGetValue(typePointer, out objectPool))
            {
                return;
            }
            objectPool.Recycle((object)obj);
        }
예제 #10
0
        /// <summary>Registers an object pool with the specified type.</summary>
        /// <param name="func">A pointer to a function that creates new objects.</param>
        /// <returns>True if the type already exists or was registered successfully. False if locking the internal pool list timed out.</returns>
        /// <remarks>The function waits 3000 milliseconds to aquire the lock of the internal pool list.</remarks>
        public static bool RegisterType <T>(Func <T> func) where T : class
        {
            long typePointer = ObjectPoolMgr.GetTypePointer <T>();

            lock (typeof(ObjectPoolMgr))
            {
                if (!ObjectPoolMgr.Pools.ContainsKey(typePointer))
                {
                    ObjectPoolMgr.Pools.Add(typePointer, (IObjectPool) new ObjectPool <T>(func));
                    return(true);
                }
            }

            return(false);
        }
예제 #11
0
 /// <summary>Returns true if the specified type is registered.</summary>
 /// <typeparam name="T">The type to check registration with.</typeparam>
 /// <returns>True if the specified type is registered.</returns>
 public static bool ContainsType <T>()
 {
     return(ObjectPoolMgr.Pools.ContainsKey(ObjectPoolMgr.GetTypePointer <T>()));
 }
예제 #12
0
 public static void ReleaseSocketArg(SocketAsyncEventArgs arg)
 {
     ObjectPoolMgr.ReleaseObject(arg);
 }