UChannel.cs 1.2 KB

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