TService.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. return channel;
  63. }
  64. public async Task<IChannel> GetChannel()
  65. {
  66. if (this.acceptor == null)
  67. {
  68. throw new Exception(string.Format("service construct must use host and port param"));
  69. }
  70. TSocket socket = new TSocket(this.poller);
  71. await acceptor.AcceptAsync(socket);
  72. TChannel channel = new TChannel(socket, this);
  73. channels[channel.RemoteAddress] = channel;
  74. return channel;
  75. }
  76. public void Remove(IChannel channel)
  77. {
  78. TChannel tChannel = channel as TChannel;
  79. if (tChannel == null)
  80. {
  81. return;
  82. }
  83. this.channels.Remove(channel.RemoteAddress);
  84. this.timerManager.Remove(tChannel.SendTimer);
  85. }
  86. public async Task<IChannel> GetChannel(string host, int port)
  87. {
  88. TChannel channel = null;
  89. if (this.channels.TryGetValue(host + ":" + port, out channel))
  90. {
  91. return channel;
  92. }
  93. return await ConnectAsync(host, port);
  94. }
  95. public async Task<IChannel> GetChannel(string address)
  96. {
  97. string[] ss = address.Split(':');
  98. int port = Convert.ToInt32(ss[1]);
  99. return await GetChannel(ss[0], port);
  100. }
  101. public void RunOnce(int timeout)
  102. {
  103. poller.Run(timeout);
  104. }
  105. public void Run()
  106. {
  107. while (true)
  108. {
  109. this.RunOnce(1);
  110. this.timerManager.Refresh();
  111. }
  112. }
  113. internal TimerManager Timer
  114. {
  115. get
  116. {
  117. return this.timerManager;
  118. }
  119. }
  120. }
  121. }