private async Task ProcessClientCallsAsync()

in src/endpointmanager/EndpointManager.cs [220:305]


        private async Task ProcessClientCallsAsync(ISocket socket)
        {
            _log.Info($"Client connected to Endpoint manager");
            try
            {
                // Send handshake
                await socket.SendWithEndMarkerAsync(Constants.EndpointManager.SocketHandshake);

                // Wait for request
                var request = await socket.ReadUntilEndMarkerAsync();
                var apiName = JsonHelpers.DeserializeObject<EndpointManagerRequest>(request).ApiName;
                _operationContext.CorrelationId = JsonHelpers.DeserializeObject<EndpointManagerRequest>(request).CorrelationId + LoggingConstants.CorrelationIdSeparator + LoggingUtils.NewId();

                if (!Enum.TryParse(apiName, out Constants.EndpointManager.ApiNames apiRequest))
                {
                    var apiNotRecogizedResult = new EndpointManagerResult() { IsSuccess = false, ErrorMessage = $"API '{apiName}' not recognized by {Constants.EndpointManager.ProcessName}" };
                    await SendJsonOverSocketAsync(socket, apiNotRecogizedResult);
                    return;
                }

                // Execute the request and send a response back
                EndpointManagerResult result;
                switch (apiRequest)
                {
                    case Constants.EndpointManager.ApiNames.AddHostsFileEntry:
                        var addHostsFileEntryRequest = JsonHelpers.DeserializeObject<EndpointManagerRequest<AddHostsFileEntryArgument>>(request);
                        result = this.InvokeWithExceptionHandler(() => _hostsFileManager.Add(addHostsFileEntryRequest.Argument.WorkloadNamespace, addHostsFileEntryRequest.Argument.Entries));
                        break;

                    case Constants.EndpointManager.ApiNames.AllocateIP:
                        var allocateIpRequest = JsonHelpers.DeserializeObject<EndpointManagerRequest<AllocateIPArgument>>(request);
                        result = this.InvokeWithExceptionHandler<IEnumerable<EndpointInfo>>(() => _ipManager.AllocateIPs(allocateIpRequest.Argument.Endpoints, addRoutingRules: true, _cancellationToken));
                        break;

                    case Constants.EndpointManager.ApiNames.DisableService:
                        var disableServiceRequest = JsonHelpers.DeserializeObject<EndpointManagerRequest<DisableServiceArgument>>(request);
                        result = this.InvokeWithExceptionHandler(() => disableServiceRequest.Argument.ServicePortMappings.ExecuteForEach(mapping => DisableService(mapping)));
                        break;

                    case Constants.EndpointManager.ApiNames.FreeIP:
                        var freeIPRequest = JsonHelpers.DeserializeObject<EndpointManagerRequest<FreeIPArgument>>(request);
                        result = this.InvokeWithExceptionHandler(() => _ipManager.FreeIPs(freeIPRequest.Argument.IPAddresses, _hostsFileManager, removeRoutingRules: true, _cancellationToken));
                        break;

                    case Constants.EndpointManager.ApiNames.KillProcess:
                        var killProcessRequest = JsonHelpers.DeserializeObject<EndpointManagerRequest<KillProcessArgument>>(request);
                        result = this.InvokeWithExceptionHandler(() => killProcessRequest.Argument.ProcessPortMappings.ExecuteForEach(mapping => KillProcess(mapping)));
                        break;

                    case Constants.EndpointManager.ApiNames.Ping:
                        RegisterClientPing();
                        result = new EndpointManagerResult { IsSuccess = true };
                        break;

                    case Constants.EndpointManager.ApiNames.SystemCheck:
                        result = this.InvokeWithExceptionHandler<EndpointManagerSystemCheckMessage>(() => SystemCheck());
                        break;

                    case Constants.EndpointManager.ApiNames.Stop:
                        result = this.InvokeWithExceptionHandler(() => _shutdownCts.Cancel());
                        break;

                    case Constants.EndpointManager.ApiNames.Version:
                        RegisterClientPing();
                        result = new EndpointManagerResult<string>
                        {
                            IsSuccess = true,
                            Value = this._assemblyMetadataProvider.AssemblyVersion
                        };
                        break;

                    default:
                        throw new NotSupportedException($"API '{apiRequest}' not yet supported by {Constants.EndpointManager.ProcessName}");
                }
                await SendJsonOverSocketAsync(socket, result);
            }
            catch (Exception e)
            {
                _log.Exception(e);
                if (socket != null && socket.Connected)
                {
                    var result = new EndpointManagerResult() { IsSuccess = false, ErrorMessage = e.Message };
                    await SendJsonOverSocketAsync(socket, result);
                }
            }
        }