public override void ConnectDevice(ConnectionDetail connectiondetails) { Console.WriteLine("Connecting USB..."); client_name = _servComm.GetNETBIOSName(); UpdateLargeUI(); UpdateSmallUI(); try { USBdriver usbdriver = new USBdriver(connectiondetails.details.ip_address, connectiondetails.details.port); _usbref = new ThreadStart(usbdriver.listen); _usbthread = new Thread(_usbref); DevicesForm._openThreads.Add(_usbthread); _usbthread.Start(); //send msg to server = "connected to device" } catch (Exception e) { Console.WriteLine("Socket not created. " + e); //turn off button UI client_name = null; UpdateLargeUI(); UpdateSmallUI(); //send msg to server - "cannot connect to device" } }
public override async void ConnectDevice(ConnectionDetail connectiondetails) { Console.WriteLine("Connecting Audio... {0} : {1}", connectiondetails.details.ip_address, connectiondetails.details.port); UpdateLargeUI(); UpdateSmallUI(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true; TcpClient tcpClient = new TcpClient(); await tcpClient.ConnectAsync(IPAddress.Parse(connectiondetails.details.ip_address), connectiondetails.details.port); _nsClient = tcpClient.GetStream(); Console.WriteLine("Connected Audio direct: {0}", tcpClient.Client.RemoteEndPoint); //_progState = ProgState.Connected; connected = true; client_name = _servComm.GetNETBIOSName(); UpdateUI(); try { while (_nsClient != null) { await ReceiveDataAsync(); } } catch { } finally { //This is disconnection client_name = null; connected = false; UpdateUI(); } }
public async void ServerRequest(Device dev) { _dev = dev; _dev.client_name = "Requesting..."; UpdateUI(); //Send a request to the server for Device Info RequestInfo requestinfo = new RequestInfo(); requestinfo.hostname = _serverComm.GetNETBIOSName(); requestinfo.localIP = _serverComm.GetLocalIPAddress(); if (DevicesForm._client_id != null) { requestinfo.client_id = DevicesForm._client_id; } string request = JsonConvert.SerializeObject(requestinfo); Console.WriteLine("Request to server: {0}", request); StringBuilder deviceUri = new StringBuilder(_serverComm.Root); deviceUri.Append("/device/"); deviceUri.Append(device_id); deviceUri.Append("/connect"); var result = await _serverComm.PostAsync(deviceUri.ToString(), new StringContent(request), contentType : "application/json"); if (result.Item1 != ServerResponse.Connected) { // should be handled better MessageBox.Show("Server disconnected during intialisation."); _dev.client_name = null; } else { //successful request string jsonstr = await result.Item2.Content.ReadAsStringAsync(); Console.WriteLine("Request Response: {0}", jsonstr); ConnectRequest connectrequest = JsonConvert.DeserializeObject <ConnectRequest>(jsonstr); DevicesForm._client_id = connectrequest.client_id; Registry.SetValue(DevicesForm._keyName, _serverComm.Email, DevicesForm._client_id); Console.WriteLine("Registry written: {0} : {1} : {2}", DevicesForm._keyName, _serverComm.Email, DevicesForm._client_id); _dev.client_name = "Polling..."; UpdateUI(); pollCounter = 0; _timer.Start(); //start the polling timer } }