JLHHJSvr.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 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. GlobalVar.webapp = false;
  31. GlobalVar.Init();
  32. XmlConfig xmlconfig = new XmlConfig();
  33. var strport = xmlconfig.GetXmlFileValue(GlobalVar.XmlFile, string.Empty, "port", "9075");
  34. var strHttpPort = xmlconfig.GetXmlFileValue(GlobalVar.XmlFile, string.Empty, "httpport", "9076");
  35. int port = int.Parse(strport);
  36. int httpport = int.Parse(strHttpPort);
  37. startHttpListener(httpport);
  38. _tcpListener = new TcpListener(IPAddress.Any, port);
  39. _tcpListener.Start();
  40. ThreadPool.QueueUserWorkItem((state) =>
  41. {
  42. running = true;
  43. // 侦听线程
  44. Thread.CurrentThread.IsBackground = false;
  45. try
  46. {
  47. while (running)
  48. {
  49. if (_tcpListener.Pending())
  50. {
  51. var c = _tcpListener.AcceptTcpClient();
  52. ThreadPool.QueueUserWorkItem((st) =>
  53. {
  54. try
  55. {
  56. Thread.CurrentThread.IsBackground = true;
  57. var handler = new P1Server(c, GlobalVar.server);
  58. handler.Handle();
  59. }
  60. catch (Exception ex)
  61. {
  62. Trace.Write("服务处理线程(直连)异常退出:" + ex.ToString());
  63. }
  64. DebugHelper.PrintAll();
  65. });
  66. }
  67. else
  68. {
  69. Thread.Sleep(100);
  70. }
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. Trace.Write("主侦听线程异常退出:" + ex.ToString());
  76. }
  77. });
  78. }
  79. catch (Exception ex)
  80. {
  81. Trace.Write(ex.ToString());
  82. }
  83. }
  84. protected override void OnStop()
  85. {
  86. running = false;
  87. }
  88. private void startHttpListener(int port)
  89. {
  90. #if DEBUG
  91. var httpServer = new SimpleHttpServer(port, GlobalVar.server, new GlobalVar.ParkFileModel());
  92. #else
  93. var httpServer = new SimpleHttpServer(port, GlobalVar.server, new GlobalVar.ParkFileModel());
  94. #endif
  95. httpServer.Listen();
  96. }
  97. }
  98. }