internal async Task<bool> IsDeviceConnectedOperationAsync(Device device) { if (device.RefreshRequired()) await GetDeviceStatusAsync(device); return device.Connected(); }
internal async Task GetDeviceStatusActionAsync(Device device) { if (ConnectionInProgress) return; var connected = false; var hostName = device.Hostname(); ConnectionAttemptInformation = ""; ConnectionInProgress = true; var portNumber = device.Port().ToString(); try { using (var tcpClient = new StreamSocket()) { await tcpClient.ConnectAsync(new HostName(hostName), portNumber, SocketProtectionLevel.PlainSocket); var remoteIp = tcpClient.Information.RemoteAddress.DisplayName; ConnectionAttemptInformation = $"Success, remote device: {device.Name()} was contacted at IP address {remoteIp} on port {portNumber}"; connected = true; tcpClient.Dispose(); } } catch (Exception ex) { switch (ex.HResult) { case -2147014835: //Actively refused connection, so is connected to the network ConnectionAttemptInformation = $"Success, remote device activelty refused the connection on port {portNumber}."; connected = true; break; case -2147013895: ConnectionAttemptInformation = "Error: No such host is known"; break; case -2147014836: ConnectionAttemptInformation = "Error: Timeout when connecting (check hostname and port)"; break; default: ConnectionAttemptInformation = "Error: Exception returned from network stack: " + ex.Message; break; } } finally { ConnectionInProgress = false; } //Debug.WriteLine(ConnectionAttemptInformation); device.UpdateStatus(connected, ConnectionAttemptInformation); }
public IAsyncAction GetDeviceStatusAsync(Device device) { return GetDeviceStatusActionAsync(device).AsAsyncAction(); }
public IAsyncOperation<bool> IsDeviceConnectedAsync(Device device) { return IsDeviceConnectedOperationAsync(device).AsAsyncOperation(); }