DeliveryServer.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.IO.Pipes;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using Sers.Core.CL.MessageDelivery;
  12. using Vit.Core.Module.Log;
  13. using Vit.Core.Util.Common;
  14. using Vit.Core.Util.Net;
  15. using Vit.Core.Util.Threading;
  16. namespace Sers.CL.Ipc.NamedPipe
  17. {
  18. public class DeliveryServer: IDeliveryServer
  19. {
  20. public string pipeName = "demo";
  21. public Action<IDeliveryConnection> Conn_OnDisconnected { private get; set; }
  22. public Action<IDeliveryConnection> Conn_OnConnected { private get; set; }
  23. /// <summary>
  24. /// connHashCode -> DeliveryConnection
  25. /// </summary>
  26. readonly ConcurrentDictionary<int, DeliveryConnection> connMap = new ConcurrentDictionary<int, DeliveryConnection>();
  27. public IEnumerable<IDeliveryConnection> ConnectedList => connMap.Values.Select(conn => ((IDeliveryConnection)conn));
  28. LongTaskHelp tcpListenerAccept_BackThread = new LongTaskHelp();
  29. #region Start
  30. string BeforeConnect()
  31. {
  32. string connKey = CommonHelp.NewGuid();
  33. Task.Run(() => {
  34. var server = new NamedPipeServerStream(pipeName + "." + connKey, PipeDirection.InOut);
  35. //TODO 10秒无连接 强制关闭
  36. // 等待客户端的连接
  37. server.WaitForConnection();
  38. if(server.IsConnected)
  39. Delivery_OnConnected(server);
  40. //server.Close();
  41. });
  42. return connKey;
  43. }
  44. /// <summary>
  45. /// 启动服务
  46. /// </summary>
  47. public bool Start()
  48. {
  49. try
  50. {
  51. Logger.Info("[CL.DeliveryServer] Socket.ThreadWait,starting... pipeName:" + pipeName);
  52. #region (x.2)启动Task监听listener
  53. tcpListenerAccept_BackThread.action = () =>
  54. {
  55. try
  56. {
  57. while (true)
  58. {
  59. ConnectionKeyHelp.Publish(BeforeConnect, pipeName);
  60. }
  61. }
  62. catch (Exception ex) when (!(ex.GetBaseException() is ThreadInterruptedException))
  63. {
  64. Logger.Error(ex);
  65. }
  66. finally
  67. {
  68. Stop();
  69. }
  70. };
  71. tcpListenerAccept_BackThread.Start();
  72. #endregion
  73. Logger.Info("[CL.DeliveryServer] Socket.ThreadWait,started.");
  74. return true;
  75. }
  76. catch (Exception ex)
  77. {
  78. Logger.Error(ex);
  79. }
  80. return false;
  81. }
  82. #endregion
  83. #region Stop
  84. /// <summary>
  85. /// 停止服务
  86. /// </summary>
  87. public void Stop()
  88. {
  89. //(x.1) stop conn
  90. ConnectedList.ToList().ForEach(Delivery_OnDisconnected);
  91. connMap.Clear();
  92. //(x.2) close socket
  93. Task.Run(() =>
  94. {
  95. Logger.Info("[CL.DeliveryServer] Socket.ThreadWait,stop...");
  96. tcpListenerAccept_BackThread.Stop();
  97. Logger.Info("[CL.DeliveryServer] Socket.ThreadWait,stoped");
  98. });
  99. }
  100. #endregion
  101. #region Delivery_Event
  102. private DeliveryConnection Delivery_OnConnected(Stream client)
  103. {
  104. var conn = new DeliveryConnection();
  105. conn.Init(client);
  106. conn.Conn_OnDisconnected = Delivery_OnDisconnected;
  107. connMap[conn.GetHashCode()] = conn;
  108. try
  109. {
  110. Conn_OnConnected?.Invoke(conn);
  111. }
  112. catch (Exception ex)
  113. {
  114. Logger.Error(ex);
  115. }
  116. conn.StartBackThreadToReceiveMsg();
  117. return conn;
  118. }
  119. private void Delivery_OnDisconnected(IDeliveryConnection _conn)
  120. {
  121. var conn = (DeliveryConnection)_conn;
  122. connMap.TryRemove(conn.GetHashCode(), out _);
  123. try
  124. {
  125. Conn_OnDisconnected?.Invoke(conn);
  126. }
  127. catch (Exception ex)
  128. {
  129. Logger.Error(ex);
  130. }
  131. }
  132. #endregion
  133. }
  134. }