Example #1
0
 public SockState(SocketAsyncEventArgs eventArgs, int bufferBlockLength, SocketAsyncEventArgsStack owner)
 {
     Pool           = owner;
     AsyncEventArgs = eventArgs;
     if (eventArgs != null)
     {
         BufferBlockOffset = eventArgs.Offset;
     }
     BufferBlockLength = bufferBlockLength;
 }
Example #2
0
        public static void Init(int bufferSize, int maxClients, int maxAcceptSockets)
        {
            maxClients *= 2; // each client has two sockets

            if (IsInitialized)
            {
                return;
            }

            IsInitialized = true;

            int total = bufferSize * ((maxClients * 2) + maxAcceptSockets + 3);

            m_Buffer = new BufferManager(total, bufferSize);

            m_PoolOfReadEventArgs   = new SocketAsyncEventArgsStack(maxClients + 1);
            m_PoolOfAcceptEventArgs = new SocketAsyncEventArgsStack(maxAcceptSockets + 1);
            m_PoolOfSendEventArgs   = new SocketAsyncEventArgsStack(maxClients + 1);

            // Allocate one large byte buffer block, which all I/O operations will
            //use a piece of. This gaurds against memory fragmentation.
            m_Buffer.InitBuffer();

            // Let's make 1 extra SAEA to spare for each operation. If we do that, then we have to
            // consider it when we specify the buffer block's size in SocketListener
            // constructor.

            int[] maxOps = new int[] { maxClients + 1, maxClients + 1, maxAcceptSockets + 1 };
            SocketAsyncEventArgsStack[] pools = new SocketAsyncEventArgsStack[] { m_PoolOfSendEventArgs, m_PoolOfReadEventArgs, m_PoolOfAcceptEventArgs };

            for (int x = 0; x < maxOps.Length; x++)
            {
                for (int i = 0; i < maxOps[x]; i++)
                {
                    SocketAsyncEventArgs args = new SocketAsyncEventArgs();

                    // assign a byte buffer from the buffer block to
                    //this particular SocketAsyncEventArg object
                    m_Buffer.SetBuffer(args);

                    SockState recState = new SockState(args, bufferSize, pools[x]);
                    recState.ID                = i + 1;
                    args.UserToken             = recState;
                    recState.BufferBlockOffset = args.Offset;

                    // add this SocketAsyncEventArg object to the pool.
                    pools[x].Push(args);
                }
            }
        }
        public static void Init(int bufferSize, int maxClients, int maxAcceptSockets)
        {
            maxClients *= 2; // each client has two sockets

            if (IsInitialized)
            {
                return;
            }

            IsInitialized = true;

            int total = bufferSize * ((maxClients * 2) + maxAcceptSockets + 3);
            m_Buffer = new BufferManager(total, bufferSize);

            m_PoolOfReadEventArgs = new SocketAsyncEventArgsStack(maxClients + 1);
            m_PoolOfAcceptEventArgs = new SocketAsyncEventArgsStack(maxAcceptSockets + 1);
            m_PoolOfSendEventArgs = new SocketAsyncEventArgsStack(maxClients + 1);

            // Allocate one large byte buffer block, which all I/O operations will
            //use a piece of. This gaurds against memory fragmentation.
            m_Buffer.InitBuffer();

            // Let's make 1 extra SAEA to spare for each operation. If we do that, then we have to
            // consider it when we specify the buffer block's size in SocketListener
            // constructor.

            int[] maxOps = new int[] { maxClients+1, maxClients+1, maxAcceptSockets+1 };
            SocketAsyncEventArgsStack[] pools = new SocketAsyncEventArgsStack[] { m_PoolOfSendEventArgs, m_PoolOfReadEventArgs, m_PoolOfAcceptEventArgs };

            for (int x = 0; x < maxOps.Length; x++)
            {
                for (int i = 0; i < maxOps[x]; i++)
                {
                    SocketAsyncEventArgs args = new SocketAsyncEventArgs();

                    // assign a byte buffer from the buffer block to
                    //this particular SocketAsyncEventArg object
                    m_Buffer.SetBuffer(args);

                    SockState recState = new SockState(args, bufferSize, pools[x]);
                    recState.ID = i+1;
                    args.UserToken = recState;
                    recState.BufferBlockOffset = args.Offset;

                    // add this SocketAsyncEventArg object to the pool.
                    pools[x].Push(args);
                }
            }
        }
Example #4
0
 static SocketAsyncEventArgsCache()
 {
     m_PoolOfReadEventArgs   = new SocketAsyncEventArgsStack(0);
     m_PoolOfAcceptEventArgs = new SocketAsyncEventArgsStack(0);
     m_PoolOfSendEventArgs   = new SocketAsyncEventArgsStack(0);
 }
 static SocketAsyncEventArgsCache()
 {
     m_PoolOfReadEventArgs = new SocketAsyncEventArgsStack(0);
     m_PoolOfAcceptEventArgs = new SocketAsyncEventArgsStack(0);
     m_PoolOfSendEventArgs = new SocketAsyncEventArgsStack(0);
 }