TService.cs 3.1 KB

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