Exemplo n.º 1
0
        public static void ReadCallback(IAsyncResult ar)
        {
            try
            {
                String content = String.Empty;

                // Retrieve the state object and the handler socket
                // from the asynchronous state object.
                StateObject state   = (StateObject)ar.AsyncState;
                Socket      handler = state.workSocket;

                // Read data from the client socket.
                int bytesRead = handler.EndReceive(ar);

                // There  might be more data, so store the data received so far.
                //state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
                state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead));

                if (handler.Available > 0)
                {
                    // Not all data received. Get more.
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
                }
                else
                {
                    content = state.sb.ToString();

                    // All the data has been read from the
                    // client. Display it on the console.
                    //Console.WriteLine("Read {0} bytes from socket.", content.Length);
                    // Log.WriteToLog(string.Format("Read {0} bytes from socket.", content.Length));

                    if (content.Length > 0)
                    {
                        QueryData receviedQueryData = QueryData.Deserialize(content);

                        OnReciveMessage(new MessageEventArgs(receviedQueryData));

                        ReplyData returnedReplyData = null;

                        switch (receviedQueryData.Type)
                        {
                        case Consts.SectionType.Message:
                            returnedReplyData = MessageBL.executeQuery(receviedQueryData);
                            // MessageBL.executeQuery(receviedQueryData);
                            break;

                        case Consts.SectionType.Processes:
                            returnedReplyData = ProcessesBL.executeQuery(receviedQueryData);
                            break;

                        case Consts.SectionType.Performance:
                            returnedReplyData = PerformanceBL.executeQuery(receviedQueryData);
                            break;

                        case Consts.SectionType.Properties:
                            returnedReplyData = PropertiesBL.executeQuery(receviedQueryData);
                            break;

                        case Consts.SectionType.SysInfo:
                            returnedReplyData = SysInfoBL.executeQuery(receviedQueryData);
                            break;

                        case Consts.SectionType.Status:
                            returnedReplyData = new ReplyData();
                            returnedReplyData.ArrDataContainers = new ArrayList();
                            Counter cnter = new Counter();
                            cnter.Name  = "UserName";
                            cnter.Value = Environment.UserName;

                            Counter cnterhostName = new Counter();
                            cnterhostName.Name  = "HostName";
                            cnterhostName.Value = Environment.UserName;

                            var dc = new DataContainer("127.0.0.1");
                            dc.ArrCounters = new ArrayList();
                            dc.ArrCounters.Add(cnter);
                            dc.ArrCounters.Add(cnterhostName);
                            returnedReplyData.ArrDataContainers.Add(dc);
                            break;

                        case Consts.SectionType.CaptureScreen:

                            if (CheckScreenCapture)
                            {
                                if (LastScreenCapture != null)
                                {
                                    returnedReplyData      = new ReplyData();
                                    returnedReplyData.Type = Consts.SectionType.CaptureScreen;

                                    returnedReplyData.ArrDataContainers.Add(LastScreenCapture);
                                }
                            }
                            else
                            {
                                returnedReplyData = CaptureScreenBL.executeQuery(receviedQueryData);
                            }
                            break;

                        case Consts.SectionType.ExplorerLogicalDrive:

                            foreach (ObjectMetaData CurrCounter in receviedQueryData.ArrCounter)
                            {
                                switch (CurrCounter.Tag)
                                {
                                case "ViewPath":

                                    try
                                    {
                                        DataContainer SysLogicalDirectoryDataContainer = new DataContainer("ViewPath");

                                        System.IO.DirectoryInfo dinfo = new System.IO.DirectoryInfo(CurrCounter.Text);


                                        System.IO.DirectoryInfo[] AllDirectoryInfo = dinfo.GetDirectories();

                                        foreach (System.IO.DirectoryInfo strDir in AllDirectoryInfo)
                                        {
                                            SysLogicalDirectoryDataContainer.ArrCounters.Add(new Counter("FolderName", strDir.Name + '-' + strDir.FullName + '-' + " "));
                                        }

                                        foreach (System.IO.FileInfo strFile in dinfo.GetFiles())
                                        {
                                            SysLogicalDirectoryDataContainer.ArrCounters.Add(new Counter("FileName", strFile.Name + '-' + strFile.FullName + '-' + " "));
                                        }

                                        if (returnedReplyData == null)
                                        {
                                            returnedReplyData = new ReplyData();
                                        }

                                        returnedReplyData.ArrDataContainers.Add(SysLogicalDirectoryDataContainer);
                                    }
                                    catch
                                    { }
                                    break;

                                case "OSLogicalDrives":
                                    DataContainer SysLogicalDrivesDataContainer = new DataContainer("OSLogicalDrives");

                                    System.IO.DriveInfo[] AllDriveInfo = System.IO.DriveInfo.GetDrives();


                                    foreach (System.IO.DriveInfo CurrLogicalDrive in System.IO.DriveInfo.GetDrives())
                                    {
                                        try
                                        {
                                            if (CurrLogicalDrive.Name == "A:\\")          //Added by rm
                                            {
                                                // if (!CurrLogicalDrive.IsReady)
                                                SysLogicalDrivesDataContainer.ArrCounters.Add(new Counter("DiskName", CurrLogicalDrive.Name + '-' + CurrLogicalDrive.Name + '-' + "Not accessable" + '-' + "Not accessable"));
                                                continue;
                                            }


                                            if (CurrLogicalDrive.IsReady)
                                            {
                                                decimal FreeSpacePercent = decimal.Divide(CurrLogicalDrive.TotalFreeSpace, CurrLogicalDrive.TotalSize) * 10000;
                                                int     tempNum          = Convert.ToInt32(FreeSpacePercent);
                                                long    TotalSizeMB      = CurrLogicalDrive.TotalSize / 1000000;
                                                FreeSpacePercent = decimal.Divide(tempNum, 100);
                                                SysLogicalDrivesDataContainer.ArrCounters.Add(new Counter("DiskName", CurrLogicalDrive.Name + '-' + CurrLogicalDrive.Name + '-' + FreeSpacePercent.ToString() + "%" + '-' + TotalSizeMB.ToString("#,#", System.Globalization.CultureInfo.InvariantCulture)));
                                            }
                                            else
                                            {
                                                SysLogicalDrivesDataContainer.ArrCounters.Add(new Counter("DiskName", CurrLogicalDrive.Name + '-' + CurrLogicalDrive.Name + '-' + "Not accessable" + '-' + "Not accessable"));
                                            }
                                        }
                                        catch (Exception)
                                        {
                                            SysLogicalDrivesDataContainer.ArrCounters.Add(new Counter("DiskName", CurrLogicalDrive.Name + '-' + CurrLogicalDrive.Name + '-' + "Not accessable" + '-' + "Not accessable"));
                                        }
                                    }

                                    if (returnedReplyData == null)
                                    {
                                        returnedReplyData = new ReplyData();
                                    }

                                    returnedReplyData.ArrDataContainers.Add(SysLogicalDrivesDataContainer);
                                    break;

                                default:
                                    break;
                                }
                            }



                            // returnedReplyData.ArrDataContainers.Add(SysLogicalDrivesDataContainer);

                            break;

                        default:
                            break;
                        }

                        try
                        {
                            // Echo the data back to the client.
                            if (returnedReplyData != null)
                            {
                                Send(handler, returnedReplyData.Serialize());
                            }
                            else
                            {
                            }
                        }
                        catch (SocketException e)
                        {
                            Log.WriteToLog(e.ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteToLog(ex.ToString());
            }
        }