Communicator.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using NNanomsg;
  2. using NNanomsg.Protocols;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Google.Protobuf;
  10. using Message;
  11. namespace parkspace_manager
  12. {
  13. class Communicator
  14. {
  15. private readonly static object lockObj = new object();
  16. /// <summary>
  17. /// 单例
  18. /// </summary>
  19. private static Communicator instance = null;
  20. /// <summary>
  21. /// 消息超时时间
  22. /// </summary>
  23. private static int timeout_milli = 2000;
  24. /// <summary>
  25. /// 接收解析锁
  26. /// </summary>
  27. private object m_receive_lock;
  28. /// <summary>
  29. /// 发送锁
  30. /// </summary>
  31. private object m_send_lock;
  32. /// <summary>
  33. /// 车位状态访问锁
  34. /// </summary>
  35. private object m_parkspace_status_access_lock;
  36. /// <summary>
  37. /// 车位消息队列
  38. /// </summary>
  39. private Parkspace_allocation_status_msg m_parkspace_current_status;
  40. /// <summary>
  41. /// 当前时间
  42. /// </summary>
  43. private DateTime m_last_parkspace_status_time;
  44. /// <summary>
  45. /// 车位状态超时
  46. /// </summary>
  47. private bool mb_parkspace_status_timeout;
  48. /// <summary>
  49. /// 发送队列
  50. /// </summary>
  51. private Queue<ByteString> m_send_queue;
  52. /// <summary>
  53. /// 接收队列
  54. /// </summary>
  55. private Queue<ByteString> m_receive_queue;
  56. /// <summary>
  57. /// 发送线程
  58. /// </summary>
  59. private Thread m_thread_send;
  60. /// <summary>
  61. /// 接收线程
  62. /// </summary>
  63. private Thread m_thread_receive;
  64. /// <summary>
  65. /// 解析接收string到protobuf消息
  66. /// </summary>
  67. private Thread m_thread_decode_receive;
  68. /// <summary>
  69. /// nanomsg 通信句柄
  70. /// </summary>
  71. private BusSocket m_socket;
  72. /// <summary>
  73. /// 实例退出标记
  74. /// </summary>
  75. private bool mb_exit;
  76. /// <summary>
  77. /// 初始化标记
  78. /// </summary>
  79. private bool mb_initialized;
  80. /// <summary>
  81. /// 构造函数
  82. /// </summary>
  83. /// <param name="server_ip"></param>
  84. /// <param name="server_port"></param>
  85. private Communicator()
  86. {
  87. mb_exit = false;
  88. mb_initialized = false;
  89. mb_parkspace_status_timeout = false;
  90. m_receive_lock = new object();
  91. m_send_lock = new object();
  92. m_parkspace_status_access_lock = new object();
  93. m_receive_queue = new Queue<ByteString>();
  94. m_send_queue = new Queue<ByteString>();
  95. m_parkspace_current_status = new Parkspace_allocation_status_msg();
  96. }
  97. /// <summary>
  98. /// 析构函数
  99. /// </summary>
  100. ~Communicator()
  101. {
  102. mb_exit = true;
  103. if (m_thread_send != null)
  104. {
  105. m_thread_send.Abort();
  106. }
  107. if (m_thread_receive != null)
  108. {
  109. m_thread_receive.Abort();
  110. }
  111. if (m_thread_decode_receive != null)
  112. m_thread_decode_receive.Abort();
  113. }
  114. /// <summary>
  115. /// 单例访问
  116. /// </summary>
  117. public static Communicator Instance
  118. {
  119. get
  120. {
  121. if (instance == null)
  122. {
  123. lock (lockObj)
  124. {
  125. if (instance == null)
  126. {
  127. instance = new Communicator();
  128. }
  129. }
  130. }
  131. return instance;
  132. }
  133. }
  134. /// <summary>
  135. /// 初始化
  136. /// </summary>
  137. /// <returns></returns>
  138. public bool Init()
  139. {
  140. mb_initialized = true;
  141. m_socket = new BusSocket();
  142. m_thread_receive = new Thread(new ParameterizedThreadStart(Receive_thread_function));
  143. m_thread_send = new Thread(new ParameterizedThreadStart(Send_thread_function));
  144. m_thread_decode_receive = new Thread(new ParameterizedThreadStart(Decode_thread_function));
  145. m_thread_receive.Start(this);
  146. m_thread_send.Start(this);
  147. m_thread_decode_receive.Start(this);
  148. return true;
  149. }
  150. /// <summary>
  151. /// 反初始化
  152. /// </summary>
  153. /// <returns></returns>
  154. public bool Uninit()
  155. {
  156. mb_exit = true;
  157. if(m_thread_receive!=null)
  158. m_thread_receive.Join();
  159. if(m_thread_send!=null)
  160. m_thread_send.Join();
  161. if (m_thread_decode_receive != null)
  162. m_thread_decode_receive.Join();
  163. return true;
  164. }
  165. /// <summary>
  166. /// 连接
  167. /// </summary>
  168. /// <param name="server_address"></param>
  169. /// <returns></returns>
  170. public bool Connect(string server_address)
  171. {
  172. if (m_socket == null)
  173. return false;
  174. NanomsgEndpoint end_point = m_socket.Connect(server_address);
  175. return true;
  176. }
  177. /// <summary>
  178. /// 绑定本地端口监听
  179. /// </summary>
  180. /// <param name="self_address"></param>
  181. /// <returns></returns>
  182. public bool Bind(string self_address)
  183. {
  184. if (m_socket == null)
  185. return false;
  186. NanomsgEndpoint end_point = m_socket.Bind(self_address);
  187. return true;
  188. }
  189. /// <summary>
  190. /// 获取当前车位状态
  191. /// </summary>
  192. public bool Get_parkspace_status(ref Parkspace_allocation_status_msg msg)
  193. {
  194. lock (m_parkspace_status_access_lock)
  195. {
  196. msg.MergeFrom(m_parkspace_current_status);
  197. }
  198. if((DateTime.Now-m_last_parkspace_status_time).Milliseconds > timeout_milli)
  199. {
  200. return false;
  201. }
  202. else
  203. {
  204. return true;
  205. }
  206. }
  207. /// <summary>
  208. /// 发送手动更新车位消息
  209. /// </summary>
  210. /// <returns></returns>
  211. public bool Send_msg()
  212. {
  213. }
  214. /// <summary>
  215. /// 接收线程函数
  216. /// </summary>
  217. /// <param name="handle"></param>
  218. private static void Receive_thread_function(object handle)
  219. {
  220. if (handle == null)
  221. return;
  222. Communicator comm = (Communicator)handle;
  223. while(!comm.mb_exit)
  224. {
  225. if (!comm.mb_initialized || comm.m_socket==null)
  226. continue;
  227. byte[] data = comm.m_socket.ReceiveImmediate();
  228. if(data!=null && data.Length>0)
  229. {
  230. lock (comm.m_receive_lock)
  231. {
  232. //ByteString tmp = ByteString.CopyFrom(data);
  233. //Base_msg base_msg = Base_msg.Parser.ParseFrom(tmp);
  234. //Console.WriteLine(base_msg.ToString());
  235. comm.m_receive_queue.Enqueue(ByteString.CopyFrom(data));
  236. }
  237. }
  238. Thread.Sleep(50);
  239. }
  240. Console.WriteLine("receive thread exit");
  241. }
  242. /// <summary>
  243. /// 发送线程函数
  244. /// </summary>
  245. /// <param name="handle"></param>
  246. private static void Send_thread_function(object handle)
  247. {
  248. if (handle == null)
  249. return;
  250. Communicator comm = (Communicator)handle;
  251. while (!comm.mb_exit)
  252. {
  253. if (!comm.mb_initialized || comm.m_socket == null)
  254. continue;
  255. lock(comm.m_send_lock)
  256. {
  257. }
  258. Thread.Sleep(500);
  259. }
  260. Console.WriteLine("send thread exit");
  261. }
  262. /// <summary>
  263. /// 解析线程函数
  264. /// </summary>
  265. /// <param name="handle"></param>
  266. private static void Decode_thread_function(object handle)
  267. {
  268. if (handle == null)
  269. return;
  270. Communicator comm = (Communicator)handle;
  271. while (!comm.mb_exit)
  272. {
  273. DateTime current_time = DateTime.Now;
  274. if (!comm.mb_initialized)
  275. continue;
  276. Parkspace_allocation_status_msg parkspace_status = null;
  277. lock (comm.m_receive_lock)
  278. {
  279. if (comm.m_receive_queue.Count > 0)
  280. {
  281. ByteString msg_str = comm.m_receive_queue.Dequeue();
  282. parkspace_status = comm.Decode_msg(msg_str);
  283. if (parkspace_status != null)
  284. {
  285. comm.m_last_parkspace_status_time = DateTime.Now;
  286. Console.WriteLine(parkspace_status.ToString());
  287. }
  288. }
  289. }
  290. if(parkspace_status != null)
  291. {
  292. lock (comm.m_parkspace_status_access_lock)
  293. {
  294. comm.m_parkspace_current_status = parkspace_status;
  295. //comm.m_parkspace_current_status.MergeFrom(parkspace_status);
  296. }
  297. }
  298. Thread.Sleep(50);
  299. }
  300. Console.WriteLine("decode thread exit");
  301. }
  302. /// <summary>
  303. /// 解析string到protobuf消息
  304. /// </summary>
  305. private Parkspace_allocation_status_msg Decode_msg(ByteString msg)
  306. {
  307. Base_msg base_msg = Base_msg.Parser.ParseFrom(msg);
  308. if(base_msg.BaseInfo.MsgType == Message_type.EParkspaceAllocationStatusMsg)
  309. {
  310. Parkspace_allocation_status_msg parsed_msg = Parkspace_allocation_status_msg.Parser.ParseFrom(msg);
  311. return parsed_msg;
  312. }
  313. else
  314. {
  315. return null;
  316. }
  317. }
  318. }
  319. }