Esempio n. 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);
                }
            });
        }