UChannel.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. public UChannel(USocket socket, UService service)
  11. {
  12. this.socket = socket;
  13. this.service = service;
  14. }
  15. protected void Dispose(bool disposing)
  16. {
  17. if (socket == null)
  18. {
  19. return;
  20. }
  21. if (disposing)
  22. {
  23. socket.Dispose();
  24. }
  25. service.Remove(this);
  26. this.socket = null;
  27. }
  28. ~UChannel()
  29. {
  30. Dispose(false);
  31. }
  32. public void Dispose()
  33. {
  34. Dispose(true);
  35. GC.SuppressFinalize(this);
  36. }
  37. public void SendAsync(byte[] buffer, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
  38. {
  39. this.socket.SendAsync(buffer, channelID, flags);
  40. }
  41. public async Task<byte[]> RecvAsync()
  42. {
  43. return await this.socket.RecvAsync();
  44. }
  45. public string RemoteAddress
  46. {
  47. get
  48. {
  49. return this.socket.RemoteAddress;
  50. }
  51. }
  52. public async Task<bool> DisconnnectAsync()
  53. {
  54. return await this.socket.DisconnectAsync();
  55. }
  56. }
  57. }