-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathProgram.cs
More file actions
151 lines (128 loc) · 5 KB
/
Copy pathProgram.cs
File metadata and controls
151 lines (128 loc) · 5 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Ookii.CommandLine;
using static System.FormattableString;
namespace SampleDebugAdapter
{
internal class ProgramArgs
{
[CommandLineArgument("debug", DefaultValue = false, IsRequired = false)]
public bool Debug { get; set; }
[CommandLineArgument("nodebug", DefaultValue = false, IsRequired = false)]
public bool NoDebug { get; set; }
[CommandLineArgument("server", DefaultValue = 0, IsRequired = false)]
public int ServerPort { get; set; }
[CommandLineArgument("stepOnEnter", DefaultValue = false, IsRequired = false)]
public bool StepOnEnter { get; set; }
}
internal class Program
{
private static void Main(string[] args)
{
CommandLineParser parser = new CommandLineParser(typeof(ProgramArgs));
ProgramArgs arguments = null;
try
{
arguments = (ProgramArgs)parser.Parse(args);
}
catch (CommandLineArgumentException ex)
{
Console.WriteLine(ex.Message);
parser.WriteUsageToConsole();
return;
}
if (arguments.Debug)
{
Console.WriteLine("Waiting for debugger...");
while (true)
{
if (Debugger.IsAttached)
{
Console.WriteLine("Debugger attached!");
break;
}
Thread.Sleep(100);
}
}
if (arguments.StepOnEnter && arguments.ServerPort == 0)
{
Console.WriteLine("Continue-on-Enter requires server mode.");
return;
}
if (arguments.ServerPort != 0)
{
// Server mode - listen on a network socket
RunServer(arguments);
}
else
{
// Standard mode - run with the adapter connected to the process's stdin and stdout
SampleDebugAdapter adapter = new SampleDebugAdapter(Console.OpenStandardInput(), Console.OpenStandardOutput());
adapter.Protocol.LogMessage += (sender, e) => Debug.WriteLine(e.Message);
adapter.Run();
}
}
private static void RunServer(ProgramArgs args)
{
Console.WriteLine(Invariant($"Waiting for connections on port {args.ServerPort}..."));
SampleDebugAdapter adapter = null;
Thread listenThread = new Thread(() =>
{
TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), args.ServerPort);
listener.Start();
while (true)
{
Socket clientSocket = listener.AcceptSocket();
Thread clientThread = new Thread(() =>
{
Console.WriteLine("Accepted connection");
using (Stream stream = new NetworkStream(clientSocket))
{
adapter = new SampleDebugAdapter(stream, stream);
adapter.Protocol.LogMessage += (sender, e) => Console.WriteLine(e.Message);
adapter.Protocol.DispatcherError += (sender, e) =>
{
Console.Error.WriteLine(e.Exception.Message);
};
adapter.Run();
adapter.Protocol.WaitForReader();
adapter = null;
}
Console.WriteLine("Connection closed");
});
clientThread.Name = "DebugServer connection thread";
clientThread.Start();
}
});
Thread keypressThread;
if (args.StepOnEnter)
{
Console.WriteLine("Will step when ENTER is pressed.");
keypressThread = new Thread(() =>
{
ConsoleKeyInfo keyInfo;
while (true)
{
keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.Enter && adapter != null)
{
Console.WriteLine("Forcing step");
adapter.ExitBreakCore(0, true);
}
}
});
keypressThread.Name = "Keypress listener thread";
keypressThread.Start();
}
listenThread.Name = "DebugServer listener thread";
listenThread.Start();
listenThread.Join();
}
}
}