UChannel.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Common.Network;
  6. namespace UNet
  7. {
  8. internal class BufferInfo
  9. {
  10. public byte[] Buffer { get; set; }
  11. public byte ChannelID { get; set; }
  12. public PacketFlags Flags { get; set; }
  13. }
  14. internal class UChannel: AChannel
  15. {
  16. private USocket socket;
  17. private readonly string remoteAddress;
  18. private readonly Queue<BufferInfo> queue = new Queue<BufferInfo>();
  19. private bool isConnected;
  20. public UChannel(USocket socket, UService service): base(service)
  21. {
  22. this.isConnected = true;
  23. this.socket = socket;
  24. this.service = service;
  25. this.remoteAddress = this.socket.RemoteAddress;
  26. }
  27. public UChannel(USocket socket, string host, int port, UService service): base(service)
  28. {
  29. this.socket = socket;
  30. this.service = service;
  31. this.remoteAddress = host + ":" + port;
  32. }
  33. private void Dispose(bool disposing)
  34. {
  35. if (this.socket == null)
  36. {
  37. return;
  38. }
  39. this.onDispose(this);
  40. if (disposing)
  41. {
  42. this.socket.Dispose();
  43. }
  44. this.service.Remove(this);
  45. this.socket = null;
  46. }
  47. ~UChannel()
  48. {
  49. this.Dispose(false);
  50. }
  51. public override void Dispose()
  52. {
  53. this.Dispose(true);
  54. GC.SuppressFinalize(this);
  55. }
  56. public override async Task<bool> ConnectAsync()
  57. {
  58. string[] ss = this.RemoteAddress.Split(':');
  59. int port = int.Parse(ss[1]);
  60. bool result = await this.socket.ConnectAsync(ss[0], (ushort) port);
  61. this.isConnected = true;
  62. while (this.queue.Count > 0)
  63. {
  64. BufferInfo info = this.queue.Dequeue();
  65. this.SendAsync(info.Buffer, info.ChannelID, info.Flags);
  66. }
  67. return result;
  68. }
  69. public override void SendAsync(
  70. byte[] buffer, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
  71. {
  72. if (!this.isConnected)
  73. {
  74. BufferInfo info = new BufferInfo { Buffer = buffer, ChannelID = channelID, Flags = flags };
  75. this.queue.Enqueue(info);
  76. return;
  77. }
  78. this.socket.SendAsync(buffer, channelID, flags);
  79. }
  80. public override void SendAsync(
  81. List<byte[]> buffers, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
  82. {
  83. int size = buffers.Select(b => b.Length).Sum();
  84. byte[] buffer = new byte[size];
  85. int index = 0;
  86. foreach (byte[] bytes in buffers)
  87. {
  88. Array.Copy(bytes, 0, buffer, index, bytes.Length);
  89. index += bytes.Length;
  90. }
  91. if (!this.isConnected)
  92. {
  93. BufferInfo info = new BufferInfo { Buffer = buffer, ChannelID = channelID, Flags = flags };
  94. this.queue.Enqueue(info);
  95. return;
  96. }
  97. this.socket.SendAsync(buffer, channelID, flags);
  98. }
  99. public override async Task<byte[]> RecvAsync()
  100. {
  101. return await this.socket.RecvAsync();
  102. }
  103. public override string RemoteAddress
  104. {
  105. get
  106. {
  107. return this.remoteAddress;
  108. }
  109. }
  110. public override async Task<bool> DisconnnectAsync()
  111. {
  112. return await this.socket.DisconnectAsync();
  113. }
  114. }
  115. }