TService.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Common.Base;
  6. using Common.Logger;
  7. using Common.Network;
  8. using MongoDB.Bson;
  9. namespace TNet
  10. {
  11. public sealed class TService: IService
  12. {
  13. private readonly IPoller poller = new TPoller();
  14. private TSocket acceptor;
  15. private readonly Dictionary<string, TChannel> channels = new Dictionary<string, TChannel>();
  16. private readonly Dictionary<ObjectId, TChannel> idChannels = new Dictionary<ObjectId, TChannel>();
  17. private readonly TimerManager timerManager = new TimerManager();
  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. private void Dispose(bool disposing)
  36. {
  37. if (this.acceptor == null)
  38. {
  39. return;
  40. }
  41. if (disposing)
  42. {
  43. foreach (ObjectId id in this.idChannels.Keys.ToArray())
  44. {
  45. TChannel channel = this.idChannels[id];
  46. channel.Dispose();
  47. }
  48. this.acceptor.Dispose();
  49. }
  50. this.acceptor = null;
  51. }
  52. ~TService()
  53. {
  54. this.Dispose(false);
  55. }
  56. public void Dispose()
  57. {
  58. this.Dispose(true);
  59. GC.SuppressFinalize(this);
  60. }
  61. public void Add(Action action)
  62. {
  63. this.poller.Add(action);
  64. }
  65. public AChannel GetChannel(ObjectId id)
  66. {
  67. TChannel channel = null;
  68. this.idChannels.TryGetValue(id, out channel);
  69. return channel;
  70. }
  71. public async Task<AChannel> GetChannel()
  72. {
  73. if (this.acceptor == null)
  74. {
  75. throw new Exception("service construct must use host and port param");
  76. }
  77. TSocket socket = new TSocket(this.poller);
  78. await this.acceptor.AcceptAsync(socket);
  79. TChannel channel = new TChannel(socket, this);
  80. this.channels[channel.RemoteAddress] = channel;
  81. this.idChannels[channel.Id] = channel;
  82. return channel;
  83. }
  84. public void Remove(AChannel channel)
  85. {
  86. TChannel tChannel = channel as TChannel;
  87. if (tChannel == null)
  88. {
  89. return;
  90. }
  91. this.idChannels.Remove(channel.Id);
  92. this.channels.Remove(channel.RemoteAddress);
  93. this.timerManager.Remove(tChannel.SendTimer);
  94. }
  95. private async void SocketConnectAsync(AChannel channel)
  96. {
  97. while (true)
  98. {
  99. try
  100. {
  101. await channel.ConnectAsync();
  102. break;
  103. }
  104. catch (Exception e)
  105. {
  106. Log.Trace(e.ToString());
  107. }
  108. await this.Timer.Sleep(5000);
  109. }
  110. }
  111. public AChannel GetChannel(string host, int port)
  112. {
  113. TChannel channel = null;
  114. if (this.channels.TryGetValue(host + ":" + port, out channel))
  115. {
  116. return channel;
  117. }
  118. TSocket newSocket = new TSocket(this.poller);
  119. channel = new TChannel(newSocket, host, port, this);
  120. this.channels[channel.RemoteAddress] = channel;
  121. this.idChannels[channel.Id] = channel;
  122. this.SocketConnectAsync(channel);
  123. return channel;
  124. }
  125. public AChannel GetChannel(string address)
  126. {
  127. string[] ss = address.Split(':');
  128. int port = int.Parse(ss[1]);
  129. return this.GetChannel(ss[0], port);
  130. }
  131. public void Update()
  132. {
  133. this.poller.Update();
  134. this.timerManager.Refresh();
  135. }
  136. public TimerManager Timer
  137. {
  138. get
  139. {
  140. return this.timerManager;
  141. }
  142. }
  143. }
  144. }