UService.cs 2.8 KB

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