示例#1
0
        static Pool <T> CreatePool <T>(TcpServicePoolType poolType, int count, Func <T> activator)
        {
            switch (poolType)
            {
            case TcpServicePoolType.Cache:
            case TcpServicePoolType.DemandCache:
                return(new CachedPool <T>(count, activator, TcpServicePoolType.DemandCache == poolType));

            case TcpServicePoolType.Cyclic:
            case TcpServicePoolType.DemandCyclic:
                return(new CyclicPool <T>(count, activator, TcpServicePoolType.DemandCyclic == poolType));

            default:
                throw new NotImplementedException(poolType.ToString());
            }
        }
示例#2
0
        static Pool <SocketAsyncEventArgs> CreateSAEABufferPool(int count, int bufferSize, TcpServicePoolType poolType)
        {
            if (poolType == TcpServicePoolType.Cyclic)
            {
                var buffer = new byte[bufferSize * count];
                var offset = 0;

                return(new CyclicPool <SocketAsyncEventArgs>(count, () =>
                {
                    var eventArgs = new SocketAsyncEventArgs();
                    eventArgs.SetBuffer(buffer, offset, bufferSize);
                    offset += bufferSize;
                    return eventArgs;
                }, false));
            }

            if (poolType == TcpServicePoolType.Cache)
            {
                var buffer = new byte[bufferSize * count];
                var offset = 0;

                return(new CachedPool <SocketAsyncEventArgs>(count, () =>
                {
                    var eventArgs = new SocketAsyncEventArgs();
                    if (offset != count)
                    {
                        eventArgs.SetBuffer(buffer, offset, bufferSize);
                        offset += bufferSize;
                    }
                    else
                    {
                        eventArgs.SetBuffer(new byte[bufferSize], 0, bufferSize);
                    }
                    return eventArgs;
                }, false));
            }

            if (poolType == TcpServicePoolType.DemandCyclic)
            {
                return(new CyclicPool <SocketAsyncEventArgs>(count, () =>
                {
                    var eventArgs = new SocketAsyncEventArgs();
                    var buffer = new byte[bufferSize];
                    eventArgs.SetBuffer(buffer, 0, buffer.Length);
                    return eventArgs;
                }, true));
            }

            if (poolType == TcpServicePoolType.DemandCache)
            {
                return(new CyclicPool <SocketAsyncEventArgs>(count, () =>
                {
                    var eventArgs = new SocketAsyncEventArgs();
                    var buffer = new byte[bufferSize];
                    eventArgs.SetBuffer(buffer, 0, buffer.Length);
                    return eventArgs;
                }, true));
            }

            throw new NotImplementedException(poolType.ToString());
        }