コード例 #1
0
        public AsyncEventServer(IPAddress ipAddress, ushort port, IConsumer consumer, AsyncEventSettings settings)
            : base(ipAddress, port, consumer)
        {
            _clients             = new AsyncEventClientList <AsyncEventClient>();
            _settings            = new AsyncEventSettings(settings);
            _socketTimeout       = TimeSpan.FromSeconds(_settings.SocketTimeoutSeconds);
            _isRunning           = false;
            _isRunningLock       = new object();
            _unitOfOrderLock     = new object();
            _acceptedConnections = 0;
            _identity            = "";
            _unitOfOrders        = new int[_settings.MaxUnitOfOrder];
            if (!string.IsNullOrEmpty(_settings.Identity))
            {
                _identity = $"[{_settings.Identity}] ";
            }

            _acceptPool  = new ConcurrentStack <SocketAsyncEventArgs>();
            _receivePool = new ConcurrentStack <SocketAsyncEventArgs>();
            _sendPool    = new ConcurrentStack <SocketAsyncEventArgs>();

            _maxNumberAccepts         = new SemaphoreSlim(NumAccepts, NumAccepts);
            _maxNumberAcceptedClients = new SemaphoreSlim(_settings.MaxConnections, _settings.MaxConnections);
            _maxNumberSendOperations  = new SemaphoreSlim(_settings.NumSimultaneouslyWriteOperations,
                                                          _settings.NumSimultaneouslyWriteOperations);

            int bufferSize = _settings.BufferSize * _settings.MaxConnections +
                             _settings.BufferSize * _settings.NumSimultaneouslyWriteOperations;

            _buffer = new byte[bufferSize];
            int bufferOffset = 0;

            for (int i = 0; i < _settings.MaxConnections; i++)
            {
                SocketAsyncEventArgs readEventArgs = new SocketAsyncEventArgs();
                readEventArgs.Completed += Receive_Completed;
                readEventArgs.SetBuffer(_buffer, bufferOffset, _settings.BufferSize);
                _receivePool.Push(readEventArgs);
                bufferOffset += _settings.BufferSize;
            }

            for (int i = 0; i < _settings.NumSimultaneouslyWriteOperations; i++)
            {
                SocketAsyncEventArgs writeEventArgs = new SocketAsyncEventArgs();
                writeEventArgs.Completed += Send_Completed;
                writeEventArgs.UserToken  = new AsyncEventToken();
                writeEventArgs.SetBuffer(_buffer, bufferOffset, _settings.BufferSize);
                _sendPool.Push(writeEventArgs);
                bufferOffset += _settings.BufferSize;
            }

            for (int i = 0; i < NumAccepts; i++)
            {
                SocketAsyncEventArgs acceptEventArgs = new SocketAsyncEventArgs();
                acceptEventArgs.Completed += Accept_Completed;
                _acceptPool.Push(acceptEventArgs);
            }
        }
コード例 #2
0
 public AsyncEventSettings(AsyncEventSettings settings)
 {
     Identity       = settings.Identity;
     BufferSize     = settings.BufferSize;
     MaxConnections = settings.MaxConnections;
     NumSimultaneouslyWriteOperations = settings.NumSimultaneouslyWriteOperations;
     Retries        = settings.Retries;
     SocketSettings = new SocketSettings(settings.SocketSettings);
     MaxUnitOfOrder = settings.MaxUnitOfOrder;
     MaxSimultaneousSendsPerClient = settings.MaxSimultaneousSendsPerClient;
     SocketTimeoutSeconds          = settings.SocketTimeoutSeconds;
 }