TService.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. namespace Base
  6. {
  7. public sealed class TService: AService
  8. {
  9. private TPoller poller = new TPoller();
  10. private readonly TSocket acceptor;
  11. private readonly Dictionary<long, TChannel> idChannels = new Dictionary<long, TChannel>();
  12. private readonly Dictionary<string, TChannel> addressChannels = new Dictionary<string, TChannel>();
  13. /// <summary>
  14. /// 即可做client也可做server
  15. /// </summary>
  16. /// <param name="host"></param>
  17. /// <param name="port"></param>
  18. public TService(string host, int port)
  19. {
  20. this.acceptor = new TSocket(this.poller, host, port);
  21. }
  22. public TService()
  23. {
  24. }
  25. public override void Dispose()
  26. {
  27. if (this.poller == null)
  28. {
  29. return;
  30. }
  31. foreach (long id in this.idChannels.Keys.ToArray())
  32. {
  33. TChannel channel = this.idChannels[id];
  34. channel.Dispose();
  35. }
  36. this.poller = null;
  37. }
  38. public override void Add(Action action)
  39. {
  40. this.poller.Add(action);
  41. }
  42. public override AChannel GetChannel(long id)
  43. {
  44. TChannel channel = null;
  45. this.idChannels.TryGetValue(id, out channel);
  46. return channel;
  47. }
  48. public override async Task<AChannel> GetChannel()
  49. {
  50. if (this.acceptor == null)
  51. {
  52. throw new Exception("service construct must use host and port param");
  53. }
  54. TSocket socket = new TSocket(this.poller);
  55. await this.acceptor.AcceptAsync(socket);
  56. TChannel channel = new TChannel(socket, this);
  57. this.addressChannels[channel.RemoteAddress] = channel;
  58. this.idChannels[channel.Id] = channel;
  59. return channel;
  60. }
  61. public override void Remove(long id)
  62. {
  63. TChannel channel;
  64. if (!this.idChannels.TryGetValue(id, out channel))
  65. {
  66. return;
  67. }
  68. if (channel == null)
  69. {
  70. return;
  71. }
  72. this.idChannels.Remove(id);
  73. channel.Dispose();
  74. }
  75. public override AChannel GetChannel(string host, int port)
  76. {
  77. string address = $"{host}:{port}";
  78. return this.GetChannel(address);
  79. }
  80. public override AChannel GetChannel(string address)
  81. {
  82. TChannel channel = null;
  83. if (this.addressChannels.TryGetValue(address, out channel))
  84. {
  85. return channel;
  86. }
  87. string[] ss = address.Split(':');
  88. string host = ss[0];
  89. int port = int.Parse(ss[1]);
  90. TSocket newSocket = new TSocket(this.poller);
  91. channel = new TChannel(newSocket, host, port, this);
  92. channel.OnError += this.OnChannelError;
  93. this.idChannels[channel.Id] = channel;
  94. return channel;
  95. }
  96. public override void Update()
  97. {
  98. this.poller.Update();
  99. }
  100. }
  101. }