TService.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Common.Base;
  5. using Network;
  6. namespace TNet
  7. {
  8. public class TService: IService
  9. {
  10. private readonly IPoller poller = new TPoller();
  11. private TSocket acceptor;
  12. private readonly Dictionary<string, TChannel> channels = new Dictionary<string, TChannel>();
  13. private readonly TimerManager timerManager = new TimerManager();
  14. /// <summary>
  15. /// 即可做client也可做server
  16. /// </summary>
  17. /// <param name="host"></param>
  18. /// <param name="port"></param>
  19. public TService(string host, int port)
  20. {
  21. this.acceptor = new TSocket(poller);
  22. this.acceptor.Bind(host, port);
  23. this.acceptor.Listen(100);
  24. }
  25. /// <summary>
  26. /// 只能做client端的构造函数
  27. /// </summary>
  28. public TService()
  29. {
  30. }
  31. protected virtual void Dispose(bool disposing)
  32. {
  33. if (this.acceptor == null)
  34. {
  35. return;
  36. }
  37. if (disposing)
  38. {
  39. this.acceptor.Dispose();
  40. }
  41. this.acceptor = null;
  42. }
  43. ~TService()
  44. {
  45. this.Dispose(false);
  46. }
  47. public void Dispose()
  48. {
  49. Dispose(true);
  50. GC.SuppressFinalize(this);
  51. }
  52. public void Add(Action action)
  53. {
  54. this.poller.Add(action);
  55. }
  56. private async Task<IChannel> ConnectAsync(string host, int port)
  57. {
  58. TSocket newSocket = new TSocket(poller);
  59. await newSocket.ConnectAsync(host, port);
  60. TChannel channel = new TChannel(newSocket, this);
  61. channels[newSocket.RemoteAddress] = channel;
  62. channel.Start();
  63. return channel;
  64. }
  65. public async Task<IChannel> GetChannel()
  66. {
  67. if (this.acceptor == null)
  68. {
  69. throw new Exception(string.Format("service construct must use host and port param"));
  70. }
  71. TSocket socket = new TSocket(this.poller);
  72. await acceptor.AcceptAsync(socket);
  73. TChannel channel = new TChannel(socket, this);
  74. channels[channel.RemoteAddress] = channel;
  75. channel.Start();
  76. return channel;
  77. }
  78. public void Remove(IChannel channel)
  79. {
  80. TChannel tChannel = channel as TChannel;
  81. if (tChannel == null)
  82. {
  83. return;
  84. }
  85. this.channels.Remove(channel.RemoteAddress);
  86. this.timerManager.Remove(tChannel.SendTimer);
  87. }
  88. public async Task<IChannel> GetChannel(string host, int port)
  89. {
  90. TChannel channel = null;
  91. if (this.channels.TryGetValue(host + ":" + port, out channel))
  92. {
  93. return channel;
  94. }
  95. return await ConnectAsync(host, port);
  96. }
  97. public async Task<IChannel> GetChannel(string address)
  98. {
  99. string[] ss = address.Split(':');
  100. int port = Convert.ToInt32(ss[1]);
  101. return await GetChannel(ss[0], port);
  102. }
  103. public void RunOnce(int timeout)
  104. {
  105. poller.Run(timeout);
  106. }
  107. public void Run()
  108. {
  109. while (true)
  110. {
  111. this.RunOnce(1);
  112. this.timerManager.Refresh();
  113. }
  114. }
  115. internal TimerManager Timer
  116. {
  117. get
  118. {
  119. return this.timerManager;
  120. }
  121. }
  122. }
  123. }