UService.cs 2.5 KB

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