TService.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 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. public TService(string host, int port)
  15. {
  16. this.acceptor = new TSocket(poller);
  17. this.acceptor.Bind(host, port);
  18. this.acceptor.Listen(100);
  19. }
  20. public void Dispose()
  21. {
  22. if (this.poller == null)
  23. {
  24. return;
  25. }
  26. this.acceptor.Dispose();
  27. this.acceptor = null;
  28. this.poller = null;
  29. }
  30. public void Add(Action action)
  31. {
  32. this.poller.Add(action);
  33. }
  34. private async void AcceptAsync()
  35. {
  36. while (true)
  37. {
  38. TSocket newSocket = new TSocket(poller);
  39. await this.acceptor.AcceptAsync(newSocket);
  40. TChannel channel = new TChannel(newSocket, this);
  41. channels[newSocket.RemoteAddress] = channel;
  42. }
  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. return channel;
  51. }
  52. public async Task<IChannel> GetChannel()
  53. {
  54. TSocket socket = new TSocket(this.poller);
  55. await acceptor.AcceptAsync(socket);
  56. TChannel channel = new TChannel(socket, this);
  57. channels[channel.RemoteAddress] = channel;
  58. return channel;
  59. }
  60. public void Remove(IChannel channel)
  61. {
  62. TChannel tChannel = channel as TChannel;
  63. if (tChannel == null)
  64. {
  65. return;
  66. }
  67. this.channels.Remove(channel.RemoteAddress);
  68. this.timerManager.Remove(tChannel.SendTimer);
  69. }
  70. public async Task<IChannel> GetChannel(string host, int port)
  71. {
  72. TChannel channel = null;
  73. if (this.channels.TryGetValue(host + ":" + port, out channel))
  74. {
  75. return channel;
  76. }
  77. return await ConnectAsync(host, port);
  78. }
  79. public async Task<IChannel> GetChannel(string address)
  80. {
  81. string[] ss = address.Split(':');
  82. int port = Convert.ToInt32(ss[1]);
  83. return await GetChannel(ss[0], port);
  84. }
  85. public void Start()
  86. {
  87. AcceptAsync();
  88. while (true)
  89. {
  90. poller.Run(1);
  91. this.timerManager.Refresh();
  92. }
  93. }
  94. internal TimerManager Timer
  95. {
  96. get
  97. {
  98. return this.timerManager;
  99. }
  100. }
  101. }
  102. }