ServerLinkMnager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 
  2. using System.Collections.Concurrent;
  3. using NetCore;
  4. using NetCore.Protocol.MemoryPack;
  5. namespace NetServer.InternalServer;
  6. public class ServerLinkMnager
  7. {
  8. private Random _random = new Random(1);
  9. public static ServerLinkMnager Instance
  10. {
  11. get
  12. {
  13. if (_serverLinkMnager == null)
  14. {
  15. _serverLinkMnager = new ServerLinkMnager();
  16. }
  17. return _serverLinkMnager;
  18. }
  19. }
  20. public static ServerLinkMnager _serverLinkMnager;
  21. public class ServerInfo
  22. {
  23. public long serverId;
  24. public IConnection ServerConnection;
  25. }
  26. public ConcurrentDictionary<int, ConcurrentDictionary<long, ServerInfo>> InternalLogicManagerDic =
  27. new ConcurrentDictionary<int, ConcurrentDictionary<long, ServerInfo>>();
  28. public void AddServerConnection(ServerType serverType, IConnection iConnection)
  29. {
  30. lock (InternalLogicManagerDic)
  31. {
  32. if (InternalLogicManagerDic.TryGetValue((int)serverType, out var serverDic))
  33. {
  34. if (serverDic.TryGetValue( iConnection.ConnectionId, out var serverInfo))
  35. {
  36. Console.Error.WriteLine("重复添加了服务器");
  37. }
  38. else
  39. {
  40. serverDic.TryAdd( iConnection.ConnectionId, new ServerInfo()
  41. {
  42. serverId = iConnection.ConnectionId,
  43. ServerConnection = iConnection
  44. });
  45. }
  46. }
  47. else
  48. {
  49. serverDic = new ConcurrentDictionary<long, ServerInfo>();
  50. serverDic.TryAdd( iConnection.ConnectionId, new ServerInfo()
  51. {
  52. serverId = iConnection.ConnectionId,
  53. ServerConnection = iConnection
  54. });
  55. InternalLogicManagerDic.TryAdd((int)serverType, serverDic);
  56. }
  57. }
  58. }
  59. public void RemoveServerConnection(ServerType serverType, long serverId)
  60. {
  61. lock (InternalLogicManagerDic)
  62. {
  63. if (InternalLogicManagerDic.TryGetValue((int)serverType, out var serverDic))
  64. {
  65. if (serverDic.TryGetValue(serverId, out var serverInfo))
  66. {
  67. serverDic.TryRemove(serverId, out _);
  68. }
  69. }
  70. }
  71. }
  72. public IConnection GetServerConnection(ServerType serverType, long serverId)
  73. {
  74. if (InternalLogicManagerDic.TryGetValue((int)serverType, out var serverDic))
  75. {
  76. if (serverDic.TryGetValue(serverId, out var serverInfo))
  77. {
  78. return serverInfo.ServerConnection;
  79. }
  80. }
  81. return null;
  82. }
  83. public IConnection GetServerConnection(ServerType serverType)
  84. {
  85. if (InternalLogicManagerDic.TryGetValue((int)serverType, out var serverDic))
  86. {
  87. int maxCount = serverDic.Count;
  88. int index = _random.Next(0, maxCount);
  89. int corrIndex = 0;
  90. foreach (var VARIABLE in serverDic.Values)
  91. {
  92. if (index == corrIndex)
  93. {
  94. return VARIABLE.ServerConnection;
  95. }
  96. corrIndex++;
  97. }
  98. }
  99. return null;
  100. }
  101. }