UChannel.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Threading.Tasks;
  3. using Common.Network;
  4. namespace UNet
  5. {
  6. internal class UChannel: AChannel
  7. {
  8. private readonly UService service;
  9. private USocket socket;
  10. private readonly string remoteAddress;
  11. public UChannel(USocket socket, UService service)
  12. {
  13. this.socket = socket;
  14. this.service = service;
  15. this.remoteAddress = this.socket.RemoteAddress;
  16. }
  17. private void Dispose(bool disposing)
  18. {
  19. if (this.socket == null)
  20. {
  21. return;
  22. }
  23. if (disposing)
  24. {
  25. this.socket.Dispose();
  26. }
  27. this.service.Remove(this);
  28. this.socket = null;
  29. }
  30. ~UChannel()
  31. {
  32. this.Dispose(false);
  33. }
  34. public override void Dispose()
  35. {
  36. this.Dispose(true);
  37. GC.SuppressFinalize(this);
  38. }
  39. public override void SendAsync(
  40. byte[] buffer, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
  41. {
  42. this.socket.SendAsync(buffer, channelID, flags);
  43. }
  44. public override async Task<byte[]> RecvAsync()
  45. {
  46. return await this.socket.RecvAsync();
  47. }
  48. public override string RemoteAddress
  49. {
  50. get
  51. {
  52. return this.remoteAddress;
  53. }
  54. }
  55. public override async Task<bool> DisconnnectAsync()
  56. {
  57. return await this.socket.DisconnectAsync();
  58. }
  59. }
  60. }