forked from microsoft/qsharp-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
139 lines (116 loc) · 4.15 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Net.Sockets;
using Microsoft.Build.Locator;
using Mono.Options;
namespace Microsoft.Quantum.QsLanguageServer
{
enum ConnectionMode
{
NamedPipe,
Socket
}
public static class Server
{
static string logFile = null;
static int? port = null;
static string writerPipeName = null;
static string readerPipeName = null;
static void Log(object msg)
{
// We open and close the file each time; this is not efficient,
// but prevents locks on the file that would otherwise be very
// annoying to debug.
if (logFile != null) {
using (var writer = new StreamWriter(logFile, append: true))
{
writer.WriteLine(msg);
}
}
}
static ConnectionMode Validate()
{
if (port != null && (writerPipeName != null || readerPipeName != null))
{
Log("[Error] Must specify either port or pipe name, not both.");
Environment.Exit(-1);
}
if (port == null && (writerPipeName == null || readerPipeName == null))
{
Log("[Error] Must specify both writer and reader pipe names.");
Environment.Exit(-2);
}
return port == null
? ConnectionMode.NamedPipe
: ConnectionMode.Socket;
}
public static QsLanguageServer ConnectNamedPipe(string writerName, string readerName)
{
var writerPipe = new NamedPipeClientStream(writerName);
var readerPipe = new NamedPipeClientStream(readerName);
readerPipe.Connect();
writerPipe.Connect();
return new QsLanguageServer(writerPipe, readerPipe);
}
private static QsLanguageServer ConnectSocket(string hostname = "localhost", int port = 8008)
{
try
{
Log($"Connecting via socket.");
var client = new TcpClient(hostname, port);
var stream = client.GetStream();
Log($"Connected to {hostname} at port {port}.");
var lsp = new QsLanguageServer(stream, stream);
return lsp;
}
catch (Exception ex)
{
Log($"[ERROR] {ex.Message}");
Environment.Exit(-2);
return null;
}
}
static int Main(string[] args)
{
MSBuildLocator.RegisterDefaults(); // needed to "configure" the design time build
Log("Called with command line arguments: " + String.Join(" ", args));
var options = new OptionSet
{
{ "l|log=", "file to write log messages to.", l => logFile = l },
{ "p|port=", "TCP port to connect to", (int p) => port = p },
{ "w|writer=", "Named pipe to write to", w => writerPipeName = w },
{ "r|reader=", "Named pipe to read from", r => readerPipeName = r }
};
List<String> extra = null;
try { extra = options.Parse(args); }
catch (OptionException e)
{ Log(e.Message); }
QsLanguageServer server = null;
switch (Validate())
{
case ConnectionMode.NamedPipe:
server = ConnectNamedPipe(writerPipeName, readerPipeName);
break;
case ConnectionMode.Socket:
server = ConnectSocket(port: port.Value);
break;
}
Log("Waiting for shutdown...");
server.WaitForShutdown();
if (server.ReadyForExit)
{
Log("Exiting normally.");
return 0;
}
else
{
Log("Exiting abnormally.");
return 1;
}
}
}
}