Example #1
0
        public Server(ServerBuilderOptions options,
                      ILogger logger,
                      IServiceProvider serviceProvider,
                      IPacketHandlers packetHandlers,
                      ITcpConnections tcpConnections,
                      IServerPacketProcessor serverPacketProcessor,
                      ITcpSocketListenerFactory tcpSocketListenerFactory,
                      IUdpSocketListenerFactory udpSocketListenerFactory,
                      IBufferManager bufferManager,
                      IServerInformation serverInformation,
                      IPacketSerialiser packetSerialiser)
        {
            this.tcpConnections    = tcpConnections;
            this.serverInformation = serverInformation;
            this.packetSerialiser  = packetSerialiser;
            bufferManager.InitBuffer();

            if (options.TcpPort > 0)
            {
                this.TcpListener = tcpSocketListenerFactory.Create();
            }

            if (options.UdpPort > 0)
            {
                this.UdpListener = udpSocketListenerFactory.Create();
            }

            Task.Factory.StartNew(() =>
            {
                while (this.serverInformation.IsRunning)
                {
                    if (this.eventArgs == null)
                    {
                        this.eventArgs = new ServerInformationEventArgs();
                    }

                    this.eventArgs.ProcessedTcpPackets =
                        serverInformation.ProcessedTcpPackets;
                    this.eventArgs.InvalidTcpPackets =
                        serverInformation.InvalidTcpPackets;
                    this.eventArgs.ProcessedUdpPackets =
                        serverInformation.ProcessedUdpPackets;
                    this.eventArgs.InvalidUdpPackets =
                        serverInformation.InvalidUdpPackets;
                    this.eventArgs.TcpConnections = tcpConnections.GetConnections()
                                                    .Count;

                    this.ServerInformationUpdated?.Invoke(this, this.eventArgs);

                    this.serverInformation.ProcessedTcpPackets = 0;
                    this.serverInformation.InvalidTcpPackets   = 0;
                    this.serverInformation.ProcessedUdpPackets = 0;
                    this.serverInformation.InvalidUdpPackets   = 0;

                    Thread.Sleep(10000);
                }
            });
        }
Example #2
0
        /// <summary>
        /// Initializes the server by preallocating reusable buffers and
        /// context objects.  These objects do not need to be preallocated
        /// or reused, but it is done this way to illustrate how the API can
        /// easily be used to create reusable objects to increase server performance.
        /// </summary>
        public virtual void Init(SocketSettings settings)
        {
            _settings = settings;

            // Allocates one large byte buffer which all I/O operations use a piece of.  This guards against memory fragmentation
            IBufferManager bufferManager = _bufferManagerFactory.Create();

            bufferManager.InitBuffer(_settings.BufferSize * _settings.MaxConnections * 2, _settings.BufferSize);
            _communicationChannelsPool = new GenericPool <ICommunicationChannel>(_settings.MaxConnections);

            for (int i = 0; i < _settings.MaxConnections; i++)
            {
                ICommunicationChannel communicationChannel = _applicationContext.Container.Resolve <ICommunicationChannel>();
                communicationChannel.SocketClosedEvent += ClientHandler_SocketClosedEvent;
                communicationChannel.Init(bufferManager, _packetsHandler);
                _communicationChannelsPool.Push(communicationChannel);
            }
        }
Example #3
0
        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            // Allocates one large byte buffer which all I/O operations use a piece of.  This gaurds
            // against memory fragmentation
            _bufferManager.InitBuffer();
            ClientList = new List <UserToken>();
            // preallocate pool of SocketAsyncEventArgs objects

            for (int i = 0; i < _maxConnectNum; i++)
            {
                var readWriteEventArg = new SocketAsyncEventArgs();
                readWriteEventArg.Completed += IO_Completed;
                readWriteEventArg.UserToken  = new UserToken();

                // assign a byte buffer from the buffer pool to the SocketAsyncEventArg object
                _bufferManager.SetBuffer(readWriteEventArg);
                // add SocketAsyncEventArg to the pool
                _pool.Push(readWriteEventArg);
            }
        }