GateNetworkComponent.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using Common.Base;
  4. using Common.Network;
  5. using MongoDB.Bson;
  6. using TNet;
  7. using UNet;
  8. namespace Model
  9. {
  10. /// <summary>
  11. /// gate对外连接使用
  12. /// </summary>
  13. public class GateNetworkComponent: Component<World>, IUpdate, IStart
  14. {
  15. private IService service;
  16. private readonly Dictionary<ObjectId, AChannel> unitIdChannels = new Dictionary<ObjectId, AChannel>();
  17. private void Accept(string host, int port, NetworkProtocol protocol = NetworkProtocol.TCP)
  18. {
  19. switch (protocol)
  20. {
  21. case NetworkProtocol.TCP:
  22. this.service = new TService(host, port);
  23. break;
  24. case NetworkProtocol.UDP:
  25. this.service = new UService(host, port);
  26. break;
  27. default:
  28. throw new ArgumentOutOfRangeException("protocol");
  29. }
  30. this.AcceptChannel();
  31. }
  32. public void Start()
  33. {
  34. this.Accept(World.Instance.Options.GateHost, World.Instance.Options.GatePort,
  35. World.Instance.Options.Protocol);
  36. }
  37. public void Update()
  38. {
  39. this.service.Update();
  40. }
  41. /// <summary>
  42. /// 接收连接
  43. /// </summary>
  44. private async void AcceptChannel()
  45. {
  46. while (true)
  47. {
  48. AChannel channel = await this.service.GetChannel();
  49. channel.OnDispose += this.OnChannelDispose;
  50. ProcessChannel(channel);
  51. }
  52. }
  53. /// <summary>
  54. /// 接收分发封包
  55. /// </summary>
  56. /// <param name="channel"></param>
  57. private static async void ProcessChannel(AChannel channel)
  58. {
  59. while (true)
  60. {
  61. byte[] message = await channel.RecvAsync();
  62. Env env = new Env();
  63. env[EnvKey.Channel] = channel;
  64. env[EnvKey.Message] = message;
  65. int opcode = BitConverter.ToUInt16(message, 0);
  66. if (!MessageTypeHelper.IsClientMessage(opcode))
  67. {
  68. continue;
  69. }
  70. World.Instance.GetComponent<EventComponent<EventAttribute>>().Run(EventType.GateRecvClientMessage, env);
  71. }
  72. }
  73. // channel删除的时候需要清除与unit id的关联
  74. private void OnChannelDispose(AChannel channel)
  75. {
  76. ChannelUnitInfoComponent channelUnitInfoComponent =
  77. channel.GetComponent<ChannelUnitInfoComponent>();
  78. if (channelUnitInfoComponent != null)
  79. {
  80. this.unitIdChannels.Remove(channelUnitInfoComponent.UnitId);
  81. }
  82. }
  83. // 将unit id与channel关联起来
  84. public void AssociateUnitIdAndChannel(ObjectId id, AChannel channel)
  85. {
  86. this.unitIdChannels[id] = channel;
  87. }
  88. public void SendAsync(ObjectId id, byte[] buffer)
  89. {
  90. AChannel channel;
  91. if (!this.unitIdChannels.TryGetValue(id, out channel))
  92. {
  93. return;
  94. }
  95. channel.SendAsync(buffer);
  96. }
  97. }
  98. }