예제 #1
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Background service is started...");

            var address = IPAddress.Parse(_config.ServerIp);

            if (!await _client.ConnectAsync(new IPEndPoint(address, _config.ServerPort), cancellationToken))
            {
                _logger.LogError("Failed to connect to the target server.");
            }

            _client.PackageHandler += async(sender, package) =>
            {
                switch (package.Key)
                {
                case CommandKey.Execute:
                    var command = Command.Parser.ParseFrom(Encoding.UTF8.GetBytes(package.Content));
                    await Execute(command.OperatorId, command.Content);

                    break;

                case CommandKey.Connected:
                    _logger.LogInformation("Connected");
                    break;

                case CommandKey.Pong:
                    var unix = long.Parse(package.Content);
                    _pongTime = DateTimeOffset.FromUnixTimeMilliseconds(unix).UtcDateTime;
                    break;

                default:
                    _logger.LogWarning($"Unknown command:{package.Key}");
                    break;
                }
            };

            _client.StartReceive();

            await _client.SendAsync(Encoding.UTF8.GetBytes($"{CommandKey.Connect} {ClientType.Agent.ToString()}{Package.Terminator}"));

            _pingTimer      = new Timer(SendPing, null, TimeSpan.Zero, TimeSpan.FromSeconds(_pingIntervalSecond));
            _checkPingTimer = new Timer(CheckPong, null, TimeSpan.Zero, TimeSpan.FromSeconds(_checkPingIntervalSecond));

            _client.Closed += async(sender, args) =>
            {
                if (!_shouldReconnect)
                {
                    return;
                }

                _logger.LogError("Connection disconnect.");

                Thread.Sleep(2000);

                if (!await _client.ConnectAsync(new IPEndPoint(address, _config.ServerPort), cancellationToken))
                {
                    _logger.LogError("Failed to reconnect to the target server.");
                }
                else
                {
                    _client.StartReceive();
                    _logger.LogInformation("Connection reconnect.");
                }
            };
        }
예제 #2
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Socket Service is starting.");

            var address = IPAddress.Parse(_serverIp);

            if (!await _client.ConnectAsync(new IPEndPoint(address, _serverPort), cancellationToken))
            {
                _logger.LogError("Failed to connect to the socket server.");
            }

            _client.PackageHandler += async(sender, package) =>
            {
                switch (package.Key)
                {
                case CommandKey.Connected:
                    _logger.LogInformation("Connected");
                    break;

                case CommandKey.Output:
                    var index      = package.Content.IndexOf(' ');
                    var operatorId = package.Content.Substring(0, index);
                    var content    = package.Content.Substring(index + 1, package.Content.Length - index - 1);
                    await _socketHub.Clients.Groups(operatorId).ReceiveMessage(content + Package.Terminator);

                    break;

                case CommandKey.Pong:
                    var unix = long.Parse(package.Content);
                    _pongTime = DateTimeOffset.FromUnixTimeMilliseconds(unix).UtcDateTime;
                    break;

                default:
                    _logger.LogError($"Unknown command:{package.Key}");
                    break;
                }
            };

            _client.StartReceive();

            await _client.SendAsync(Encoding.UTF8.GetBytes($"{CommandKey.Connect} {ClientType.Web.ToString()}{Package.Terminator}"));

            _pingTimer      = new Timer(SendPing, null, TimeSpan.Zero, TimeSpan.FromSeconds(_pingIntervalSecond));
            _checkPingTimer = new Timer(CheckPong, null, TimeSpan.Zero, TimeSpan.FromSeconds(_checkPingIntervalSecond));

            _client.Closed += async(sender, args) =>
            {
                if (!_shouldReconnect)
                {
                    return;
                }

                Thread.Sleep(2000);

                if (!await _client.ConnectAsync(new IPEndPoint(address, _serverPort), cancellationToken))
                {
                    _logger.LogError("Failed to connect to the socket server.");
                }
                else
                {
                    _client.StartReceive();
                }
            };
        }