-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
83 lines (75 loc) · 2.73 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using Repeat.compiler;
using System.Runtime.InteropServices;
using System.Threading;
using System;
using log4net;
using log4net.Config;
using Repeat.ipc;
using Newtonsoft.Json;
using System.Text;
using System.IO;
using Repeat.utilities;
using Repeat.userDefinedAction;
using Newtonsoft.Json.Linq;
namespace Repeat
{
class Program {
private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private const int DEFAULT_PORT = 9999;
/**************************************************************************************************************************************/
// Declare the SetConsoleCtrlHandler function
// as external and receiving a delegate.
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
// A delegate type to be used as the handler routine
// for SetConsoleCtrlHandler.
public delegate bool HandlerRoutine(CtrlTypes CtrlType);
// An enumerated type for the control messages
// sent to the handler routine.
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
/**************************************************************************************************************************************/
private static RepeatClient client;
private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
{
if (client != null)
{
logger.Info("Caught signal. Terminating IPC client...");
client.StopRunning();
}
return true;
}
private static int GetPort(String[] args)
{
for (int i = 0; i < args.Length - 1; i++)
{
if (args[i].Equals("--port"))
{
int output = 0;
if (int.TryParse(args[i + 1], out output))
{
logger.Info("Using provided port " + DEFAULT_PORT);
return output;
}
}
}
logger.Info("Using default port " + DEFAULT_PORT);
return DEFAULT_PORT;
}
public static void Main(String[] args)
{
SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
BasicConfigurator.Configure();
int port = GetPort(args);
client = new RepeatClient(port);
client.StartRunning();
logger.Info("Successfully started C# IPC client.");
}
}
}