UChannel.cs 3.0 KB

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