//Connection asynchronous ping private async Task PingConnectionAsync(XenaxDeviceConnection xenaxDevice) { if (xenaxDevice.pingPending) { return; } else { xenaxDevice.pingPending = true; Ping ping = new Ping(); var reply = await ping.SendPingAsync(xenaxDevice.stageIPAddress); xenaxDevice.pingPending = false; if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) { if (xenaxDevice.stageConnectionStatus != ConnectionStatusEn.Connected) { xenaxDevice.stageConnectionStatus = ConnectionStatusEn.Available; } } else { if (xenaxDevice.stageConnectionStatus == ConnectionStatusEn.Connected) { StopClient(); } xenaxDevice.stageConnectionStatus = ConnectionStatusEn.NotAvailable; } } }
//Start client connection public void StartClient(XenaxDeviceConnection xenaxDeviceConnection) { try { //Connection attempt timeout timer 2seconds connectionTimeoutTimer = new DispatcherTimer(); connectionTimeoutTimer.Interval = new TimeSpan(0, 0, 5); connectionTimeoutTimer.Tick += new EventHandler(OnConnectionTimeoutTimerTick); connectionTimeoutTimer.Start(); connection = xenaxDeviceConnection; //Establish the remote endpoint for the socket. //IPEndPoint remoteEP = new IPEndPoint(xenaxDeviceConnection.stageIPAddress, port); IPEndPoint remoteEP = new IPEndPoint(connection.stageIPAddress, port); //Create a TCP/IP socket. //client = new Socket(xenaxDeviceConnection.stageIPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); client = new Socket(connection.stageIPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); //Connect to the remote endpoint. client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); } catch (Exception e) { MessageBox.Show(e.Message, "Xenax controller - Error", MessageBoxButton.OK, MessageBoxImage.Error); } }