TService.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /// 用作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. public void Dispose()
  32. {
  33. if (this.acceptor == null)
  34. {
  35. return;
  36. }
  37. this.acceptor.Dispose();
  38. this.acceptor = null;
  39. }
  40. public void Add(Action action)
  41. {
  42. this.poller.Add(action);
  43. }
  44. private async Task<IChannel> ConnectAsync(string host, int port)
  45. {
  46. TSocket newSocket = new TSocket(poller);
  47. await newSocket.ConnectAsync(host, port);
  48. TChannel channel = new TChannel(newSocket, this);
  49. channels[newSocket.RemoteAddress] = channel;
  50. channel.Start();
  51. return channel;
  52. }
  53. public async Task<IChannel> GetChannel()
  54. {
  55. if (this.acceptor == null)
  56. {
  57. throw new Exception(string.Format("service construct must use host and port param"));
  58. }
  59. TSocket socket = new TSocket(this.poller);
  60. await acceptor.AcceptAsync(socket);
  61. TChannel channel = new TChannel(socket, this);
  62. channels[channel.RemoteAddress] = channel;
  63. channel.Start();
  64. return channel;
  65. }
  66. public void Remove(IChannel channel)
  67. {
  68. TChannel tChannel = channel as TChannel;
  69. if (tChannel == null)
  70. {
  71. return;
  72. }
  73. this.channels.Remove(channel.RemoteAddress);
  74. this.timerManager.Remove(tChannel.SendTimer);
  75. }
  76. public async Task<IChannel> GetChannel(string host, int port)
  77. {
  78. TChannel channel = null;
  79. if (this.channels.TryGetValue(host + ":" + port, out channel))
  80. {
  81. return channel;
  82. }
  83. return await ConnectAsync(host, port);
  84. }
  85. public async Task<IChannel> GetChannel(string address)
  86. {
  87. string[] ss = address.Split(':');
  88. int port = Convert.ToInt32(ss[1]);
  89. return await GetChannel(ss[0], port);
  90. }
  91. public void RunOnce(int timeout)
  92. {
  93. poller.Run(timeout);
  94. }
  95. public void Run()
  96. {
  97. while (true)
  98. {
  99. this.RunOnce(1);
  100. this.timerManager.Refresh();
  101. }
  102. }
  103. internal TimerManager Timer
  104. {
  105. get
  106. {
  107. return this.timerManager;
  108. }
  109. }
  110. }
  111. }