Example #1
0
        /// <summary>
        /// 
        /// </summary>
        public SocketAsyncClient(IPEndPoint remoteEndPoint, int bufferSize, AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
        {
            _remoteEndPoint = remoteEndPoint;
            _client = new Socket(addressFamily, socketType, protocolType);
            socketObject = new SocketObject(Guid.NewGuid(), _client);
            int maxConnection = 1;
            int numOfSaeaForRecSend = maxConnection * OpsToPreAlloc;
            _readWritePool = new SocketAsyncPool(numOfSaeaForRecSend);
            int numSize = numOfSaeaForRecSend * bufferSize;
            _bufferManager = new BufferManager(numSize, bufferSize);
            _bufferManager.InitBuffer();

            _saeaProxy = new SocketAsyncEventArgsProxy(bufferSize);
            _saeaProxy.ReceiveCompleted += OnReceiveCompleted;
            _saeaProxy.SendCompleted += OnSendCompleted;
            _saeaProxy.ClosedHandle += OnSocketClosing;

            SocketAsyncEventArgs saea;
            for (int i = 0; i < numOfSaeaForRecSend; i++)
            {
                saea = _saeaProxy.CreateNewSaea();
                _bufferManager.SetBuffer(saea);
                saea.UserToken = new DataToken(saea.Offset);
                _readWritePool.Push(saea);
            }
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ZyGames.Framework.RPC.Sockets.SocketListener"/> class.
        /// </summary>
        /// <param name="socketSettings">Socket settings.</param>
        public SocketListener(SocketSettings socketSettings)
        {
            this.socketSettings = socketSettings;
            this.prefixHandler = new PrefixHandler();
            this.messageHandler = new MessageHandler();

            this.bufferManager = new BufferManager(this.socketSettings.BufferSize * this.socketSettings.NumOfSaeaForRecSend, this.socketSettings.BufferSize);

            this.ioEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.NumOfSaeaForRecSend);
            this.acceptEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.MaxAcceptOps);
            this.maxConnectionsEnforcer = new Semaphore(this.socketSettings.MaxConnections, this.socketSettings.MaxConnections);
            Init();
            expireTimer = new Timer(CheckExpire, null, socketSettings.ExpireInterval, socketSettings.ExpireInterval);
        }
Example #3
0
        /// <summary>
        /// 
        /// </summary>
        public SocketListener(SocketSettings settings)
        {
            _settings = settings;
            _sessioinListen = new CacheListener("__SOCKET_SESSION_POOL", 10, OnRemoveSession);
            _resetEvent = new AutoResetEvent[1];
            _resetEvent[0] = new AutoResetEvent(false);

            _sessionPool = new SocketSessionPool();
            _readWritePool = new SocketAsyncPool(_settings.NumOfSaeaForRecSend);
            _acceptPool = new SocketAsyncPool(_settings.MaxConnection);
            int numSize = settings.NumOfSaeaForRecSend * settings.BufferSize;
            _bufferManager = new BufferManager(numSize, settings.BufferSize);
            _saeaProxy = new SocketAsyncEventArgsProxy(settings.BufferSize);
            _saeaProxy.ReceiveCompleted += OnReceiveCompleted;
            _saeaProxy.SendCompleted += OnSendCompleted;
            _saeaProxy.ClosedHandle += OnSocketClosing;
            Init();
        }
Example #4
0
 /// <summary>
 /// 释放
 /// </summary>
 /// <param name="disposing"></param>
 protected virtual void DoDispose(bool disposing)
 {
     if (disposing)
     {
         _sessioinListen.Stop();
         _acceptPool.Dispose();
         _acceptPool = null;
         _bufferManager = null;
         _readWritePool.Dispose();
         _readWritePool = null;
         _sessionPool = null;
         _saeaProxy = null;
         _settings = null;
         if (_listenSocket != null)
         {
             try
             {
                 _listenSocket.Close();
             }
             catch { }
         }
         //清理托管对象
         GC.SuppressFinalize(this);
     }
     //清理非托管对象
 }
Example #5
0
        /// <summary>
        /// Initializes a new instance
        /// </summary>
        /// <param name="socketSettings">Socket settings.</param>
        /// <param name="requestHandler"></param>
        public SocketListener(SocketSettings socketSettings, RequestHandler requestHandler)
        {
            this.socketSettings = socketSettings;
            this.requestHandler = requestHandler;
            this.bufferManager = new BufferManager(this.socketSettings.BufferSize * this.socketSettings.NumOfSaeaForRecSend, this.socketSettings.BufferSize);

            this.ioEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.NumOfSaeaForRecSend);
            this.acceptEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.MaxAcceptOps);
            this.maxConnectionsEnforcer = new Semaphore(this.socketSettings.MaxConnections, this.socketSettings.MaxConnections);
            Init();
        }