Example #1
0
    void MessageReceivedCallback(IAsyncResult result)
    {
        EndPoint remoteEndPoint = new IPEndPoint(0, 0);

        //IPEndPoint LocalIPEndPoint = new IPEndPoint(IPAddress.Any, 3001);
        //EndPoint LocalEndPoint = (EndPoint)LocalIPEndPoint;
        //IPEndPoint remoteEP = (IPEndPoint)LocalEndPoint;
        //System.Diagnostics.Debug.WriteLine("Remote IP: " + remoteEP.Address.ToString());

        try
        {
            //all data should fit in one package!
            int bytesRead = receiveSocket.EndReceiveFrom(result, ref remoteEndPoint);
            //System.Diagnostics.Debug.WriteLine("Remote IP: " + ((IPEndPoint)(remoteEndPoint)).Address.ToString());

            byte[] bData = new byte[bytesRead];
            Array.Copy(recBuffer, bData, bytesRead);
            if (ByteHelper.isEndOfTransfer(bData))
                updateEndOfTransfer();// end of transfer
            else
            {
                ProcessStatistics.process_statistics stats = new ProcessStatistics.process_statistics(bData);
                stats.remoteIP = ((IPEndPoint)(remoteEndPoint)).Address.ToString();
                //System.Diagnostics.Debug.WriteLine( stats.dumpStatistics() );
                updateStatus(stats);
            }
        }
        catch (SocketException e)
        {
            System.Diagnostics.Debug.WriteLine(String.Format("MessageReceivedCallback SocketException: {0} {1}", e.ErrorCode, e.Message));
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine(String.Format("MessageReceivedCallback Exception: {0}", e.Message));
        }
        try
        {
            //ready to receive next packet
            receiveSocket.BeginReceiveFrom(recBuffer, 0, recBuffer.Length, SocketFlags.None, ref bindEndPoint, new AsyncCallback(MessageReceivedCallback), (object)this);
        }
        catch (Exception) { }
    }
Example #2
0
        /// <summary>
        /// build thread and process list periodically and fire update event and enqueue results for the socket thread
        /// </summary>
        void usageThread()
        {
            try
            {
                int interval = 3000;

                uint start = Process.GetTickCount();
                Dictionary<uint, thread> old_thread_List;// = Process.GetThreadList();

                string exeFile = Process.exefile;
                //read all processes
                Dictionary<uint, process> ProcList = Process.getProcessNameList();
                DateTime dtCurrent = DateTime.Now;

                //######### var declarations
                Dictionary<uint, thread> new_ThreadList;
                uint duration;
                long system_total;
                long user_total, kernel_total;      //total process spend in user/kernel
                long thread_user, thread_kernel;    //times the thread spend in user/kernel
                DWORD dwProc;
                float user_percent;
                float kernel_percent;    
                ProcessStatistics.process_usage usage;
                ProcessStatistics.process_statistics stats = null;
                
                string sProcessName = "";
                List<thread> processThreadList = new List<thread>();

                //extended list
                List<threadStatistic> processThreadStatsList = new List<threadStatistic>(); //to store thread stats
                //threadtimes threadTimesTotal;
                while (!bStopMainThread)
                {
                    eventEnableCapture.WaitOne();
                    old_thread_List = Process.GetThreadList();  //build a list of threads with user and kernel times

                    System.Threading.Thread.Sleep(interval);

                    //get a new thread list
                    new_ThreadList = Process.GetThreadList();   //build another list of threads with user and kernel times, to compare

                    duration = Process.GetTickCount() - start;

                    ProcList = Process.getProcessNameList();    //update process list
                    dtCurrent = DateTime.Now;
                    system_total = 0;
                    statisticsTimes.Clear();
                    //look thru all processes
                    foreach (KeyValuePair<uint, process> p2 in ProcList)
                    {
                        //empty the process's thread list
                        processThreadList=new List<thread>();
                        processThreadStatsList = new List<threadStatistic>();

                        user_total     = 0;  //hold sum of thread user times for a process
                        kernel_total   = 0;  //hold sum of thread kernel times for a process
                        sProcessName = p2.Value.sName;

                        //SUM over all threads with that ProcID
                        dwProc = p2.Value.dwProcID;
                        foreach (KeyValuePair<uint, thread> kpNew in new_ThreadList)
                        {
                            thread_user = 0;
                            thread_kernel = 0;
                            //if the thread belongs to the process
                            if (kpNew.Value.dwOwnerProcID == dwProc)
                            {
                                //is there an old thread entry we can use to calc?
                                thread threadOld;
                                if (old_thread_List.TryGetValue(kpNew.Value.dwThreadID, out threadOld))
                                {
                                    thread_user=Process.GetThreadTick(kpNew.Value.thread_times.user) - Process.GetThreadTick(old_thread_List[kpNew.Value.dwThreadID].thread_times.user);
                                    user_total += thread_user;
                                    thread_kernel =Process.GetThreadTick(kpNew.Value.thread_times.kernel) - Process.GetThreadTick(old_thread_List[kpNew.Value.dwThreadID].thread_times.kernel);
                                    kernel_total += thread_kernel;
                                }
                                //simple list
                                thread threadsOfProcess = new thread(kpNew.Value.dwOwnerProcID, kpNew.Value.dwThreadID, kpNew.Value.thread_times);
                                processThreadList.Add(threadsOfProcess);
                                
                                //extended list
                                threadStatistic threadStats = 
                                    new threadStatistic(
                                        kpNew.Value.dwOwnerProcID, 
                                        kpNew.Value.dwThreadID, 
                                        new threadtimes(thread_user, thread_kernel), 
                                        duration, 
                                        dtCurrent.Ticks);
                                processThreadStatsList.Add(threadStats);

                            }//if dwProcID matches
                        }
                        //end of sum for process
                        user_percent      = (float)user_total / (float)duration * 100f;
                        kernel_percent    = (float)kernel_total / (float)duration * 100f;
                        system_total = user_total + kernel_total;

                        // update the statistics with this process' info
                        usage = new ProcessStatistics.process_usage(kernel_total, user_total);
                        // update process statistics
                        //stats = new ProcessStatistics.process_statistics(p2.Value.dwProcID, p2.Value.sName, usage, dtCurrent.Ticks, duration, processThreadList);
                        stats = new ProcessStatistics.process_statistics(p2.Value.dwProcID, p2.Value.sName, usage, dtCurrent.Ticks, duration, processThreadStatsList);
                        
                        //add or update the proc stats
                        if (exeFile != p2.Value.sName || bIncludeMySelf)
                        {
                            //if (sProcessName == "device.exe")
                            //    System.Diagnostics.Debug.WriteLine(stats.ToString());

                            statisticsTimes[p2.Value.sName] = stats;
                            //lock (lockQueue)
                            //{
                                //System.Diagnostics.Debug.WriteLine("Queue Adding " + stats.sName);
                                //procStatsQueue.Enqueue(stats);
                                procStatsQueueBytes.Enqueue(stats.ToByte());
                            //}
                        }
                        
                        start = Process.GetTickCount();
                    }//foreach process
 
                    onUpdateHandler(new ProcessStatsEventArgs(statisticsTimes, duration));
                    procStatsQueueBytes.Enqueue(ByteHelper.endOfTransferBytes);
                    ((AutoResetEvent)eventEnableSend).Set();
                    //dumpStatistics(statisticsTimes);
                }//while true
            }
            catch (ThreadAbortException ex)
            {
                System.Diagnostics.Debug.WriteLine("ThreadAbortException: usageThread(): " + ex.Message);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception: usageThread(): " + ex.Message);
            }
            System.Diagnostics.Debug.WriteLine("Thread ENDED");
        }