CommunicationTcp.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using ProjectManagementSystem.Device.Extenions;
  2. using RRQMCore;
  3. using RRQMCore.ByteManager;
  4. using RRQMSocket;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace ProjectManagementSystem.Device.Core
  13. {
  14. public class CommunicationTcp : CommunicationBase, ICommunication
  15. {
  16. public bool IsStart { get { return tcpService?.ServerState == ServerState.Running; } }
  17. protected string ipHost;
  18. protected TcpService tcpService;
  19. public CommunicationTcp(string ipAddress, int port)
  20. {
  21. ipHost = $"{ipAddress}:{port}";
  22. }
  23. public void Start()
  24. {
  25. tcpService = new TcpService();
  26. tcpService.Logger = new LogerBox(this.LogMsg);
  27. //订阅事件
  28. tcpService.Connecting += (client, e) =>
  29. {
  30. e.DataHandlingAdapter = new NormalDataHandlingAdapter();
  31. };
  32. tcpService.Connected += (client, e) =>
  33. {
  34. LogMsg($"客户端{client.IP}:{client.Port}已连接,总连接数:{tcpService.SocketClients.Count}");
  35. };
  36. tcpService.Disconnected += (client, e) =>
  37. {
  38. LogMsg($"客户端{client.IP}:{client.Port}已断开,总连接数:{tcpService.SocketClients.Count}");
  39. };
  40. tcpService.Received += OnReceived;
  41. ////声明配置
  42. var config = new RRQMConfig();
  43. config.SetListenIPHosts(new IPHost[] { new IPHost(ipHost) });
  44. config.SetClearInterval(-1);
  45. //载入配置
  46. tcpService.Setup(config);
  47. try
  48. {
  49. //启动
  50. tcpService.Start();
  51. LogMsg($"TCP服务端{ipHost}已启动");
  52. var address = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault(d => ipHost.Contains(d.ToString()));
  53. if (address == null)
  54. {
  55. LogMsg($"注意:TCP监听IP不属于本机IP");
  56. }
  57. }
  58. catch (Exception ex)
  59. {
  60. LogMsg($"TCP服务端{ipHost}启动异常", ex);
  61. Stop();
  62. }
  63. }
  64. public void Stop()
  65. {
  66. if (this.tcpService != null)
  67. {
  68. this.tcpService.Stop();
  69. }
  70. }
  71. public IEnumerable<string> GetClientIDs()
  72. {
  73. return tcpService.SocketClients.GetIDs();
  74. }
  75. public void SendCommand(byte[] data, string address)
  76. {
  77. var clients = tcpService.GetClients();
  78. for (int i = 0; i < clients.Length; i++)
  79. {
  80. var client = clients[i];
  81. if (client.Online)
  82. {
  83. string clientName = client.MainSocket.RemoteEndPoint.ToString();
  84. if (address == "127.0.0.1"
  85. || string.IsNullOrEmpty(address)
  86. || clientName.StartsWith(address))
  87. {
  88. try
  89. {
  90. client.Send(data);
  91. LogMsg($"{clientName}: Send: {data.Byte2HexString()}");
  92. }
  93. catch (Exception ex)
  94. {
  95. LogMsg($"{clientName}: SendErr: {data.Byte2HexString()}", ex);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. private void OnReceived(SocketClient client, ByteBlock byteBlock, IRequestInfo obj)
  102. {
  103. byte[] data = new byte[byteBlock.Length];
  104. Buffer.BlockCopy(byteBlock.Buffer, 0, data, 0, data.Length);
  105. string clientName = client.MainSocket.RemoteEndPoint.ToString();
  106. LogMsg($"{clientName}: Recv: {data.Byte2HexString()}");
  107. DataReceived?.Invoke(clientName, data);
  108. }
  109. }
  110. }