private void SendData2(IntPtr hWnd, int id, queueData qData) { COPYDATASTRUCT2 data = new COPYDATASTRUCT2(); data.dwData = id; // 3; // whatever value (have to match receiver end) data.cbData = 256; //System.Runtime.InteropServices.Marshal.SizeOf(UserData2);// size of UserData struct UserData2 userData = new UserData2(); if (qData.sIP.Length > 240) { qData.sIP = qData.sIP.Substring(0, 240); } userData.sIP = qData.sIP; userData.iPingCount = qData.iPingCount; userData.iPingReplies = qData.iPingReplies; userData.iPingReplyTime = qData.iPingReplyTime; userData.iPingTimeout = qData.iPingTimeout; //copy data to unmanaged pointers data.lpData = Marshal.AllocCoTaskMem(data.cbData); Marshal.StructureToPtr(userData, data.lpData, false); IntPtr lpData = Marshal.AllocCoTaskMem(Marshal.SizeOf(data)); Marshal.StructureToPtr(data, lpData, false); Message msg1 = Message.Create(hWnd, WM_COPYDATA, lpData, lpData); MessageWindow.SendMessage(ref msg1); Marshal.FreeCoTaskMem(data.lpData); Marshal.FreeCoTaskMem(lpData); }
public int addHost2Ping(string s) { int iRet = 0; queueData theData = new queueData(); theData.sHost = s; //theData.sIP=s; lock (theQueue.SyncRoot)// syncedCollection.SyncRoot) { theQueue.Enqueue(theData); numberOfQueuedData++; } return iRet; }
private void NotifyData(queueData qData) { BgThreadEventArgs _bgThread2EventArgs; //is there any subscriber if (this.bgThread2Event == null) { return; } try { _bgThread2EventArgs = new BgThreadEventArgs(qData); this.bgThread2Event(this, _bgThread2EventArgs); } catch (MissingMethodException) { } }
private void NotifyData(UserData2 u2Data) { BgThreadEventArgs _bgThread2EventArgs; //is there any subscriber if (this.bgThread2Event == null) { return; } try { queueData qData = new queueData(); qData.sIP = u2Data.sIP; qData.iPingCount = u2Data.iPingCount; qData.iPingReplies = u2Data.iPingReplies; qData.iPingReplyTime = u2Data.iPingReplyTime; qData.iPingTimeout = u2Data.iPingTimeout; _bgThread2EventArgs = new BgThreadEventArgs(qData); this.bgThread2Event(this, _bgThread2EventArgs); } catch (MissingMethodException) { } }
public static int PingQdata(ref queueData qData) { IntPtr ICMPHandle; Int32 iIP; String sData; ICMP_OPTIONS oICMPOptions = new ICMP_OPTIONS(); ICMP_ECHO_REPLY ICMPReply = new ICMP_ECHO_REPLY(); Int32 iReplies; int[] roundTrips = new int[qData.iPingCount]; int averageRoundTrip = 0; //reset ping reply count qData.iPingReplies = 0; /* * qData.iPingTimeout = (byte)qData.iPingTimeout; * qData.sIP = IP.ToString(); * qData.IP = IP; */ ICMPHandle = IcmpCreateFile(); iIP = BitConverter.ToInt32(qData.IP.GetAddressBytes(), 0); sData = "x"; oICMPOptions.Ttl = (byte)qData.iPingTimeout; //time to live for (int i = 0; i < qData.iPingCount; i++) { iReplies = IcmpSendEcho(ICMPHandle, iIP, sData, sData.Length, ref oICMPOptions, ref ICMPReply, Marshal.SizeOf(ICMPReply), 30); if (iReplies == 1) { roundTrips[i] = ICMPReply.RoundTripTime; qData.iPingReplies++; } else { roundTrips[i] = -1; } } //calc average int counts = 0; for (int i = 0; i < qData.iPingCount; i++) { if (roundTrips[i] != -1) { averageRoundTrip += roundTrips[i]; counts++; } } if (counts > 0) { averageRoundTrip = averageRoundTrip / counts; } else { averageRoundTrip = 0; } qData.iPingReplyTime = averageRoundTrip; IcmpCloseHandle(ICMPHandle); return(qData.iPingReplies); }
public BgThreadEventArgs(queueData data) { this.qData = data; }
private void myThreadStart() { queueData _qData = new queueData(); System.Diagnostics.Debug.WriteLine("Entering thread proc"); int iPreplies = 0; int _i = 0; try { do { //The blocking function... if (_theQueue.Count > 0) { //dequeue one IP to ping lock (_theQueue.SyncRoot) // syncedCollection.SyncRoot) { _qData = (queueData)_theQueue.Dequeue(); // get object from queue } //System.Net.IPAddress ip; try { _qData.IP = System.Net.Dns.Resolve(_qData.sHost).AddressList[0]; iPreplies = myPing.PingQdata(ref _qData); } catch (Exception x) { System.Diagnostics.Debug.WriteLine("bgThread2: Exception in GetHostEntry(): " + x.Message); //invalid host, unable to get IP iPreplies = 0; } System.Diagnostics.Debug.WriteLine("Calling into UI thread..."); //Microsoft.WindowsCE.Forms.Message msg = Message.Create(bgWnd.Hwnd, msgID, new IntPtr(_i), IntPtr.Zero); int id = _i; //a simple counter //_qData.iPingReplies = iReply; //SendData(bgWnd.Hwnd, id, "ping replies=" + iReply.ToString()); //SendData2(bgWnd.Hwnd, id, _qData); _i++; _qData._iCount = _i; //need to know if this is the last queued data this.NotifyData(_qData); System.Diagnostics.Debug.WriteLine("Thread sleeps..."); } Thread.Sleep(1000); } while (bRunThread); } catch (ThreadAbortException) { System.Diagnostics.Debug.WriteLine("Thread will abort"); bRunThread = false; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception in ThreadStart: " + ex.Message); } System.Diagnostics.Debug.WriteLine("ThreadProc ended"); }
private void SendData2(IntPtr hWnd, int id, queueData qData) { COPYDATASTRUCT2 data = new COPYDATASTRUCT2(); data.dwData = id;// 3; // whatever value (have to match receiver end) data.cbData = 256; //System.Runtime.InteropServices.Marshal.SizeOf(UserData2);// size of UserData struct UserData2 userData = new UserData2(); if (qData.sIP.Length > 240) { qData.sIP = qData.sIP.Substring(0, 240); } userData.sIP = qData.sIP; userData.iPingCount = qData.iPingCount; userData.iPingReplies = qData.iPingReplies; userData.iPingReplyTime = qData.iPingReplyTime; userData.iPingTimeout = qData.iPingTimeout; //copy data to unmanaged pointers data.lpData = Marshal.AllocCoTaskMem(data.cbData); Marshal.StructureToPtr( userData, data.lpData, false); IntPtr lpData = Marshal.AllocCoTaskMem(Marshal.SizeOf(data)); Marshal.StructureToPtr(data, lpData, false); Message msg1 = Message.Create(hWnd, WM_COPYDATA, lpData, lpData); MessageWindow.SendMessage(ref msg1); Marshal.FreeCoTaskMem(data.lpData); Marshal.FreeCoTaskMem(lpData); }
private void myThreadStart() { queueData _qData = new queueData(); System.Diagnostics.Debug.WriteLine("Entering thread proc"); int iPreplies = 0; int _i=0; try { do { //The blocking function... if (_theQueue.Count > 0) { //dequeue one IP to ping lock (_theQueue.SyncRoot)// syncedCollection.SyncRoot) { _qData = (queueData)_theQueue.Dequeue();// get object from queue } //System.Net.IPAddress ip; try { _qData.IP = System.Net.Dns.Resolve(_qData.sHost).AddressList[0]; iPreplies = myPing.PingQdata(ref _qData); } catch (Exception x) { System.Diagnostics.Debug.WriteLine("bgThread2: Exception in GetHostEntry(): " + x.Message); //invalid host, unable to get IP iPreplies = 0; } System.Diagnostics.Debug.WriteLine("Calling into UI thread..."); //Microsoft.WindowsCE.Forms.Message msg = Message.Create(bgWnd.Hwnd, msgID, new IntPtr(_i), IntPtr.Zero); int id = _i; //a simple counter //_qData.iPingReplies = iReply; //SendData(bgWnd.Hwnd, id, "ping replies=" + iReply.ToString()); //SendData2(bgWnd.Hwnd, id, _qData); _i++; _qData._iCount = _i; //need to know if this is the last queued data this.NotifyData(_qData); System.Diagnostics.Debug.WriteLine("Thread sleeps..."); } Thread.Sleep(1000); } while (bRunThread); } catch (ThreadAbortException) { System.Diagnostics.Debug.WriteLine("Thread will abort"); bRunThread = false; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception in ThreadStart: " + ex.Message); } System.Diagnostics.Debug.WriteLine("ThreadProc ended"); }
public static int PingQdata(ref queueData qData) { IntPtr ICMPHandle; Int32 iIP; String sData; ICMP_OPTIONS oICMPOptions = new ICMP_OPTIONS(); ICMP_ECHO_REPLY ICMPReply = new ICMP_ECHO_REPLY(); Int32 iReplies; int[] roundTrips= new int[qData.iPingCount]; int averageRoundTrip=0; //reset ping reply count qData.iPingReplies = 0; /* qData.iPingTimeout = (byte)qData.iPingTimeout; qData.sIP = IP.ToString(); qData.IP = IP; */ ICMPHandle = IcmpCreateFile(); iIP = BitConverter.ToInt32(qData.IP.GetAddressBytes(), 0); sData = "x"; oICMPOptions.Ttl = (byte) qData.iPingTimeout; //time to live for(int i=0; i<qData.iPingCount; i++){ iReplies = IcmpSendEcho(ICMPHandle, iIP, sData, sData.Length, ref oICMPOptions, ref ICMPReply, Marshal.SizeOf(ICMPReply), 30); if (iReplies == 1) { roundTrips[i] = ICMPReply.RoundTripTime; qData.iPingReplies++; } else roundTrips[i] = -1; } //calc average int counts = 0; for (int i = 0; i < qData.iPingCount; i++) { if (roundTrips[i] != -1) { averageRoundTrip += roundTrips[i]; counts++; } } if(counts>0) averageRoundTrip = averageRoundTrip / counts; else averageRoundTrip = 0; qData.iPingReplyTime = averageRoundTrip; IcmpCloseHandle(ICMPHandle); return qData.iPingReplies; }