Пример #1
0
        /// <summary>
        /// 释放
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (this.disposed)
            {
                return;
            }

            if (disposing)
            {
                if (this.listenSocket != null)
                {
                    this.listenSocket.Close();
                    this.listenSocket = null;
                }
                if (this.readPool != null)
                {
                    this.readPool.Clear();
                    this.readPool = null;
                }
                if (this.writePool != null)
                {
                    this.writePool.Clear();
                    this.writePool = null;
                }
                if (semaphore)
                {
                    this.maxNumberAcceptedClients.Close();
                }
            }

            this.disposed = true;
        }
Пример #2
0
        private void InitializePool()
        {
            this.bufferManager = new AsyncSocketEventArgsBufferManager(this.bufferSize * this.maxConnections * 2, this.bufferSize);
            this.readPool      = new AsyncSocketEventArgsPool();
            this.writePool     = new AsyncSocketEventArgsPool();
#if NET40
            this.tokens         = new ConcurrentDictionary <Guid, AsyncSocketTokenEventArgs>();
            this.singleIPTokens = new ConcurrentDictionary <IPAddress, AsyncSocketTokenEventArgs>();
#else
            this.tokens         = new Dictionary <Guid, AsyncSocketTokenEventArgs>();
            this.singleIPTokens = new Dictionary <IPAddress, AsyncSocketTokenEventArgs>();
#endif
            if (semaphore)
            {
                this.maxNumberAcceptedClients = new Semaphore(this.maxConnections, this.maxConnections);
            }
            this.bufferManager.InitBuffer();
            SocketAsyncEventArgs      readWriteEventArg;
            AsyncSocketTokenEventArgs token;

            // read Pool
            for (int i = 0; i < this.maxConnections; i++)
            {
                token = new AsyncSocketTokenEventArgs();
                token.ReadEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(this.IO_Completed);
                this.bufferManager.SetBuffer(token.ReadEventArgs);
                token.SetBuffer(token.ReadEventArgs.Buffer, token.ReadEventArgs.Offset);
                this.readPool.Push(token.ReadEventArgs);
            }

            // write Pool
            for (int i = 0; i < this.maxConnections; i++)
            {
                readWriteEventArg            = new SocketAsyncEventArgs();
                readWriteEventArg.Completed += new EventHandler <SocketAsyncEventArgs>(this.IO_Completed);
                readWriteEventArg.UserToken  = null;
                this.bufferManager.SetBuffer(readWriteEventArg);
                this.writePool.Push(readWriteEventArg);
            }
        }