Files
dotnet-screenlogic/FindUnits.cs
Parnic e524e394c6 C#-ified
Converted as much ugly code as I could find into appropriate C#-friendly versions. For example, all the manual ByteHelper stuff, buffer index tracking, etc. are now BinaryWriters/BinaryReaders. Also cleaned up a bunch of getter and setter methods to use C# properties.

There's still more to be done here, but this greatly simplifies the code for reading and comprehension.
2018-03-28 15:30:05 -05:00

51 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace ScreenLogicConnect
{
public class FindUnits
{
protected const short multicastPort = 1444;
private Socket searchSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
public static async Task<IEnumerable<EasyTouchUnit>> Find()
{
List<EasyTouchUnit> units = new List<EasyTouchUnit>();
using (var udpClient = new UdpClient(new IPEndPoint(GetMyIP(), 53112))
{
EnableBroadcast = true,
MulticastLoopback = false,
})
{
await udpClient.SendAsync(FindUnitBroadcast.data, FindUnitBroadcast.data.Length, new IPEndPoint(IPAddress.Broadcast, multicastPort));
var buf = await udpClient.ReceiveAsync();
var findServerResponse = new EasyTouchUnit(buf);
if (findServerResponse.isValid)
{
units.Add(findServerResponse);
}
}
return units;
}
public static IPAddress GetMyIP()
{
IPAddress localIP;
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
{
socket.Connect("8.8.8.8", 65530);
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
localIP = endPoint.Address;
}
return localIP;
}
}
}