CentralForWebSocketServer.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. private Queue<MessageUTF8> reserveQueue = null;
  22. private object reserveLock = new object();
  23. Int32 port;
  24. IPAddress localAddr;
  25. bool isClosing;
  26. //public AbstractMessage GetMessage()
  27. //{
  28. // return (AbstractMessage)blockingQueue.Dequeue();
  29. //}
  30. private void WebConnect(TcpListener listener)
  31. {
  32. try
  33. {
  34. if (listener != null)
  35. {
  36. //得到包含客户端信息的套接字
  37. var tcpClient = listener.AcceptTcpClient();
  38. //创建消息服务线程对象
  39. //把multiSocket类的run方法委托给线程
  40. multiSocketThread = new MultiSocketThread(tcpClient);
  41. Thread newThread = new Thread(multiSocketThread.Run);
  42. newThread.IsBackground = true;
  43. newThread.Start();
  44. }
  45. }
  46. catch (Exception e)
  47. {
  48. if (multiSocketThread != null)
  49. multiSocketThread.Close();
  50. Console.WriteLine(e.Message);
  51. throw new Exception();
  52. }
  53. }
  54. private void Run()
  55. {
  56. bool linked = false;
  57. //web重连机制
  58. Task.Factory.StartNew(() =>
  59. {
  60. TcpListener listener = null;
  61. try
  62. {
  63. listener = LazySingleton.GetInstance(localAddr, port);
  64. listener.Start();
  65. linked = true;
  66. }
  67. catch (Exception) { Console.WriteLine("未能与Web服务器连接,本地ip错误或网络异常"); linked = false; }
  68. while (isClosing)
  69. {
  70. try
  71. {
  72. while (linked)
  73. {
  74. WebConnect(listener);
  75. }
  76. if (!linked)
  77. {
  78. throw new Exception();
  79. }
  80. }
  81. catch (Exception)
  82. {
  83. //Task.Factory.StartNew(() =>
  84. //{
  85. MyTimer mt = new MyTimer();
  86. mt.StartTiming();
  87. while (!linked)
  88. {
  89. Thread.Sleep(10000);
  90. try
  91. {
  92. listener = LazySingleton.GetInstance(localAddr, port);
  93. listener.Start();
  94. linked = true;
  95. Console.WriteLine("web已成功重连");
  96. break;
  97. }
  98. catch (Exception)
  99. {
  100. mt.EndTiming();
  101. int count = 0;
  102. if (mt.IsLonger(30, 1, true, out count))
  103. {
  104. Console.WriteLine("未能与Web服务器连接,本地ip错误或网络异常");
  105. }
  106. }
  107. }
  108. //});
  109. }
  110. Thread.Sleep(5000);
  111. }
  112. if (multiSocketThread != null)
  113. multiSocketThread.Close();
  114. });
  115. //处理接收到的指令
  116. Task.Factory.StartNew(() =>
  117. {
  118. while (!isClosing)
  119. {
  120. object obj = blockingQueue.Dequeue();
  121. if (obj.GetType().Equals(typeof(MessageUTF8)))
  122. {
  123. MessageUTF8 msg = (MessageUTF8)obj;
  124. switch (msg.cmd)
  125. {
  126. //预约停
  127. case 'a':
  128. lock (reserveLock)
  129. {
  130. reserveQueue.Enqueue(msg);
  131. }
  132. //通知PLC减少可预约车位数
  133. break;
  134. //预约取
  135. case 'b':
  136. break;
  137. //更新广告
  138. case 'c':
  139. string adAlert = "";
  140. bool result = Monitor.Monitor.advertMgr.UpdateAdvert(out adAlert);
  141. if (!result)
  142. {
  143. Monitor.Monitor.SetNotification("广告更新失败,请尝试手动更新");
  144. }
  145. else
  146. {
  147. Monitor.Monitor.SetNotification("广告更新成功\n" + adAlert);
  148. }
  149. break;
  150. default:
  151. Monitor.Monitor.SetNotification("接收到无法识别的指令");
  152. break;
  153. }
  154. }
  155. }
  156. });
  157. //处理超时预约指令
  158. Task.Factory.StartNew(() =>
  159. {
  160. while (!isClosing)
  161. {
  162. lock (reserveLock)
  163. {
  164. for (int i = 0; i < reserveQueue.Count; i++)
  165. {
  166. try
  167. {
  168. MessageUTF8 msg = reserveQueue.Dequeue();
  169. DateTime startTime = DateTime.Parse(msg.bookTime);
  170. if ((DateTime.Now - startTime).TotalMinutes > msg.bookLength * 60)
  171. {
  172. //通知PLC将可预约车位数恢复一个
  173. }
  174. else
  175. {
  176. reserveQueue.Enqueue(msg);
  177. }
  178. }
  179. catch { }
  180. }
  181. }
  182. Thread.Sleep(5000);
  183. }
  184. });
  185. }
  186. private void DownloadAds()
  187. {
  188. }
  189. /// <summary>
  190. /// 启动
  191. /// </summary>
  192. public bool Start(int port)
  193. {
  194. //string strTemp = ConfigurationManager.AppSettings["WebConfig"];
  195. //string[] strArray = strTemp.Split(':');
  196. //string ipstr = strArray[0];
  197. //string portstr = strArray[1];
  198. //获取本机ip地址
  199. try
  200. {
  201. TcpClient client = new TcpClient();
  202. client.Connect("www.baidu.com", 80);
  203. string ip = ((IPEndPoint)client.Client.LocalEndPoint).Address.ToString();
  204. client.Close();
  205. localAddr = IPAddress.Parse(ip);
  206. this.port = port;//Convert.ToInt32(ConfigurationManager.AppSettings["webPort"]);
  207. reserveQueue = new Queue<MessageUTF8>();
  208. Console.WriteLine(ip.ToString());
  209. }
  210. catch (Exception e) { Console.WriteLine("wrong ipAddr"); return false; }
  211. Run();
  212. return true;
  213. }
  214. /// <summary>
  215. /// 停止
  216. /// </summary>
  217. public void Stop()
  218. {
  219. isClosing = true;
  220. }
  221. /// <summary>
  222. /// 预约停车记录
  223. /// </summary>
  224. public void bookParkRecord()
  225. {
  226. }
  227. /// <summary>
  228. /// 预约取车记录
  229. /// </summary>
  230. public void bookFetchRecord()
  231. {
  232. }
  233. }
  234. }