-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCli.cs
60 lines (52 loc) · 2.24 KB
/
Cli.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
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using PCR1000;
using PCR1000.Network.Server;
namespace PCRNetworkServer
{
public static class Cli
{
private static PcrNetworkServer _pcrNetworkServer;
public static void Run(bool security, string password, int port, string device)
{
var title = Console.Title;
Console.Title = "Network Server";
var conCol = Console.ForegroundColor;
Console.WriteLine("############################################\n" +
"# #\n" +
"# PCR1000 Network Server #\n" +
"# <https://github.com/mrkno/PCR1000-API> #\n" +
"# #\n" +
"############################################");
_pcrNetworkServer = new PcrNetworkServer(new PcrSerialComm(device), port, security, password);
if (!_pcrNetworkServer.Start())
{
throw new InvalidOperationException("Starting the network server failed.");
}
Console.WriteLine($"IP:\t\t{GetLocalIPAddress()}");
Console.WriteLine($"Port:\t\t{port}");
Console.WriteLine($"TLS:\t\t{(security?"Enabled":"Disabled")}");
Console.WriteLine($"Password:\t{(string.IsNullOrEmpty(password)?"<none>":password)}");
Console.WriteLine($"Device:\t\t{device}");
Console.WriteLine("Server Started. Press any key to stop...");
Console.ReadKey(true);
if (!_pcrNetworkServer.Stop())
{
throw new InvalidOperationException("Stopping the network server failed.");
}
Console.ForegroundColor = conCol;
Console.Title = title;
}
private static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork))
{
return ip.ToString();
}
return "<ERRNOIP>";
}
}
}