static void Main(string[] args) { // Connect to DCS InterceptConnection preConnection = new InterceptConnection(); InterceptConnection postConnection = new InterceptConnection(); if (args.Length == 0) { preConnection.Connect(InterceptionMode.Pre).Wait(); postConnection.Connect(InterceptionMode.Post).Wait(); } else { preConnection.Connect(InterceptionMode.Pre, args[0]).Wait(); postConnection.Connect(InterceptionMode.Post, args[0]).Wait(); } Console.WriteLine("Connected!"); // Start two tasks waiting for codes to be received Task[] tasks = new [] { PrintIncomingCodes("pre", preConnection), PrintIncomingCodes("post", postConnection) }; Task.WaitAll(tasks); }
static async Task Main(string[] args) { // Connect to DCS using InterceptConnection preConnection = new InterceptConnection(); using InterceptConnection postConnection = new InterceptConnection(); using InterceptConnection executedConnection = new InterceptConnection(); if (args.Length == 0) { await preConnection.Connect(InterceptionMode.Pre); await postConnection.Connect(InterceptionMode.Post); await executedConnection.Connect(InterceptionMode.Executed); } else { await preConnection.Connect(InterceptionMode.Pre, args[0]); await postConnection.Connect(InterceptionMode.Post, args[0]); await executedConnection.Connect(InterceptionMode.Executed, args[0]); } Console.WriteLine("Connected!"); // Start waiting for incoming codes Task[] tasks = new Task[] { PrintIncomingCodes("pre", preConnection), PrintIncomingCodes("post", postConnection), PrintIncomingCodes("executed", executedConnection) }; // Wait for all tasks to finish await Task.WhenAll(tasks); }
public static async Task Main(string[] args) { // Parse the command line arguments string lastArg = null, socketPath = Defaults.FullSocketPath; bool quiet = false; foreach (string arg in args) { if (lastArg == "-s" || lastArg == "--socket") { socketPath = arg; } else if (arg == "-q" || lastArg == "--quiet") { quiet = true; } else if (arg == "-h" || arg == "--help") { Console.WriteLine("Available command line arguments:"); Console.WriteLine("-s, --socket <socket>: UNIX socket to connect to"); Console.WriteLine("-q, --quiet: Do not display when a connection has been established"); Console.WriteLine("-h, --help: Display this help text"); return; } lastArg = arg; } // Connect to DCS using InterceptConnection preConnection = new InterceptConnection(); using InterceptConnection postConnection = new InterceptConnection(); using InterceptConnection executedConnection = new InterceptConnection(); await preConnection.Connect(InterceptionMode.Pre, socketPath); await postConnection.Connect(InterceptionMode.Post, socketPath); await executedConnection.Connect(InterceptionMode.Executed, socketPath); if (!quiet) { Console.WriteLine("Connected!"); } // Start waiting for incoming codes Task[] tasks = new Task[] { PrintIncomingCodes(preConnection), PrintIncomingCodes(postConnection), PrintIncomingCodes(executedConnection) }; // Wait for all tasks to finish await Task.WhenAll(tasks); }
static async Task PrintIncomingCodes(string prefix, InterceptConnection connection) { while (true) { Code code = await connection.ReceiveCode(); // If you do not wish to let the received code execute, you can run connection.ResolveCode instead. // Before you call either Ignore or Resolve, you may execute as many commands as you want. // Codes initiated from the intercepting connection cannot be intercepted from the same connection. await connection.IgnoreCode(); Console.WriteLine($"[{prefix}] {code.Channel}: {code}"); } }
private static async Task PrintIncomingCodes(InterceptConnection connection) { try { Code code; do { code = await connection.ReceiveCode(); // If you do not wish to let the received code execute, you can run connection.ResolveCode instead. // Before you call one of Cancel, Ignore, or Resolve you may execute as many commands as you want. // Codes initiated from the intercepting connection cannot be intercepted from the same connection. await connection.IgnoreCode(); Console.WriteLine($"[{connection.Mode}] {code.Channel}: {code}"); }while (true); } catch (SocketException) { // Server has closed the connection } }
public static async Task Main(string[] args) { bool quiet = false, priorityCodes = false; CodeChannel[] channels = null; string[] filters = null, types = new string[] { "pre", "post", "executed" }; // Parse the command line arguments string lastArg = null, socketPath = Defaults.FullSocketPath; foreach (string arg in args) { if (lastArg == "-t" || lastArg == "--types") { types = arg.Split(','); } else if (lastArg == "-c" || lastArg == "--channels") { channels = arg.Split(',').Select(item => (CodeChannel)Enum.Parse(typeof(CodeChannel), item, true)).ToArray(); } else if (lastArg == "-f" || lastArg == "--filters") { filters = arg.Split(','); } else if (lastArg == "-s" || lastArg == "--socket") { socketPath = arg; } else if (arg == "-p" || arg == "--priority-codes") { priorityCodes = true; } else if (arg == "-q" || lastArg == "--quiet") { quiet = true; } else if (arg == "-h" || arg == "--help") { Console.WriteLine("Available command line arguments:"); Console.WriteLine("-t, --types <types>: Comma-delimited interception types (pre, post, or executed)"); Console.WriteLine("-c, --channels <channels>: Comma-delimited input channels where codes may be intercepted"); Console.WriteLine("-f, --filters <filters>: Comma-delimited code types that may be intercepted (main codes, keywords, or Q0 for comments)"); Console.WriteLine("-p, --priority-codes: Intercept priorty codes instead of regular codes (not recommended)"); Console.WriteLine("-s, --socket <socket>: UNIX socket to connect to"); Console.WriteLine("-q, --quiet: Do not display when a connection has been established"); Console.WriteLine("-h, --help: Display this help text"); return; } lastArg = arg; } InterceptConnection preConnection = null, postConnection = null, executedConnection = null; try { // Connect to DCS if (types.Contains("pre")) { preConnection = new InterceptConnection(); await preConnection.Connect(InterceptionMode.Pre, channels, filters, priorityCodes, socketPath); } if (types.Contains("post")) { postConnection = new InterceptConnection(); await postConnection.Connect(InterceptionMode.Post, channels, filters, priorityCodes, socketPath); } if (types.Contains("executed")) { executedConnection = new InterceptConnection(); await executedConnection.Connect(InterceptionMode.Executed, channels, filters, priorityCodes, socketPath); } if (!quiet) { Console.WriteLine("Connected!"); } // Start waiting for incoming codes Task[] tasks = new Task[] { (preConnection != null) ? PrintIncomingCodes(preConnection) : Task.CompletedTask, (postConnection != null) ? PrintIncomingCodes(postConnection) : Task.CompletedTask, (executedConnection != null) ? PrintIncomingCodes(executedConnection) : Task.CompletedTask }; // Wait for all tasks to finish await Task.WhenAll(tasks); } finally { preConnection?.Dispose(); postConnection?.Dispose(); executedConnection?.Dispose(); } }