UService.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /// <summary>
  15. /// 即可做client也可做server
  16. /// </summary>
  17. /// <param name="host"></param>
  18. /// <param name="port"></param>
  19. public UService(string host, int port)
  20. {
  21. this.poller = new UPoller(host, (ushort) port);
  22. }
  23. /// <summary>
  24. /// 只能做client
  25. /// </summary>
  26. public UService()
  27. {
  28. this.poller = new UPoller();
  29. }
  30. private void Dispose(bool disposing)
  31. {
  32. if (this.poller == null)
  33. {
  34. return;
  35. }
  36. if (disposing)
  37. {
  38. foreach (ObjectId id in this.idChannels.Keys.ToArray())
  39. {
  40. UChannel channel = this.idChannels[id];
  41. channel.Dispose();
  42. }
  43. this.poller.Dispose();
  44. }
  45. this.poller = null;
  46. }
  47. ~UService()
  48. {
  49. this.Dispose(false);
  50. }
  51. public void Dispose()
  52. {
  53. this.Dispose(true);
  54. GC.SuppressFinalize(this);
  55. }
  56. public void Add(Action action)
  57. {
  58. this.poller.Add(action);
  59. }
  60. private async Task<AChannel> ConnectAsync(string host, int port)
  61. {
  62. USocket newSocket = await this.poller.ConnectAsync(host, (ushort) port);
  63. UChannel channel = new UChannel(newSocket, this);
  64. this.channels[channel.RemoteAddress] = channel;
  65. this.idChannels[channel.Id] = channel;
  66. return channel;
  67. }
  68. public async Task<AChannel> GetChannel(string host, int port)
  69. {
  70. UChannel channel = null;
  71. if (this.channels.TryGetValue(host + ":" + port, out channel))
  72. {
  73. return channel;
  74. }
  75. return await this.ConnectAsync(host, port);
  76. }
  77. public async Task<AChannel> GetChannel()
  78. {
  79. USocket socket = await this.poller.AcceptAsync();
  80. UChannel channel = new UChannel(socket, this);
  81. this.channels[channel.RemoteAddress] = channel;
  82. this.idChannels[channel.Id] = channel;
  83. return channel;
  84. }
  85. public AChannel GetChannel(ObjectId id)
  86. {
  87. UChannel channel = null;
  88. this.idChannels.TryGetValue(id, out channel);
  89. return channel;
  90. }
  91. public void Remove(AChannel channel)
  92. {
  93. UChannel tChannel = channel as UChannel;
  94. if (tChannel == null)
  95. {
  96. return;
  97. }
  98. this.idChannels.Remove(channel.Id);
  99. this.channels.Remove(channel.RemoteAddress);
  100. }
  101. public void Update()
  102. {
  103. this.poller.Update();
  104. }
  105. }
  106. }