/// <summary>
        /// 释放数据
        /// protected virtual for non-sealed class; private for sealed class.
        /// </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.clientSocket != null)
                {
                    this.clientSocket.Shutdown(SocketShutdown.Both);
                    this.clientSocket.Close();
                    this.clientSocket = null;
                    IsConnected       = false;
                }
                if (this.sendEventArgsPool != null)
                {
                    this.sendEventArgsPool.Clear();
                    this.sendEventArgsPool = null;
                }
            }

            this.disposed = true;
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="bufferSize">Buffer size of 发送的数据</param>
        public AsyncSocketClientPool(int bufferSize = 2048, int poolMax = 10)
        {
            ProtocolType = ProtocolType.Tcp;
            SetKeepAliveValues();
            NoDelay         = true;
            this.bufferSize = bufferSize;

            receiveEventArgs            = new SocketAsyncEventArgs();
            receiveEventArgs.Completed += ReceiveCompleted;
            receiveEventArgs.SetBuffer(new byte[bufferSize], 0, bufferSize);

            this.sendEventArgsPool = new AsyncSocketEventArgsPool();
            SocketAsyncEventArgs readWriteEventArg;

            for (int i = 0; i < poolMax; i++)
            {
                readWriteEventArg            = new SocketAsyncEventArgs();
                readWriteEventArg.Completed += SendCompleted;
                readWriteEventArg.UserToken  = null;
                this.sendEventArgsPool.Push(readWriteEventArg);
            }
        }
예제 #3
0
        public SocketListener(ISocketListenerSettings theSocketListenerSettings, ILog log)
        {
            this._socketListenerSettings = theSocketListenerSettings;
            this._log = log;
            _log.WriteLine("SocketListener constructor");
            //Allocate memory for buffers. We are using a separate buffer space for
            //receive and send, instead of sharing the buffer space, like the Microsoft
            //example does.
            this._theBufferManager = new BufferManager(this._socketListenerSettings.BufferSize * this._socketListenerSettings.NumberOfSaeaForRecSend * this._socketListenerSettings.OpsToPreAllocate,
                                                       this._socketListenerSettings.BufferSize * this._socketListenerSettings.OpsToPreAllocate);

            this._poolOfRecSendEventArgs = new AsyncSocketEventArgsPool(this._socketListenerSettings.NumberOfSaeaForRecSend, log);

            // Create a semaphore that can satisfy up to maxConnectionCount concurrent requests.
            _acceptedClientsSemaphore = new Semaphore(this._socketListenerSettings.MaxConnections, this._socketListenerSettings.MaxConnections);

            _sendingQueue = new BlockingCollection <MessageData>();

            _sendMessageWorker = new Thread(new ThreadStart(SendQueueMessage));

            Init();

            _waitSendEvent = new AutoResetEvent(false);
        }