CentralForWebSocketServer.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using parkMonitor.entity;
  7. using System.Configuration;
  8. using System.Net.Sockets;
  9. using System.Net;
  10. using System.Threading;
  11. using parkMonitor.tools;
  12. namespace WebServer
  13. {
  14. /// <summary>
  15. /// web线程类,接收用户指令
  16. /// </summary>
  17. public class CentralForWeb : IWebServer
  18. {
  19. public MultiSocketThread multiSocketThread { set; get; }
  20. public static BlockingQueue blockingQueue = new BlockingQueue();
  21. Int32 port;
  22. IPAddress localAddr;
  23. bool isClosing;
  24. //public AbstractMessage GetMessage()
  25. //{
  26. // return (AbstractMessage)blockingQueue.Dequeue();
  27. //}
  28. private void WebConnect(TcpListener listener)
  29. {
  30. try
  31. {
  32. if (listener != null)
  33. {
  34. //得到包含客户端信息的套接字
  35. var tcpClient = listener.AcceptTcpClient();
  36. //创建消息服务线程对象
  37. //把multiSocket类的run方法委托给线程
  38. multiSocketThread = new MultiSocketThread(tcpClient);
  39. Thread newThread = new Thread(multiSocketThread.Run);
  40. newThread.IsBackground = true;
  41. newThread.Start();
  42. }
  43. }
  44. catch (Exception e)
  45. {
  46. if (multiSocketThread != null)
  47. multiSocketThread.Close();
  48. Console.WriteLine(e.Message);
  49. throw new Exception();
  50. }
  51. }
  52. private void Run()
  53. {
  54. bool linked = false;
  55. Task.Factory.StartNew(() =>
  56. {
  57. TcpListener listener = null;
  58. try
  59. {
  60. listener = LazySingleton.GetInstance(localAddr, port);
  61. listener.Start();
  62. linked = true;
  63. }
  64. catch (Exception) { Console.WriteLine("未能与Web服务器连接,本地ip错误或网络异常"); linked = false; }
  65. while (isClosing)
  66. {
  67. try
  68. {
  69. while (linked)
  70. {
  71. WebConnect(listener);
  72. }
  73. if (!linked)
  74. {
  75. throw new Exception();
  76. }
  77. }
  78. catch (Exception)
  79. {
  80. //Task.Factory.StartNew(() =>
  81. //{
  82. MyTimer mt = new MyTimer();
  83. mt.StartTiming();
  84. while (!linked)
  85. {
  86. Thread.Sleep(10000);
  87. try
  88. {
  89. listener = LazySingleton.GetInstance(localAddr, port);
  90. listener.Start();
  91. linked = true;
  92. Console.WriteLine("web已成功重连");
  93. break;
  94. }
  95. catch (Exception)
  96. {
  97. mt.EndTiming();
  98. int count = 0;
  99. if (mt.IsLonger(30, 1, true, out count))
  100. {
  101. Console.WriteLine("未能与Web服务器连接,本地ip错误或网络异常");
  102. }
  103. }
  104. }
  105. //});
  106. }
  107. Thread.Sleep(5000);
  108. }
  109. if (multiSocketThread != null)
  110. multiSocketThread.Close();
  111. });
  112. }
  113. private void DownloadAds()
  114. {
  115. }
  116. /// <summary>
  117. /// 启动
  118. /// </summary>
  119. public bool Start(int port)
  120. {
  121. //string strTemp = ConfigurationManager.AppSettings["WebConfig"];
  122. //string[] strArray = strTemp.Split(':');
  123. //string ipstr = strArray[0];
  124. //string portstr = strArray[1];
  125. //获取本机ip地址
  126. try
  127. {
  128. TcpClient client = new TcpClient();
  129. client.Connect("www.baidu.com", 80);
  130. string ip = ((IPEndPoint)client.Client.LocalEndPoint).Address.ToString();
  131. client.Close();
  132. localAddr = IPAddress.Parse(ip);
  133. this.port = port;//Convert.ToInt32(ConfigurationManager.AppSettings["webPort"]);
  134. Console.WriteLine(ip.ToString());
  135. }
  136. catch (Exception e) { Console.WriteLine("wrong ipAddr");return false; }
  137. Run();
  138. return true;
  139. }
  140. /// <summary>
  141. /// 停止
  142. /// </summary>
  143. public void Stop()
  144. {
  145. isClosing = true;
  146. }
  147. /// <summary>
  148. /// 停车预约
  149. /// </summary>
  150. public void ParkBooking()
  151. {
  152. }
  153. /// <summary>
  154. /// 取车预约
  155. /// </summary>
  156. public void FetchBooking()
  157. {
  158. }
  159. /// <summary>
  160. /// 预约停车记录
  161. /// </summary>
  162. public void bookParkRecord()
  163. {
  164. }
  165. /// <summary>
  166. /// 预约取车记录
  167. /// </summary>
  168. public void bookFetchRecord()
  169. {
  170. }
  171. }
  172. }