UChannel.cs 935 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Threading.Tasks;
  2. using Network;
  3. namespace UNet
  4. {
  5. internal class UChannel: IChannel
  6. {
  7. private readonly UService service;
  8. private USocket socket;
  9. public UChannel(USocket socket, UService service)
  10. {
  11. this.socket = socket;
  12. this.service = service;
  13. }
  14. public void Dispose()
  15. {
  16. if (socket == null)
  17. {
  18. return;
  19. }
  20. service.Remove(this);
  21. socket.Dispose();
  22. this.socket = null;
  23. }
  24. public void SendAsync(byte[] buffer, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
  25. {
  26. this.socket.WriteAsync(buffer, channelID, flags);
  27. }
  28. public async Task<byte[]> RecvAsync()
  29. {
  30. return await this.socket.ReadAsync();
  31. }
  32. public string RemoteAddress
  33. {
  34. get
  35. {
  36. return this.socket.RemoteAddress;
  37. }
  38. }
  39. public async Task<bool> DisconnnectAsync()
  40. {
  41. return await this.socket.DisconnectAsync();
  42. }
  43. }
  44. }