JLHHJSvr.cs 3.6 KB

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