예제 #1
0
        private void OnReceive(IAsyncResult ar)
        {
            var remoteIpEndPoint = new IPEndPoint(IPAddress.Any, GlobalVars.LPR_UDPPort);
            var datagram         = _udpServer.EndReceive(ar, ref remoteIpEndPoint);

            try
            {
                var delay = new TimeSpan(0, 0, 0, 1);
                var frame = new SntpFrame(datagram);

                frame.RearrangeForResponse(delay);
                var responseBytes = frame.ToBytes();

                SimpleService.WriteLog($"Receive time sync request from {remoteIpEndPoint.ToString()}.");
                _udpServer.Send(responseBytes, responseBytes.Length, remoteIpEndPoint);
                SimpleService.WriteLog($"Finished time sync to {remoteIpEndPoint.ToString()}.");
            }
            catch (Exception ex)
            {
                SimpleService.WriteLog(ex.Message);
            }
            finally
            {
                _udpServer.BeginReceive(OnReceive, null);
            }
        }
예제 #2
0
 public void Start()
 {
     TurnOffWindowTimeService();
     PerformTimeSync(GlobalVars.NTP_Server);
     if (GlobalVars.EnableTimeSyncService)
     {
         try
         {
             _udpServer = new UdpClient(GlobalVars.NTP_UDPPort);
             _udpServer.BeginReceive(OnReceive, null);
             SimpleService.WriteLog($"NTP Service is started at Port:{GlobalVars.NTP_UDPPort}");
         }
         catch (Exception ex)
         {
             if (ex.HResult.Equals(-2147467259)) //Open Port Error
             {
                 SimpleService.WriteLog($"Failed To Open Port {GlobalVars.NTP_UDPPort}, Turn Off Windows Time Service If It Is Running!");
             }
             else
             {
                 SimpleService.WriteLog(ex.Message + Environment.NewLine + ex.StackTrace);
             }
         }
     }
 }
예제 #3
0
 public void Dispose()
 {
     try
     {
         _udpServer = null;
         _udpClient = null;
     }
     catch (Exception ex)
     {
         SimpleService.WriteLog(ex.Message + Environment.NewLine + ex.StackTrace);
         throw;
     }
 }
예제 #4
0
 private void PerformTimeSync(string TimeServer)
 {
     try
     {
         SimpleService.WriteLog($"Connecting to Time Server: {TimeServer}");
         _udpClient = new NTPClient(TimeServer);
         _udpClient.Connect(true);
         SimpleService.WriteLog($"{_udpClient.ToString()}");
     }
     catch (Exception ex)
     {
         SimpleService.WriteLog($"ERROR: {ex.Message}");
     }
 }
예제 #5
0
        public static void Start(string[] args)
        {
            m_consoleMode = args != null && args.Length > 0;

            if (m_worker == null)
            {
                WriteLog("Null worker", EventLogEntryType.Error);
            }

            if (m_consoleMode)
            {
                if (args.Length > 1)
                {
                    WriteLog("Only one argument is allowed");
                    ShowHelp();
                    return;
                }

                switch (args[0].Trim().ToLower())
                {
                case "-console":
                case "-consol":
                case "-conso":
                case "-cons":
                case "-con":
                case "-co":
                case "-c":
                    break;

                case "-install":
                case "-instal":
                case "-insta":
                case "-inst":
                case "-ins":
                case "-in":
                case "-i":
                    ManagedInstallerClass.InstallHelper(
                        new string[] { Assembly.GetExecutingAssembly().Location }
                        );
                    return;

                case "-uninstall":
                case "-uninstal":
                case "-uninsta":
                case "-uninst":
                case "-unins":
                case "-unin":
                case "-uni":
                case "-un":
                case "-u":
                    ManagedInstallerClass.InstallHelper(
                        new string[] { "/u", Assembly.GetExecutingAssembly().Location }
                        );
                    return;

                case "-start":
                case "-star":
                    using (ServiceController oController = new ServiceController(SimpleServiceInstaller.ServiceName))
                    {
                        oController.Start();
                    }
                    return;

                case "-stop":
                case "-sto":
                    using (ServiceController oController = new ServiceController(SimpleServiceInstaller.ServiceName))
                    {
                        oController.Stop();
                    }
                    return;

                case "-status":
                case "-statu":
                case "-stat":
                    using (ServiceController oController = new ServiceController(SimpleServiceInstaller.ServiceName))
                    {
                        try
                        {
                            WriteLog(oController.Status.ToString());
                        }
                        catch (Win32Exception e)
                        {
                            WriteLog(e);
                        }
                        catch (InvalidOperationException e)
                        {
                            WriteLog(e);
                        }
                    }
                    return;

                case "-help":
                case "-hel":
                case "-he":
                case "-h":
                case "-?":
                    ShowHelp();
                    return;

                default:
                    WriteLog(string.Format("Invalid argument: {0}", args[0]));
                    ShowHelp();
                    return;
                }
            }

            using (EventWaitHandle threadFinish = new EventWaitHandle(false, EventResetMode.ManualReset))
            {
                Thread thread = null;
                try
                {
                    m_worker.Init();
                    thread = new Thread(() =>
                    {
                        try
                        {
                            m_worker.Run();
                        }
                        catch (ThreadAbortException)
                        {
                            SimpleService.WriteLog(string.Format("{0} stopping", SimpleServiceInstaller.ServiceName));
                        }
                        finally
                        {
                            threadFinish.Set();
                        }
                    });
                    thread.Start();

                    if (m_consoleMode)
                    {
                        // Console.TreatControlCAsInput = true;
                        WriteLog("Press the Escape (Esc) key to quit:");
                        while (Console.ReadKey(true).Key != ConsoleKey.Escape)
                        {
                            ;
                        }
                    }
                    else
                    {
                        ServiceBase[] ServicesToRun = new ServiceBase[] { new ServiceBase {
                                                                              ServiceName = SimpleServiceInstaller.ServiceName, AutoLog = true
                                                                          } };
                        ServiceBase.Run(ServicesToRun);
                    }
                }
                catch (Exception e)
                {
                    WriteLog(e);
                }
                finally
                {
                    try
                    {
                        if (thread != null)
                        {
                            thread.Abort();
                            thread = null;
                        }
                    }
                    catch (Exception e)
                    {
                        WriteLog(e);
                    }
                    finally
                    {
                        Mutex.WaitAll(new EventWaitHandle[] { threadFinish }, Timeout.Infinite);
                        m_worker.Cleanup();
                    }
                }
            }
        }