Ejemplo n.º 1
0
        /// <summary>
        ///     Start the API Server
        /// </summary>
        public static void Start()
        {
            PacketLoader.LoadPackets();
            var config = Config.Load();
            var clientUpdateService = new UpdateService();

            clientUpdateService.Start();
            FileSearchService = new FileSearchService(Path.Combine(AppEnvironment.DataPath, "fileIndex.db"));
            FileSearchService.Start();
            CronJobService = new CronJobService(Path.Combine(AppEnvironment.DataPath, "jobs.json"), Path.Combine(AppEnvironment.DataPath, "scripts"));
            CronJobService.ConfigureJobs();
            var apiPort = config.TaskServer.TaskServerPort;

            AllClients         = new ConcurrentDictionary <Guid, AuthClient>();
            ScreenShareService = new ScreenShareService();
            var address         = NetworkService.GetAddress();
            var webCamPort      = config.Webcams.WebcamPort;
            var screenSharePort = config.ScreenShareService.ScreenSharePort;
            var endPoints       = new List <IPEndPoint>
            {
                new IPEndPoint(address, apiPort),
                new IPEndPoint(address, webCamPort),
                new IPEndPoint(address, screenSharePort)
            };
            var server = new WebSocketEventListener(endPoints, new WebSocketListenerOptions
            {
                PingTimeout              = TimeSpan.FromSeconds(2),
                NegotiationTimeout       = TimeSpan.FromSeconds(2),
                WebSocketSendTimeout     = TimeSpan.FromSeconds(2),
                WebSocketReceiveTimeout  = TimeSpan.FromSeconds(2),
                ParallelNegotiations     = Environment.ProcessorCount * 2,
                NegotiationQueueCapacity = 256,
                TcpBacklog        = 1000,
                OnHttpNegotiation = (request, response) =>
                {
                    if (request.Cookies["ConnectionId"] == null)
                    {
                        response.Cookies.Add(new Cookie("ConnectionId", Guid.NewGuid().ToString()));
                    }
                }
            });

            server.OnConnect          += HandleConnect;
            server.OnDisconnect       += HandleDisconnect;
            server.OnPlainTextMessage += HandlePlainTextMessage;
            server.OnEncryptedMessage += HandleEncryptedMessage;
            server.OnError            += HandleError;
            server.Start();
            Log("Api Server started at " + address);
            if (RunningAsService)
            {
                AgentClient = new UlteriusAgentClient();
                AgentClient.Start();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Start the API Server
        /// </summary>
        public static void Start()
        {
            _bufferPoolSize = 100 * _bufferSize;
            PacketLoader.LoadPackets();
            var config = Config.Load();
            var clientUpdateService = new UpdateService();

            clientUpdateService.Start();
            FileSearchService = new FileSearchService(Path.Combine(AppEnvironment.DataPath, "fileIndex.db"));
            FileSearchService.Start();
            CronJobService = new CronJobService(Path.Combine(AppEnvironment.DataPath, "jobs.json"), Path.Combine(AppEnvironment.DataPath, "scripts"));
            CronJobService.ConfigureJobs();
            var apiPort = config.TaskServer.TaskServerPort;

            AllClients         = new ConcurrentDictionary <Guid, AuthClient>();
            ScreenShareService = new ScreenShareService();
            var address         = NetworkService.GetAddress();
            var webCamPort      = config.Webcams.WebcamPort;
            var screenSharePort = config.ScreenShareService.ScreenSharePort;
            var listenEndPoints = new Uri[] {
                new Uri($"ws://{address}:{apiPort}"),
                new Uri($"ws://{address}:{webCamPort}"),
                new Uri($"ws://{address}:{screenSharePort}")
            };
            var options = new WebSocketListenerOptions
            {
                PingMode                 = PingMode.LatencyControl,
                NegotiationTimeout       = TimeSpan.FromSeconds(30),
                PingTimeout              = TimeSpan.FromSeconds(5),
                ParallelNegotiations     = 16,
                NegotiationQueueCapacity = 256,
                BufferManager            = BufferManager.CreateBufferManager(_bufferPoolSize, _bufferSize),
                Logger = NullLogger.Instance,
                HttpAuthenticationHandler = async(request, response) =>
                {
                    await Task.Delay(TimeSpan.FromMilliseconds(1));

                    if (request.Cookies["ConnectionId"] == null)
                    {
                        response.Cookies.Add(new Cookie("ConnectionId", Guid.NewGuid().ToString()));
                    }
                    return(true);
                }
            };

            options.Transports.ConfigureTcp(tcp =>
            {
                tcp.BacklogSize       = 1000; // max pending connections waiting to be accepted
                tcp.ReceiveBufferSize = _bufferSize;
                tcp.SendBufferSize    = _bufferSize;
                tcp.LingerState       = new LingerOption(true, 0);
                tcp.NoDelay           = true;
                tcp.IsAsync           = true;

                tcp.ReceiveTimeout = TimeSpan.FromSeconds(1);
                tcp.SendTimeout    = TimeSpan.FromSeconds(3);
            });

            var server = new WebSocketEventListener(listenEndPoints, options);

            server.OnConnect          += HandleConnect;
            server.OnDisconnect       += HandleDisconnect;
            server.OnPlainTextMessage += HandlePlainTextMessage;
            server.OnEncryptedMessage += HandleEncryptedMessage;
            server.OnError            += HandleError;
            server.Start();
            Log("Api Server started at " + address);
        }