UDPForKCP.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Buffers;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Net.Sockets.Kcp;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Fort23.UTool;
  9. public class UDPForKCP : IKcpCallback, IDisposable
  10. {
  11. private UdpClient client;
  12. private Thread _udpClientThread;
  13. public int myPort;
  14. public int ycPort;
  15. public bool isLockSend;
  16. public object LockObject;
  17. public bool isConnect;
  18. // public int PiPeiPort;
  19. public UDPForKCP()
  20. {
  21. }
  22. public UDPForKCP(int port, uint conv) : this(port, null, conv)
  23. {
  24. }
  25. public UDPForKCP(int port, IPEndPoint endPoint, uint conv)
  26. {
  27. myPort = port;
  28. // LogTool.Log("链接端口"+port+"__"+endPoint.Address);
  29. client = new UdpClient(port);
  30. if (endPoint != null)
  31. {
  32. client.Connect(endPoint);
  33. isConnect = true;
  34. }
  35. _kcp = new SimpleSegManager.Kcp(conv, this);
  36. kcp.NoDelay(1, 40, 2, 0);
  37. this.EndPoint = endPoint;
  38. _udpClientThread = new Thread(BeginRecv);
  39. _udpClientThread.Start();
  40. }
  41. public SimpleSegManager.Kcp kcp
  42. {
  43. get { return _kcp; }
  44. }
  45. public IPEndPoint EndPoint { get; set; }
  46. private SimpleSegManager.Kcp _kcp;
  47. public void Output(IMemoryOwner<byte> buffer, int avalidLength)
  48. {
  49. if (EndPoint == null)
  50. {
  51. return;
  52. }
  53. var s = buffer.Memory.Span.Slice(0, avalidLength).ToArray();
  54. if (isLockSend)
  55. {
  56. lock (LockObject)
  57. {
  58. client.Send(s, s.Length, EndPoint);
  59. // Debug.Log("发送"+client.Client.RemoteEndPoint);
  60. buffer.Dispose();
  61. }
  62. }
  63. else
  64. {
  65. // Debug.Log("发送"+client.Client.RemoteEndPoint!=null);
  66. if (isConnect)
  67. {
  68. client.Send(s, s.Length);
  69. }
  70. else
  71. {
  72. client.Send(s, s.Length, EndPoint);
  73. }
  74. buffer.Dispose();
  75. }
  76. }
  77. public async void SendAsync(byte[] datagram, int bytes)
  78. {
  79. int c = kcp.Send(datagram.AsSpan().Slice(0, bytes));
  80. // LogTool.Error("发送错误" + c);
  81. }
  82. public byte[] ReceiveAsync()
  83. {
  84. var (buffer, avalidLength) = kcp.TryRecv();
  85. while (buffer == null)
  86. {
  87. Thread.Sleep(5);
  88. if (_udpClientThread == null)
  89. {
  90. return null;
  91. }
  92. (buffer, avalidLength) = kcp.TryRecv();
  93. }
  94. var s = buffer.Memory.Span.Slice(0, avalidLength).ToArray();
  95. return s;
  96. }
  97. private async void BeginRecv()
  98. {
  99. while (_udpClientThread != null)
  100. {
  101. try
  102. {
  103. var res = await client.ReceiveAsync();
  104. int ok = kcp.Input(res.Buffer);
  105. if (ok >= 0)
  106. {
  107. EndPoint = res.RemoteEndPoint;
  108. ycPort = EndPoint.Port;
  109. }
  110. else
  111. {
  112. LogTool.Error("kcp.Receive 返回错误" + ok+"___"+res.RemoteEndPoint);
  113. }
  114. }
  115. catch (Exception e)
  116. {
  117. SocketException socketException = e as SocketException;
  118. if (socketException.ErrorCode == 10054)
  119. {
  120. }
  121. LogTool.Error(e);
  122. }
  123. Thread.Sleep(1);
  124. }
  125. }
  126. public void Dispose()
  127. {
  128. _udpClientThread = null;
  129. client?.Dispose();
  130. kcp?.Dispose();
  131. client = null;
  132. _kcp = null;
  133. }
  134. }