TService.cs 3.4 KB

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