private void FillList() { lock (this) { using (var client = new UdpClient(m_portNumber)) { //configure the socket properly client.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true); client.Client.EnableBroadcast = true; client.Client.ReceiveTimeout = 1000; ScoutHelper.BroadcastRequest(request, m_portNumber, logger); try { // loop until you timeout or read a bad client while (true) { //IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); //var buf = client.Receive(ref endPoint); //int bytesRead = buf.Length; SocketFlags socketFlags = SocketFlags.None; IPPacketInformation packetInfo; EndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); byte[] buf = new byte[2000]; int bytesRead = client.Client.ReceiveMessageFrom(buf, 0, 2000, ref socketFlags, ref endPoint, out packetInfo); if (ScoutHelper.IsMyAddress(((IPEndPoint)endPoint).Address)) { continue; } if (bytesRead < m_offset_router_IP + 4) { logger.Log("Foscam scout got invalid UDP packet of length {0}", bytesRead.ToString()); continue; } //var device = CreateDevice(buf, null); var device = CreateDevice(buf, ScoutHelper.GetInterface(packetInfo)); currDeviceList.InsertDevice(device); } } catch (SocketException) { //we expect this error becuase of timeout } catch (Exception e) { logger.Log("Exception in FoscamScout FillList: {0}", e.ToString()); } client.Close(); } } }
void ScanWifi() { //we are putting this lock here just in case two of these are running at the same time (e.g., if the ScanNow timer was really short) //only one of these calls can be active because of socket conflicts lock (this) { //logger.Log("GadgeteerScout:ScanWifi\n"); using (var client = new UdpClient(responsePortNumber)) { //configure the socket properly client.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true); client.Client.EnableBroadcast = true; client.Client.ReceiveTimeout = 1000; ScoutHelper.BroadcastRequest(request, queryPortNumber, logger); try { // loop until you timeout or read a bad client while (true) { //var buf = client.Receive(ref endPoint); SocketFlags socketFlags = SocketFlags.None; EndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); IPPacketInformation packetInfo; byte[] buf = new byte[2000]; int bytesRead = client.Client.ReceiveMessageFrom(buf, 0, 2000, ref socketFlags, ref endPoint, out packetInfo); if (ScoutHelper.IsMyAddress(((IPEndPoint)endPoint).Address)) { continue; } var device = CreateDeviceWifi(buf, bytesRead, (IPEndPoint)endPoint, ScoutHelper.GetInterface(packetInfo)); currentDeviceList.InsertDevice(device); logger.Log("Put device on devicelist: {0} \n", device.ToString()); } } catch { ; } } } }
public void ReceiveCallback(IAsyncResult ar) { lock (this) { if (listenClientClosed) { return; } } IPPacketInformation packetInfo; int bytesRead = listenClient.Client.EndReceiveMessageFrom(ar, ref asyncSocketFlags, ref asyncRemoteEndPoint, out packetInfo); var device = CreateDeviceWifi(asyncBuffer, bytesRead, (IPEndPoint)asyncRemoteEndPoint, ScoutHelper.GetInterface(packetInfo)); currentDeviceList.InsertDevice(device); //logger.Log("GadgeteerListener got packet from {0}", device.UniqueName); //let the platform know of the device platform.ProcessNewDiscoveryResults(new List <Device>() { device }); //start listening again //listenClient.BeginReceive(new AsyncCallback(ReceiveCallback), null); asyncSocketFlags = SocketFlags.None; asyncRemoteEndPoint = new IPEndPoint(IPAddress.Any, 0); listenClient.Client.BeginReceiveMessageFrom(asyncBuffer, 0, 2000, asyncSocketFlags, ref asyncRemoteEndPoint, new AsyncCallback(ReceiveCallback), null); }