/// <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); } }
/// <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); } //清理非托管对象 }
/// <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(); }