Esempio n. 1
0
        private static void PipeClient_ServerMessage(NamedPipeConnection <string, string> connection, string message)
        {
            if (pipeCancellation != null)
            {
                return;
            }
            PipeCommandReceived?.Invoke(null, new PipeEventArgs(message));
            int res = -1;

            switch (message)
            {
            case PipeCommands.ExecuteScenario:
                pipeCancellation = new CancellationTokenSource();
                res = Scenario.Execute(pipeCancellation);
                break;

            case PipeCommands.LoopScenario:
                pipeCancellation = new CancellationTokenSource();
                res = Scenario.Loop(pipeCancellation);
                break;

            case PipeCommands.StopScenario:
                pipeCancellation.Cancel();
                break;

            default:
                break;
            }
            pipeCancellation = null;
            pipeClient.PushMessage(res.ToString());
        }
Esempio n. 2
0
        private void InitPipeClient()
        {
            try
            {
                _namedPipe.Connect(2000);
            }
            catch (TimeoutException)
            {
                if (ExitIfNoConnection)
                {
                    AppLogger.Log.Warn("No Pipe available. Closing the application.");
                    Environment.FailFast("No pipe available");
                }
                else
                {
                    throw;
                }
            }

            SendCommand(new PipeCommand(PipeCommandType.InitiateService,
                                        BitConverter.ToString(AppConfigs.PipeConfiguration.AesKeyBytes)));
            var cmd = PipeCommand.GetPipeCommand(_namedPipe);

            if (cmd == null)
            {
                throw new Exception("No handshake");
            }
            if (cmd.Type != PipeCommandType.InitiateService)
            {
                throw new Exception("Wrong Command: " + cmd.Type);
            }
            if (cmd.Data != AppConfigs.PipeConfiguration.AuthentificationString)
            {
                throw new Exception("Wrong Auth: " + cmd.Data);
            }
            _serverAuth = cmd.Auth;
            IsConnected = true;
            var task = new Task(() =>
            {
                while (_namedPipe.IsConnected)
                {
                    var pipeCmd = PipeCommand.GetPipeCommand(_namedPipe);
                    if (pipeCmd != null)
                    {
                        PipeCommandReceived?.Invoke(this, new PipeCommandReceivedEvent(pipeCmd));
                    }
                }
            });

            task.Start();
        }