public async Task ConnectAsync(ReceiverInfo receiverInfo) { await ConnectAsync(receiverInfo.IPAddress, receiverInfo.Port); }
public void Connect(ReceiverInfo receiverInfo) { Connect(receiverInfo.IPAddress, receiverInfo.Port); }
/// <summary> /// Try to find ISCP devices on network. /// </summary> public static Task <List <ReceiverInfo> > DiscoverAsync(int millisecondsTimeout = 5000) { return(Task.Run(() => { int onkyoPort = 60128; byte[] onkyoMagic = new ISCPMessage("ECNQSTN").GetBytes("!x"); var foundReceivers = new List <ReceiverInfo>(); IEnumerable <string> ips = GetInterfaceAddresses(); // Broadcast magic Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); using (socket) { socket.Blocking = false; // So we can use Poll socket.EnableBroadcast = true; socket.Bind(new IPEndPoint(IPAddress.Any, 0)); foreach (var i in ips) { try { var ip = IPAddress.Parse(i); socket.SendTo(onkyoMagic, new IPEndPoint(ip, onkyoPort)); } catch (SocketException) { } } EndPoint addr = new IPEndPoint(IPAddress.Broadcast, onkyoPort); byte[] data = new byte[1024]; int read = 0; while (true) { int microsecondTimeout = (int)(millisecondsTimeout * 1000); if (!socket.Poll(microsecondTimeout, SelectMode.SelectRead)) { break; } read = socket.ReceiveFrom(data, ref addr); var msg = ISCPMessage.Parse(data); // Return string looks something like this: // !1ECNTX-NR609/60128/DX GroupCollection info = Regex.Match(msg.ToString(), //@"!" + //@"(?<device_category>\d)" + @"ECN" + @"(?<model_name>[^/]*)/" + @"(?<iscp_port>\d{5})/" + @"(?<area_code>\w{2})/" + @"(?<identifier>.*)" ).Groups; IPAddress adr = (addr as IPEndPoint).Address; if (!foundReceivers.Any(p => p.IPAddress == adr)) { var ri = new ReceiverInfo { Port = Int32.Parse(info["iscp_port"].Value), Model = info["model_name"].Value, IPAddress = adr }; foundReceivers.Add(ri); } } } return foundReceivers; })); }