示例#1
0
 /// <summary>
 /// Measure the state of our system
 /// </summary>
 private static void MeasureSystemState(object state)
 {
     while (true)
     {
         long Mem;
         MM_System_Profiling.MEMORYSTATUSEX MemStatus;
         SystemInformation.CPUUsage                = MM_System_Profiling.GetCPUUsage(out Mem, out MemStatus);
         SystemInformation.SystemFreeMemory        = MemStatus.ullAvailPhys;
         SystemInformation.SystemTotalMemory       = MemStatus.ullTotalPhys;
         SystemInformation.ApplicationMemoryUse    = Mem;
         SystemInformation.LineCount               = LineData.Length;
         SystemInformation.LoadCount               = LoadData.Length;
         SystemInformation.BusCount                = BusData.Count;
         SystemInformation.LineOutageCount         = LineOutageData.Count;
         SystemInformation.TransformerOutageCount  = TransformerOutageData.Count;
         SystemInformation.UnitOutageCount         = UnitOutageData.Count;
         SystemInformation.UnitCount               = UnitData.Length;
         SystemInformation.UnitGenCount            = UnitGenData.Length;
         SystemInformation.InterfaceCount          = InterfaceData.Length;
         SystemInformation.BasecaseCount           = BasecaseViolationData.Count;
         SystemInformation.ContingencyCount        = ContingencyViolationData.Count;
         SystemInformation.TransformerCount        = TransformerData.Length;
         SystemInformation.PhaseShifterCount       = TransformerData.Length;
         SystemInformation.BreakerSwitchCount      = BreakerSwitchData.Length;
         SystemInformation.TieCount                = TieData.Length;
         SystemInformation.SVCCount                = SVCData.Length;
         SystemInformation.UnitTypeCount           = UnitTypeGenerationData.Length;
         SystemInformation.SystemCount             = SystemWideGenerationData.Length;
         SystemInformation.LMPCount                = LmpData.Length;
         SystemInformation.FlowgateCount           = FlowgateData.Length;
         SystemInformation.AnalogCount             = AnalogMeasurementData.Length;
         SystemInformation.StateCount              = StateMeasurementData.Length;
         SystemInformation.SCADAStatusCount        = SCADAStatuses.Length;
         SystemInformation.SCADAAnalogCount        = SCADAAnalogs.Length;
         SystemInformation.LoadForecastCount       = LoadForecastData.Length;
         SystemInformation.UnitSimulationCount     = UnitSimulationData.Length;
         SystemInformation.SynchroscopeCount       = SynchroscopeData.Length;
         SystemInformation.UnitControlCount        = UnitControlStatusData.Count;
         SystemInformation.ShuntCompensatorCount   = ShuntCompensatorData.Length;
         SystemInformation.ZBRCount                = ZBRData.Length;
         SystemInformation.UserCount               = ConnectedUsers.Count;
         SystemInformation.CommandCount            = EMSCommands == null ? -1 : EMSCommands.Count;
         SystemInformation.OperatorshipUpdateCount = OperatorshipUpdates.Count;
         SystemInformation.LastTEDeAddress         = LastTEDEAddress;
         SystemInformation.LastTEDeUpdate          = LastTEDEUpdate;
         if (SimulatorTimeData.Length > 0)
         {
             SystemInformation.SimulationTime   = SimulatorTimeData[0].Simulation_Time;
             SystemInformation.SimulationStatus = SimulatorTimeData[0].Status;
         }
         Thread.Sleep(1000);
     }
 }
        /// <summary>
        /// Ping our Macomber Map servers
        /// </summary>
        internal static void StartPingMacomberMapServers()
        {
            Task.Run(() =>
            {
                var queryUdp = new UdpClient();
                queryUdp.ExclusiveAddressUse   = false;
                queryUdp.Client.ReceiveTimeout = 500;
                queryUdp.Client.SendTimeout    = 500;
                queryUdp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false);
                //queryUdp.Client.Bind(new IPEndPoint(IPAddress.Any, Settings.Default.MacomberMapServerQueryPort));

                int CurrentIter = 0;
                //if (Settings.Default.ServersToPing.IndexOf("localhost", StringComparison.CurrentCultureIgnoreCase) < 0)
                //    Settings.Default.ServersToPing += ",localhost";
                while (true)
                {
                    //First, once every 10 seconds, send our commands out for manual servers
                    if (CurrentIter == 0)
                    {
                        foreach (String str in Settings.Default.ServersToPing.Split(','))
                        {
                            try
                            {
                                foreach (IPAddress Addr in Dns.GetHostEntry(str).AddressList)
                                {
                                    if (Addr.AddressFamily == AddressFamily.InterNetwork)
                                    {
                                        byte[] OutBytes = new UTF8Encoding(false).GetBytes("Macomber Map Client|" + Application.ProductVersion + "|" + Environment.MachineName + "|");
                                        queryUdp.Send(OutBytes, OutBytes.Length, new IPEndPoint(Addr, Settings.Default.MacomberMapServerQueryPort));

                                        Task.Run(async() =>
                                        {
                                            var response = await queryUdp.ReceiveAsync();
                                            HandleUDPMessage(response);
                                        }).Wait(queryUdp.Client.ReceiveTimeout);
                                    }
                                }
                            }
                            catch (SocketException ex)
                            {
                                if (ex.ErrorCode == 10054)
                                {
                                    Debug.WriteLine("Server not available: " + str);
                                }
                            }
                        }
                    }
                    catch { }

                    //Every 5 seconds, ping our clients
                    if (CurrentIter % 5 == 0)
                    {
                        using (Ping p = new Ping())
                        {
                            foreach (MM_Server_Information Server in MMServers.Values.ToArray())
                            {
                                try
                                {
                                    var reply       = p.Send(Server.ServerURI.Host, 25);
                                    Server.PingTime = reply.RoundtripTime;
                                }
                                catch
                                {
                                    Server.PingTime = -1;
                                }
                            }
                            p.Dispose();
                        }
                    }
                    //Capture our CPU usage
                    try
                    {
                        long Mem;
                        MM_System_Profiling.MEMORYSTATUSEX MemStatus;
                        ClientStatus.CPUUsage             = MM_System_Profiling.GetCPUUsage(out Mem, out MemStatus);
                        ClientStatus.SystemFreeMemory     = MemStatus.ullAvailPhys;
                        ClientStatus.SystemTotalMemory    = MemStatus.ullTotalPhys;
                        ClientStatus.OpenWindows          = -1;
                        ClientStatus.ApplicationMemoryUse = Mem;

                        //Send CPU status every 2 seconds
                        if (CurrentIter % 2 == 0 && Client != null)
                        {
                            Client.ReportUserStatus(ClientStatus);
                        }
                    }
                    catch { }


                    //Wait a second
                    Thread.Sleep(1500);
                }
示例#3
0
        /// <summary>
        /// Initialize DirectX factories, swap chain and render targets.
        /// </summary>
        protected void InitGraphics()
        {
            var width  = Size.Width;
            var height = Size.Height;

            Factory2D          = new Factory();
            FactoryDirectWrite = new SharpDX.DirectWrite.Factory();

            var desc = new SwapChainDescription
            {
                BufferCount       = 1,
                ModeDescription   = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                IsWindowed        = true,
                OutputHandle      = Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput | Usage.BackBuffer,
            };

            // use software driver if RDP or VirtualMachine
            _driverType = MM_System_Profiling.IsTerminalOrVirtual() ? DriverType.Warp : DriverType.Hardware;
            bool swapChainCreated = false;

            while (!swapChainCreated)
            {
                try
                {
                    Device.CreateWithSwapChain(_driverType, DeviceCreationFlags.BgraSupport, desc, out _device, out _swapChain);
                    MM_System_Interfaces.LogError("Initialized DirectX Driver: {0}", _driverType);
                    swapChainCreated = true;
                }
                catch (Exception ex)
                {
                    // downgrade driver and try again using outer while loop
                    if (_driverType == DriverType.Hardware)
                    {
                        _driverType = DriverType.Warp;
                    }
                    else if (_driverType == DriverType.Warp)
                    {
                        _driverType = DriverType.Software;
                    }
                    else
                    {
                        MM_System_Interfaces.LogError(ex);
                        throw new NotSupportedException("DirectX Rendering could not be initialized using hardware of software.", ex);
                    }
                }
            }

            Context = _device.ImmediateContext;

            // Ignore all windows events
            FactoryDxgi = _swapChain.GetParent <SharpDX.DXGI.Factory>();
            FactoryDxgi.MakeWindowAssociation(Handle, WindowAssociationFlags.IgnoreAll);

            // New RenderTargetView from the backbuffer
            _backBuffer                  = Resource.FromSwapChain <Texture2D>(_swapChain, 0);
            RenderTargetView             = new RenderTargetView(_device, _backBuffer);
            DxgiSurface                  = _backBuffer.QueryInterface <Surface>();
            RenderTarget2D               = new RenderTarget(Factory2D, DxgiSurface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));
            RenderTarget2D.AntialiasMode = AntialiasMode;

            PresenterReady = true;
        }