UService.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using System.Threading.Tasks;
  6. namespace Base
  7. {
  8. public sealed class UService: AService
  9. {
  10. private UPoller poller;
  11. private readonly Dictionary<long, UChannel> idChannels = new Dictionary<long, UChannel>();
  12. private readonly Dictionary<string, UChannel> addressChannels = new Dictionary<string, 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. public override void Dispose()
  30. {
  31. if (this.poller == null)
  32. {
  33. return;
  34. }
  35. foreach (long id in this.idChannels.Keys.ToArray())
  36. {
  37. UChannel channel = this.idChannels[id];
  38. channel.Dispose();
  39. }
  40. this.poller = null;
  41. }
  42. public override void Add(Action action)
  43. {
  44. this.poller.Add(action);
  45. }
  46. public override AChannel GetChannel(string host, int port)
  47. {
  48. return this.GetChannel($"{host}:{port}");
  49. }
  50. public override AChannel GetChannel(string address)
  51. {
  52. UChannel channel = null;
  53. if (this.addressChannels.TryGetValue(address, out channel))
  54. {
  55. return channel;
  56. }
  57. USocket newSocket = new USocket(this.poller);
  58. string[] ss = address.Split(':');
  59. int port = int.Parse(ss[1]);
  60. string host = ss[0];
  61. channel = new UChannel(newSocket, host, port, this);
  62. newSocket.Disconnect += () => this.OnChannelError(channel.Id, SocketError.SocketError);
  63. this.idChannels[channel.Id] = channel;
  64. return channel;
  65. }
  66. public override async Task<AChannel> GetChannel()
  67. {
  68. USocket socket = await this.poller.AcceptAsync();
  69. UChannel channel = new UChannel(socket, this);
  70. this.addressChannels[channel.RemoteAddress] = channel;
  71. this.idChannels[channel.Id] = channel;
  72. return channel;
  73. }
  74. public override AChannel GetChannel(long id)
  75. {
  76. UChannel channel = null;
  77. this.idChannels.TryGetValue(id, out channel);
  78. return channel;
  79. }
  80. public override void Remove(long id)
  81. {
  82. UChannel channel;
  83. if (!this.idChannels.TryGetValue(id, out channel))
  84. {
  85. return;
  86. }
  87. if (channel == null)
  88. {
  89. return;
  90. }
  91. this.idChannels.Remove(id);
  92. channel.Dispose();
  93. }
  94. public override void Update()
  95. {
  96. this.poller.Update();
  97. }
  98. }
  99. }