-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
54 lines (47 loc) · 1.79 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
using System;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Threading;
#nullable enable
namespace IRWA
{
public class Program
{
public static void Main()
{
Debug.WriteLine("Waiting for Wifi network...");
var address = WaitForNetwork(default, TimeSpan.FromSeconds(10));
Debug.WriteLine($"Connection established as \"{address ?? "?"}\". Starting HTTP server...");
IrSenderNec irSenderNec = new IrSenderNec(23);
AppServer appServer = new AppServer(80, irSenderNec);
appServer.Start();
Debug.WriteLine("HTTP server started - system ready!");
Thread.Sleep(Timeout.Infinite);
}
private static string? WaitForNetwork(CancellationToken token, TimeSpan timeout)
{
NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];
DateTime timeoutTime = default;
if (timeout > TimeSpan.Zero)
{
timeoutTime = DateTime.UtcNow + timeout;
}
while (!token.IsCancellationRequested &&
(DateTime.UtcNow < timeoutTime || timeoutTime == default))
{
if (networkInterface.IPv4Address != null &&
networkInterface.IPv4Address.Length > 0 &&
networkInterface.IPv4Address[0] != '0')
{
return networkInterface.IPv4Address;
}
else
{
token.WaitHandle.WaitOne(500, false);
networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];
}
}
return null;
}
}
}