UService.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 UNet
  10. {
  11. public sealed class UService: IService
  12. {
  13. private UPoller poller;
  14. private readonly Dictionary<string, UChannel> channels = new Dictionary<string, UChannel>();
  15. private readonly Dictionary<ObjectId, UChannel> idChannels = new Dictionary<ObjectId, UChannel>();
  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 UService(string host, int port)
  23. {
  24. this.poller = new UPoller(host, (ushort) port);
  25. }
  26. /// <summary>
  27. /// 只能做client
  28. /// </summary>
  29. public UService()
  30. {
  31. this.poller = new UPoller();
  32. }
  33. private void Dispose(bool disposing)
  34. {
  35. if (this.poller == null)
  36. {
  37. return;
  38. }
  39. if (disposing)
  40. {
  41. foreach (ObjectId id in this.idChannels.Keys.ToArray())
  42. {
  43. UChannel channel = this.idChannels[id];
  44. channel.Dispose();
  45. }
  46. this.poller.Dispose();
  47. }
  48. this.poller = null;
  49. }
  50. ~UService()
  51. {
  52. this.Dispose(false);
  53. }
  54. public void Dispose()
  55. {
  56. this.Dispose(true);
  57. GC.SuppressFinalize(this);
  58. }
  59. public void Add(Action action)
  60. {
  61. this.poller.Add(action);
  62. }
  63. private async void SocketConnectAsync(AChannel channel)
  64. {
  65. while (true)
  66. {
  67. try
  68. {
  69. await channel.ConnectAsync();
  70. break;
  71. }
  72. catch (Exception e)
  73. {
  74. Log.Trace(e.ToString());
  75. }
  76. await this.Timer.Sleep(5000);
  77. }
  78. }
  79. public AChannel GetChannel(string host, int port)
  80. {
  81. UChannel channel = null;
  82. if (this.channels.TryGetValue(host + ":" + port, out channel))
  83. {
  84. return channel;
  85. }
  86. USocket newSocket = new USocket(this.poller);
  87. channel = new UChannel(newSocket, host, port, this);
  88. this.channels[channel.RemoteAddress] = channel;
  89. this.idChannels[channel.Id] = channel;
  90. this.SocketConnectAsync(channel);
  91. return channel;
  92. }
  93. public AChannel GetChannel(string address)
  94. {
  95. string[] ss = address.Split(':');
  96. int port = int.Parse(ss[1]);
  97. return this.GetChannel(ss[0], port);
  98. }
  99. public async Task<AChannel> GetChannel()
  100. {
  101. USocket socket = await this.poller.AcceptAsync();
  102. UChannel channel = new UChannel(socket, this);
  103. this.channels[channel.RemoteAddress] = channel;
  104. this.idChannels[channel.Id] = channel;
  105. return channel;
  106. }
  107. public AChannel GetChannel(ObjectId id)
  108. {
  109. UChannel channel = null;
  110. this.idChannels.TryGetValue(id, out channel);
  111. return channel;
  112. }
  113. public void Remove(AChannel channel)
  114. {
  115. UChannel tChannel = channel as UChannel;
  116. if (tChannel == null)
  117. {
  118. return;
  119. }
  120. this.idChannels.Remove(channel.Id);
  121. this.channels.Remove(channel.RemoteAddress);
  122. }
  123. public void Update()
  124. {
  125. this.poller.Update();
  126. }
  127. public TimerManager Timer
  128. {
  129. get
  130. {
  131. return this.timerManager;
  132. }
  133. }
  134. }
  135. }