UService.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Network;
  5. namespace UNet
  6. {
  7. public sealed class UService: IService
  8. {
  9. private UPoller poller;
  10. private readonly Dictionary<string, UChannel> channels = new Dictionary<string, UChannel>();
  11. /// <summary>
  12. /// 即可做client也可做server
  13. /// </summary>
  14. /// <param name="host"></param>
  15. /// <param name="port"></param>
  16. public UService(string host, int port)
  17. {
  18. this.poller = new UPoller(host, (ushort)port);
  19. }
  20. /// <summary>
  21. /// 只能做client
  22. /// </summary>
  23. public UService()
  24. {
  25. this.poller = new UPoller();
  26. }
  27. private void Dispose(bool disposing)
  28. {
  29. if (this.poller == null)
  30. {
  31. return;
  32. }
  33. if (disposing)
  34. {
  35. this.poller.Dispose();
  36. }
  37. this.poller = null;
  38. }
  39. ~UService()
  40. {
  41. Dispose(false);
  42. }
  43. public void Dispose()
  44. {
  45. Dispose(true);
  46. GC.SuppressFinalize(this);
  47. }
  48. public void Add(Action action)
  49. {
  50. this.poller.Events += action;
  51. }
  52. public UPoller Poller
  53. {
  54. get
  55. {
  56. return this.poller;
  57. }
  58. }
  59. private async Task<IChannel> ConnectAsync(string host, int port, uint channelCount)
  60. {
  61. USocket newSocket = new USocket(this.poller);
  62. await newSocket.ConnectAsync(host, (ushort)port, channelCount);
  63. UChannel channel = new UChannel(newSocket, this);
  64. channels[channel.RemoteAddress] = channel;
  65. return channel;
  66. }
  67. public async Task<IChannel> GetChannel(string address, uint channelCount)
  68. {
  69. string[] ss = address.Split(':');
  70. int port = Convert.ToInt32(ss[1]);
  71. return await GetChannel(ss[0], port, channelCount);
  72. }
  73. public async Task<IChannel> GetChannel(string host, int port, uint channelCount)
  74. {
  75. UChannel channel = null;
  76. if (this.channels.TryGetValue(host + ":" + port, out channel))
  77. {
  78. return channel;
  79. }
  80. return await ConnectAsync(host, port, channelCount);
  81. }
  82. public async Task<IChannel> GetChannel()
  83. {
  84. USocket socket = new USocket(this.poller);
  85. await socket.AcceptAsync();
  86. UChannel channel = new UChannel(socket, this);
  87. channels[channel.RemoteAddress] = channel;
  88. return channel;
  89. }
  90. public void Remove(IChannel channel)
  91. {
  92. UChannel tChannel = channel as UChannel;
  93. if (tChannel == null)
  94. {
  95. return;
  96. }
  97. this.channels.Remove(channel.RemoteAddress);
  98. }
  99. public void RunOnce(int timeout)
  100. {
  101. this.poller.RunOnce(timeout);
  102. }
  103. public void Run()
  104. {
  105. this.poller.Run();
  106. }
  107. }
  108. }