public void ServiceControlActionRequest(object sender, ServiceControlRequestEventArgs args) { _logger.LogTrace($"Service control request received, action: {args.Action}"); if (args.Action == "GetStatus") { args.IsSuccess = true; var response = new Dictionary <string, string>() { { "Timestamp", DateTime.Now.ToString("yyyyMMddHHmmss") }, { "Monitor", _probeMonitor.MonitorState.ToString() }, { "Sync", _syncManager.SyncState.ToString() }, { "Service", _managedService.ServiceState.ToString() } }; args.Responses.Add(response); _logger.LogTrace($"Service control response sent"); } }
private void HandleClient(TcpClient client, string token) { byte[] bytes = new byte[256]; string data = null; NetworkStream stream = client.GetStream(); int i; while (client.Connected && (i = stream.Read(bytes, 0, bytes.Length)) > 0) { data = Encoding.ASCII.GetString(bytes, 0, i); try { var action = GetActionFromInput(data); if (GetTokenFromInput(data) != token) { AnswerClient(stream, "Token error. Bye bye!"); } else { var args = new ServiceControlRequestEventArgs(action); ServiceControlRequest(this, args); var response = args.IsSuccess ? JsonConvert.SerializeObject(args.Responses) : "Action error. Bye bye!"; AnswerClient(stream, response); } } catch (Exception e) { throw e; } finally { client.Close(); } } }