private void ProcessIncomingUdp() { CircleCollection <Socket> circleCollection = new CircleCollection <Socket>(); foreach (Socket current in this.m_pSockets) { circleCollection.Add(current); } byte[] array = new byte[this.m_MTU]; while (this.m_IsRunning) { try { if (this.m_pQueuedPackets.Count >= this.m_MaxQueueSize) { Thread.Sleep(1); } else { bool flag = false; for (int i = 0; i < circleCollection.Count; i++) { Socket socket = circleCollection.Next(); if (socket.Poll(0, SelectMode.SelectRead)) { EndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); int num = socket.ReceiveFrom(array, ref endPoint); this.m_BytesReceived += (long)num; this.m_PacketsReceived += 1L; byte[] array2 = new byte[num]; Array.Copy(array, array2, num); Queue <UdpPacket> pQueuedPackets; Monitor.Enter(pQueuedPackets = this.m_pQueuedPackets); try { this.m_pQueuedPackets.Enqueue(new UdpPacket(socket, (IPEndPoint)endPoint, array2)); } finally { Monitor.Exit(pQueuedPackets); } flag = true; break; } } if (!flag) { Thread.Sleep(1); } } } catch (Exception x) { this.OnError(x); } } }
public void Stop() { if (!this.m_IsRunning) { return; } this.m_IsRunning = false; this.m_pQueuedPackets = null; foreach (Socket current in this.m_pSockets) { current.Close(); } this.m_pSockets = null; this.m_pSendSocketsIPv4 = null; this.m_pSendSocketsIPv6 = null; }
public void Start() { if (this.m_IsRunning) { return; } this.m_IsRunning = true; this.m_StartTime = DateTime.Now; this.m_pQueuedPackets = new Queue <UdpPacket>(); if (this.m_pBindings != null) { List <IPEndPoint> list = new List <IPEndPoint>(); IPEndPoint[] pBindings = this.m_pBindings; for (int i = 0; i < pBindings.Length; i++) { IPEndPoint iPEndPoint = pBindings[i]; if (iPEndPoint.Address.Equals(IPAddress.Any)) { IPEndPoint item = new IPEndPoint(IPAddress.Loopback, iPEndPoint.Port); if (!list.Contains(item)) { list.Add(item); } IPAddress[] hostAddresses = Dns.GetHostAddresses(""); for (int j = 0; j < hostAddresses.Length; j++) { IPAddress address = hostAddresses[j]; IPEndPoint item2 = new IPEndPoint(address, iPEndPoint.Port); if (!list.Contains(item2)) { list.Add(item2); } } } else { if (!list.Contains(iPEndPoint)) { list.Add(iPEndPoint); } } } this.m_pSockets = new List <Socket>(); foreach (IPEndPoint current in list) { try { this.m_pSockets.Add(UdpCore.CreateSocket(current, ProtocolType.Udp)); } catch (Exception x) { this.OnError(x); } } this.m_pSendSocketsIPv4 = new CircleCollection <Socket>(); this.m_pSendSocketsIPv6 = new CircleCollection <Socket>(); foreach (Socket current2 in this.m_pSockets) { if (((IPEndPoint)current2.LocalEndPoint).AddressFamily == AddressFamily.InterNetwork) { if (!((IPEndPoint)current2.LocalEndPoint).Equals(IPAddress.Loopback)) { this.m_pSendSocketsIPv4.Add(current2); } } else { if (((IPEndPoint)current2.LocalEndPoint).AddressFamily == AddressFamily.InterNetworkV6) { this.m_pSendSocketsIPv6.Add(current2); } } } Thread thread = new Thread(new ThreadStart(this.ProcessIncomingUdp)); thread.Start(); Thread thread2 = new Thread(new ThreadStart(this.ProcessQueuedPackets)); thread2.Start(); } }