-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
119 lines (93 loc) · 3.42 KB
/
Program.cs
File metadata and controls
119 lines (93 loc) · 3.42 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
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ScriptCs;
using ScriptCs.Contracts;
using ScriptCs.Hosting;
using ScriptCs.Engine.Roslyn;
namespace RemoteCSharpShell
{
class Program
{
static void Main(string[] args)
{
var replServer = new TcpListener(IPAddress.Loopback, 1234);
replServer.Start();
Console.WriteLine("Waiting for connection");
new Thread(() => Listen(replServer))
{
IsBackground = true
}.Start();
Console.ReadLine();
replServer.Stop();
}
private static void Listen(TcpListener replServer)
{
while (true)
{
var client = replServer.AcceptTcpClient();
new Thread(() => HandleClient(client)).Start();
}
}
private static void HandleClient(TcpClient client)
{
Console.WriteLine("Accepted connection {0}", client.Client.RemoteEndPoint);
try
{
var stream = client.GetStream();
using (var reader = new StreamReader(stream))
{
using (var writer = new StreamWriter(stream))
{
writer.AutoFlush = true;
RunRepl(reader, writer);
}
}
client.Close();
}
catch (IOException e)
{
}
}
private static void RunRepl(TextReader input, TextWriter output)
{
var vt = new VirtualTerminal(input, output);
var console = new InOutConsole(vt);
var scriptServices = BuildScriptServices(console);
while (true)
{
string line = vt.ReadLine(">");
if (line == "q")
{
scriptServices.Repl.Terminate();
break;
}
if (!string.IsNullOrWhiteSpace(line))
{
vt.RecordHistoryLine(line);
}
scriptServices.Repl.Execute(line);
}
}
private static ScriptServices BuildScriptServices(InOutConsole console)
{
var logConfiguration = new LoggerConfigurator(LogLevel.Info);
logConfiguration.Configure(console);
var logger = logConfiguration.GetLogger();
var initializationServices = new InitializationServices(logger);
initializationServices.GetAppDomainAssemblyResolver().Initialize();
var scriptServicesBuilder = new ScriptServicesBuilder(console, logger, null, null, initializationServices)
.Repl(true);
scriptServicesBuilder.LoadModules("");
var scriptServices = scriptServicesBuilder.Build();
initializationServices.GetInstallationProvider().Initialize();
scriptServices.Repl.Initialize(Enumerable.Empty<string>(), Enumerable.Empty<IScriptPack>());
return scriptServices;
}
}
}