TService.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 sealed 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. /// <summary>
  18. /// 即可做client也可做server
  19. /// </summary>
  20. /// <param name="host"></param>
  21. /// <param name="port"></param>
  22. public TService(string host, int port)
  23. {
  24. this.acceptor = new TSocket(this.poller);
  25. this.acceptor.Bind(host, port);
  26. this.acceptor.Listen(100);
  27. }
  28. /// <summary>
  29. /// 只能做client端的构造函数
  30. /// </summary>
  31. public TService()
  32. {
  33. }
  34. private void Dispose(bool disposing)
  35. {
  36. if (this.acceptor == null)
  37. {
  38. return;
  39. }
  40. if (disposing)
  41. {
  42. foreach (ObjectId id in this.idChannels.Keys.ToArray())
  43. {
  44. TChannel channel = this.idChannels[id];
  45. channel.Dispose();
  46. }
  47. this.acceptor.Dispose();
  48. }
  49. this.acceptor = null;
  50. }
  51. ~TService()
  52. {
  53. this.Dispose(false);
  54. }
  55. public void Dispose()
  56. {
  57. this.Dispose(true);
  58. GC.SuppressFinalize(this);
  59. }
  60. public void Add(Action action)
  61. {
  62. this.poller.Add(action);
  63. }
  64. public AChannel GetChannel(ObjectId id)
  65. {
  66. TChannel channel = null;
  67. this.idChannels.TryGetValue(id, out channel);
  68. return channel;
  69. }
  70. private async Task<AChannel> ConnectAsync(string host, int port)
  71. {
  72. TSocket newSocket = new TSocket(this.poller);
  73. await newSocket.ConnectAsync(host, port);
  74. TChannel channel = new TChannel(newSocket, this);
  75. this.channels[newSocket.RemoteAddress] = channel;
  76. this.idChannels[channel.Id] = channel;
  77. return channel;
  78. }
  79. public async Task<AChannel> GetChannel()
  80. {
  81. if (this.acceptor == null)
  82. {
  83. throw new Exception(string.Format("service construct must use host and port param"));
  84. }
  85. TSocket socket = new TSocket(this.poller);
  86. await this.acceptor.AcceptAsync(socket);
  87. TChannel channel = new TChannel(socket, this);
  88. this.channels[channel.RemoteAddress] = channel;
  89. this.idChannels[channel.Id] = channel;
  90. return channel;
  91. }
  92. public void Remove(AChannel channel)
  93. {
  94. TChannel tChannel = channel as TChannel;
  95. if (tChannel == null)
  96. {
  97. return;
  98. }
  99. this.idChannels.Remove(channel.Id);
  100. this.channels.Remove(channel.RemoteAddress);
  101. this.timerManager.Remove(tChannel.SendTimer);
  102. }
  103. public async Task<AChannel> GetChannel(string host, int port)
  104. {
  105. TChannel channel = null;
  106. if (this.channels.TryGetValue(host + ":" + port, out channel))
  107. {
  108. return channel;
  109. }
  110. return await this.ConnectAsync(host, port);
  111. }
  112. public void Update()
  113. {
  114. this.poller.Update();
  115. this.timerManager.Refresh();
  116. }
  117. internal TimerManager Timer
  118. {
  119. get
  120. {
  121. return this.timerManager;
  122. }
  123. }
  124. }
  125. }