JLHHJSvr.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using LJLib.D;
  2. using LJLib.DAL.SQL;
  3. using LJLib.HttpServer;
  4. using LJLib.TcpHandle;
  5. using LJLib.Tools.File;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Serialization;
  8. using System;
  9. using System.Diagnostics;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.ServiceProcess;
  13. using System.Threading;
  14. namespace JLHHJSvr
  15. {
  16. partial class JLHHJSvr : ServiceBase
  17. {
  18. private TcpListener _tcpListener = null;
  19. private LJHttpServer _httpSvr = null;
  20. private static bool running = false;
  21. public JLHHJSvr()
  22. {
  23. InitializeComponent();
  24. }
  25. protected override void OnStart(string[] args)
  26. {
  27. try
  28. {
  29. DbSqlHelper.Add(new SqlServerEngine());
  30. var newtonsettings = new JsonSerializerSettings();
  31. newtonsettings.NullValueHandling = NullValueHandling.Ignore;
  32. newtonsettings.ContractResolver = new DefaultContractResolver();
  33. newtonsettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
  34. JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() =>
  35. {
  36. return newtonsettings;
  37. });
  38. GlobalVar.webapp = false;
  39. GlobalVar.Init();
  40. XmlConfig xmlconfig = new XmlConfig();
  41. var strport = xmlconfig.GetXmlFileValue(GlobalVar.XmlFile, string.Empty, "port", "9075");
  42. var strHttpPort = xmlconfig.GetXmlFileValue(GlobalVar.XmlFile, string.Empty, "httpport", "9076");
  43. int port = int.Parse(strport);
  44. int httpport = int.Parse(strHttpPort);
  45. startHttpListener(httpport);
  46. _tcpListener = new TcpListener(IPAddress.Any, port);
  47. _tcpListener.Start();
  48. ThreadPool.QueueUserWorkItem((state) =>
  49. {
  50. running = true;
  51. // 侦听线程
  52. Thread.CurrentThread.IsBackground = false;
  53. try
  54. {
  55. while (running)
  56. {
  57. if (_tcpListener.Pending())
  58. {
  59. var c = _tcpListener.AcceptTcpClient();
  60. ThreadPool.QueueUserWorkItem((st) =>
  61. {
  62. try
  63. {
  64. Thread.CurrentThread.IsBackground = true;
  65. var handler = new P1Server(c, GlobalVar.server);
  66. handler.Handle();
  67. }
  68. catch (Exception ex)
  69. {
  70. Trace.Write("服务处理线程(直连)异常退出:" + ex.ToString());
  71. }
  72. DebugHelper.PrintAll();
  73. });
  74. }
  75. else
  76. {
  77. Thread.Sleep(100);
  78. }
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. Trace.Write("主侦听线程异常退出:" + ex.ToString());
  84. }
  85. });
  86. }
  87. catch (Exception ex)
  88. {
  89. Trace.Write(ex.ToString());
  90. }
  91. }
  92. protected override void OnStop()
  93. {
  94. running = false;
  95. if (_httpSvr != null)
  96. {
  97. _httpSvr.Stop();
  98. }
  99. }
  100. private void startHttpListener(int port)
  101. {
  102. #if DEBUG
  103. _httpSvr = new SimpleHttpServer(port, GlobalVar.server, new GlobalVar.ParkFileModel());
  104. #else
  105. _httpSvr = new SimpleHttpServer(port, GlobalVar.server, new GlobalVar.ParkFileModel());
  106. #endif
  107. _httpSvr.Listen();
  108. }
  109. }
  110. }