-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
128 lines (116 loc) · 3.92 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
using System.IO.Pipes;
using Coder.Desktop.Vpn.Proto;
namespace Coder.Desktop.Vpn.DebugClient;
public static class Program
{
private static Speaker<ClientMessage, ServiceMessage>? _speaker;
private static string? _coderUrl;
private static string? _apiToken;
public static void Main()
{
Console.WriteLine("Type 'exit' to exit the program");
Console.WriteLine("Type 'connect' to connect to the service");
Console.WriteLine("Type 'disconnect' to disconnect from the service");
Console.WriteLine("Type 'configure' to set the parameters");
Console.WriteLine("Type 'start' to send a start command with the current parameters");
Console.WriteLine("Type 'stop' to send a stop command");
while (true)
{
Console.Write("> ");
var input = Console.ReadLine()?.Trim();
try
{
switch (input)
{
case "exit":
return;
case "connect":
Connect();
break;
case "disconnect":
Disconnect();
break;
case "configure":
Configure();
break;
case "start":
Start();
break;
case "stop":
Stop();
break;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex}");
}
}
}
private static void Connect()
{
var client = new NamedPipeClientStream(".", "Coder.Desktop.Vpn", PipeDirection.InOut, PipeOptions.Asynchronous);
client.Connect();
Console.WriteLine("Connected to named pipe.");
_speaker = new Speaker<ClientMessage, ServiceMessage>(client);
_speaker.Receive += message => { Console.WriteLine($"Received({message.Message.MsgCase}: {message.Message}"); };
_speaker.Error += exception =>
{
Console.WriteLine($"Error: {exception}");
Disconnect();
};
_speaker.StartAsync().Wait();
Console.WriteLine("Speaker started.");
}
private static void Disconnect()
{
_speaker?.DisposeAsync().AsTask().Wait();
_speaker = null;
Console.WriteLine("Disconnected from named pipe");
}
private static void Configure()
{
Console.Write("Coder URL: ");
_coderUrl = Console.ReadLine()?.Trim();
Console.Write("API Token: ");
_apiToken = Console.ReadLine()?.Trim();
}
private static void Start()
{
if (_speaker is null)
{
Console.WriteLine("Not connected to Coder.Desktop.Vpn.");
return;
}
var message = new ClientMessage
{
Start = new StartRequest
{
CoderUrl = _coderUrl,
ApiToken = _apiToken,
},
};
Console.WriteLine("Sending start message...");
var sendTask = _speaker.SendRequestAwaitReply(message).AsTask();
Console.WriteLine("Start message sent, awaiting reply.");
sendTask.Wait();
Console.WriteLine($"Received reply: {sendTask.Result.Message}");
}
private static void Stop()
{
if (_speaker is null)
{
Console.WriteLine("Not connected to Coder.Desktop.Vpn.");
return;
}
var message = new ClientMessage
{
Stop = new StopRequest(),
};
Console.WriteLine("Sending stop message...");
var sendTask = _speaker.SendRequestAwaitReply(message);
Console.WriteLine("Stop message sent, awaiting reply.");
var reply = sendTask.AsTask().Result;
Console.WriteLine($"Received reply: {reply.Message}");
}
}