public BorIPDevice(CyUSBDevice usbDevice, MonoUsbProfile usbProfile, EDeviceType deviceType) : base(usbDevice, usbProfile, deviceType) { if (deviceType == EDeviceType.ADC) { byte[] response = ReceiveVendorResponse((byte)EVendorRequests.DeviceParam, 2); dataPortNo = (ushort)(response[0] + (response[1] << 8)); } else { byte[] response = ReceiveVendorResponse((byte)EVendorRequests.DeviceParam, 4); dataPortNo = (ushort)(response[0] + (response[1] << 8)); ControlPortNo = (ushort)(response[2] + (response[3] << 8)); } Console.WriteLine($"+ {this}"); var ct = Cts.Token; Task.Run(() => { TcpListener listener = BorIPCreateListener(BORIP_SERVERPORT); try { listener.Start(); var addresses = Dns.GetHostAddresses(Dns.GetHostName()) .Where(p => p.ToString().Contains('.')); Console.WriteLine($"{BORIP_SERVERPORT}: {string.Join(" ", addresses)}"); CancellationTokenSource tcpCts = null; while (!ct.IsCancellationRequested) { TcpClient client = listener.AcceptTcpClient(); Console.WriteLine($"{BORIP_SERVERPORT}: accepted"); if (tcpCts != null) { tcpCts.Cancel(); } tcpCts = new CancellationTokenSource(); var tcpCt = tcpCts.Token; Task.Run(() => { BorIPClient borIPClient = new BorIPClient(client); borIPClients.Add(borIPClient); try { using (NetworkStream ns = client.GetStream()) using (StreamReader sr = new StreamReader(ns, Encoding.ASCII)) using (StreamWriter sw = new StreamWriter(ns, Encoding.ASCII)) { BorIPWriteLine(sw, "DEVICE -"); while (!tcpCt.IsCancellationRequested) { string str = sr.ReadLine(); if (string.IsNullOrWhiteSpace(str)) { return; // keep alive } Console.WriteLine($"{BORIP_SERVERPORT}: [in] {str.Trim()}"); BorIPProcessInput(borIPClient, sw, str); } } } catch (Exception) { // nothing to do } finally { borIPClients.Remove(borIPClient); Console.WriteLine($"{BORIP_SERVERPORT}: closed"); } }, tcpCt); } } catch (SocketException ex) when(ex.ErrorCode == 10004) { // nothing to do } catch (OperationCanceledException) { // nothing to do } catch (Exception ex) { Console.WriteLine($"{BORIP_SERVERPORT}: {ex.Message}"); } finally { listener.Stop(); //Console.WriteLine($"{BORIP_PORTNO}: listener stopped"); } }, ct); if (USBDevice != null) { endpoint2 = USBDevice.EndPointOf(0x82) as CyBulkEndPoint; } Task.Run(() => { while (!ct.IsCancellationRequested) { if (borIPClients.Count == 0 && (ControlPortNo > 0 && controlClients.Count == 0)) { Thread.Sleep(100); continue; } UdpClient udp = new UdpClient(); try { int maxPacketSize; if (USBDevice != null) { maxPacketSize = endpoint2.MaxPktSize; } else { maxPacketSize = MonoUsbApi.GetMaxPacketSize(USBProfile.ProfileHandle, 0x82); } byte[] inData = new byte[maxPacketSize]; byte[] outData = null; int outDataPos = 0; byte[] borIPData = null; int borIPDataPos = 0; while (!ct.IsCancellationRequested && !(borIPClients.Count == 0 && (ControlPortNo > 0 && controlClients.Count == 0))) { int xferLen = inData.Length; bool ret = false; if (USBDevice != null) { ret = endpoint2.XferData(ref inData, ref xferLen); } else { ret = MonoUsbApi.BulkTransfer(MonoDeviceHandle, 0x82, inData, inData.Length, out xferLen, TIMEOUT) == 0; } if (ret == false) { break; } int inDataPos = 0; while (!ct.IsCancellationRequested && inDataPos < xferLen) { if (outData == null) { outData = new byte[1472]; } if (borIPData == null) { borIPData = new byte[4 + 4 * NUM_SAMPLES]; borIPData[borIPDataPos++] = (byte)((RunningState == ERunningState.Start) ? 0x10 : 0x00); RunningState = ERunningState.Continued; borIPData[borIPDataPos++] = 0; borIPData[borIPDataPos++] = (byte)(sequence & 0xff); borIPData[borIPDataPos++] = (byte)((sequence >> 8) & 0xff); } while (outDataPos < outData.Length && borIPDataPos < borIPData.Length && inDataPos < xferLen) { byte b = inData[inDataPos++]; outData[outDataPos++] = b; borIPData[borIPDataPos++] = b; } if (borIPDataPos == borIPData.Length) { List <string> remoteAddrList = new List <string>(); foreach (var client in borIPClients.ToArray()) { if (client.Header) { string remoteAddr; try { remoteAddr = client.DestAddr; } catch { continue; } if (remoteAddrList.Contains(remoteAddr) == false) { remoteAddrList.Add(remoteAddr); udp.Send(borIPData, borIPData.Length, remoteAddr, client.DestPort); } } } sequence++; if (sequence == 0x10000) { sequence = 0; } borIPData = null; borIPDataPos = 0; } if (outDataPos == outData.Length) { List <string> remoteAddrList = new List <string>(); foreach (var client in borIPClients.ToArray()) { if (client.Header == false) { string remoteAddr; try { remoteAddr = client.DestAddr; } catch { continue; } if (remoteAddrList.Contains(remoteAddr) == false) { remoteAddrList.Add(remoteAddr); udp.Send(outData, outData.Length, remoteAddr, client.DestPort); } } } remoteAddrList.Clear(); foreach (var client in controlClients.ToArray()) { string remoteAddr; try { remoteAddr = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(); } catch { continue; } if (remoteAddrList.Contains(remoteAddr) == false) { remoteAddrList.Add(remoteAddr); udp.Send(outData, outData.Length, remoteAddr, dataPortNo); } } if (ControlPortNo == 0) { udp.Send(outData, outData.Length, "127.0.0.1", dataPortNo); } outData = null; outDataPos = 0; } } } } catch (OperationCanceledException) { // nothing to do } //catch (Exception ex) //{ // Console.WriteLine($"BorIP: {ex.Message}"); //} finally { udp.Close(); } Thread.Sleep(1000); } }, ct); }
public ADCDevice(CyUSBDevice usbDevice, MonoUsbProfile usbProfile) : base(usbDevice, usbProfile, EDeviceType.ADC) { byte[] response = ReceiveVendorResponse((byte)EVendorRequests.DeviceParam, 2); dataPortNo = (ushort)(response[0] + (response[1] << 8)); Console.WriteLine($"DeviceAttached: {this}"); if (USBDevice != null) { endpoint2 = USBDevice.EndPointOf(0x82) as CyBulkEndPoint; } var ct = Cts.Token; Task.Run(() => { UdpClient udp = new UdpClient(); try { int maxPacketSize; if (USBDevice != null) { maxPacketSize = endpoint2.MaxPktSize; } else { maxPacketSize = MonoUsbApi.GetMaxPacketSize(USBProfile.ProfileHandle, 0x82); } byte[] inData = new byte[maxPacketSize]; byte[] outData = null; int outDataPos = 0; while (!ct.IsCancellationRequested) { int xferLen = inData.Length; bool ret = false; if (USBDevice != null) { ret = endpoint2.XferData(ref inData, ref xferLen); } else { ret = MonoUsbApi.BulkTransfer(MonoDeviceHandle, 0x82, inData, inData.Length, out xferLen, TIMEOUT) == 0; } if (ret == false) { break; } int inDataPos = 0; while (!ct.IsCancellationRequested && inDataPos < xferLen) { if (outData == null) { outData = new byte[1472]; } while (outDataPos < outData.Length && inDataPos < xferLen) { outData[outDataPos++] = inData[inDataPos++]; } if (outDataPos == outData.Length) { udp.Send(outData, outData.Length, "127.0.0.1", dataPortNo); outData = null; outDataPos = 0; } } } } catch (OperationCanceledException) { // nothing to do } catch (Exception ex) { Console.WriteLine($"{DeviceType}: {ex.Message}"); } finally { udp.Close(); } }, ct); }
public DACDevice(CyUSBDevice usbDevice, MonoUsbProfile usbProfile, EDeviceType deviceType) : base(usbDevice, usbProfile, deviceType) { if (deviceType == EDeviceType.DAC) { byte[] response = ReceiveVendorResponse((byte)EVendorRequests.DeviceParam, 2); dataPortNo = (ushort)(response[0] + (response[1] << 8)); } else { byte[] response = ReceiveVendorResponse((byte)EVendorRequests.DeviceParam, 4); dataPortNo = (ushort)(response[0] + (response[1] << 8)); ControlPortNo = (ushort)(response[2] + (response[3] << 8)); } Console.WriteLine($"+ {this}"); if (USBDevice != null) { endpoint2 = USBDevice.EndPointOf(0x02) as CyBulkEndPoint; } var ct = Cts.Token; Task.Run(() => { try { while (!ct.IsCancellationRequested) { if (ControlPortNo > 0 && controlClients.Count == 0) { Thread.Sleep(100); continue; } TcpClient client = null; try { string remoteAddr; if (ControlPortNo == 0) { remoteAddr = "127.0.0.1"; } else { try { remoteAddr = ((IPEndPoint)controlClients[0].Client.RemoteEndPoint).Address.ToString(); } catch { continue; } } client = new TcpClient(remoteAddr, dataPortNo); } catch (SocketException ex) { if (ex.ErrorCode == 10061) { Thread.Sleep(1000); continue; } throw ex; } Console.WriteLine($"{dataPortNo}: accepted", DeviceType, ((IPEndPoint)client.Client.RemoteEndPoint).Address, ((IPEndPoint)client.Client.RemoteEndPoint).Port); NetworkStream ns = client.GetStream(); try { int maxPacketSize; if (USBDevice != null) { maxPacketSize = endpoint2.MaxPktSize; } else { maxPacketSize = MonoUsbApi.GetMaxPacketSize(USBProfile.ProfileHandle, 0x02); } byte[] inData = new byte[64 * 1024]; byte[] outData = new byte[maxPacketSize - 16]; // Some PCs cannot send 1024 bytes int outDataPos = 0; while (!ct.IsCancellationRequested && !(ControlPortNo > 0 && controlClients.Count == 0)) { int resSize = ns.Read(inData, 0, inData.Length); if (resSize == 0) { break; } int inDataLen = resSize; int inDataPos = 0; while (!ct.IsCancellationRequested && inDataPos < inDataLen) { while (outDataPos < outData.Length && inDataPos < inDataLen) { outData[outDataPos++] = inData[inDataPos++]; } if (outDataPos == outData.Length) { int xferLen = outData.Length; bool ret = false; if (USBDevice != null) { ret = endpoint2.XferData(ref outData, ref xferLen); } else { ret = MonoUsbApi.BulkTransfer(MonoDeviceHandle, 0x02, outData, outData.Length, out xferLen, TIMEOUT) == 0; } if (ret == false || xferLen == 0) { break; } if (xferLen != outData.Length) { Console.WriteLine($"{dataPortNo}: the response size {xferLen} not equal to the requested size {outData.Length}"); } outDataPos = 0; } } } } catch (Exception) { // nothing to do } finally { ns.Close(); client.Close(); Console.WriteLine($"{dataPortNo}: closed"); } } } catch (OperationCanceledException) { // nothing to do } catch (Exception ex) { Console.WriteLine($"{dataPortNo}: {ex.Message}"); } }, ct); }
public ADCDevice(CyUSBDevice usbDevice, MonoUsbProfile usbProfile, EDeviceType deviceType) : base(usbDevice, usbProfile, deviceType) { if (deviceType == EDeviceType.ADC) { byte[] response = ReceiveVendorResponse((byte)EVendorRequests.DeviceParam, 2); dataPortNo = (ushort)(response[0] + (response[1] << 8)); } else { byte[] response = ReceiveVendorResponse((byte)EVendorRequests.DeviceParam, 4); dataPortNo = (ushort)(response[0] + (response[1] << 8)); ControlPortNo = (ushort)(response[2] + (response[3] << 8)); } Console.WriteLine($"+ {this}"); if (USBDevice != null) { endpoint2 = USBDevice.EndPointOf(0x82) as CyBulkEndPoint; } var ct = Cts.Token; Task.Run(() => { while (!ct.IsCancellationRequested) { if (ControlPortNo > 0 && controlClients.Count == 0) { Thread.Sleep(100); continue; } UdpClient udp = new UdpClient(); try { int maxPacketSize; if (USBDevice != null) { maxPacketSize = endpoint2.MaxPktSize; } else { maxPacketSize = MonoUsbApi.GetMaxPacketSize(USBProfile.ProfileHandle, 0x82); } byte[] inData = new byte[maxPacketSize]; byte[] outData = null; int outDataPos = 0; while (!ct.IsCancellationRequested && !(ControlPortNo > 0 && controlClients.Count == 0)) { int xferLen = inData.Length; bool ret = false; if (USBDevice != null) { ret = endpoint2.XferData(ref inData, ref xferLen); } else { ret = MonoUsbApi.BulkTransfer(MonoDeviceHandle, 0x82, inData, inData.Length, out xferLen, TIMEOUT) == 0; } if (ret == false) { break; } int inDataPos = 0; while (!ct.IsCancellationRequested && inDataPos < xferLen) { if (outData == null) { outData = new byte[1472]; } while (outDataPos < outData.Length && inDataPos < xferLen) { outData[outDataPos++] = inData[inDataPos++]; } if (outDataPos == outData.Length) { List <string> remoteAddrList = new List <string>(); foreach (var client in controlClients.ToArray()) { string remoteAddr; try { remoteAddr = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(); } catch { continue; } if (remoteAddrList.Contains(remoteAddr) == false) { remoteAddrList.Add(remoteAddr); udp.Send(outData, outData.Length, remoteAddr, dataPortNo); } } if (ControlPortNo == 0) { udp.Send(outData, outData.Length, "127.0.0.1", dataPortNo); } outData = null; outDataPos = 0; } } } } catch (OperationCanceledException) { // nothing to do } catch (Exception ex) { Console.WriteLine($"{dataPortNo}: {ex.Message}"); } finally { udp.Close(); } Thread.Sleep(1000); } }, ct); }