GateNetworkComponent.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 =
  17. new Dictionary<ObjectId, AChannel>();
  18. private void Accept(string host, int port, NetworkProtocol protocol = NetworkProtocol.TCP)
  19. {
  20. switch (protocol)
  21. {
  22. case NetworkProtocol.TCP:
  23. this.service = new TService(host, port);
  24. break;
  25. case NetworkProtocol.UDP:
  26. this.service = new UService(host, port);
  27. break;
  28. default:
  29. throw new ArgumentOutOfRangeException("protocol");
  30. }
  31. this.AcceptChannel();
  32. }
  33. public void Start()
  34. {
  35. this.Accept(World.Instance.Options.GateHost, World.Instance.Options.GatePort,
  36. World.Instance.Options.Protocol);
  37. }
  38. public void Update()
  39. {
  40. this.service.Update();
  41. }
  42. /// <summary>
  43. /// 接收连接
  44. /// </summary>
  45. private async void AcceptChannel()
  46. {
  47. while (true)
  48. {
  49. AChannel channel = await this.service.GetChannel();
  50. channel.OnDispose += this.OnChannelDispose;
  51. ProcessChannel(channel);
  52. }
  53. }
  54. /// <summary>
  55. /// 接收分发封包
  56. /// </summary>
  57. /// <param name="channel"></param>
  58. private static async void ProcessChannel(AChannel channel)
  59. {
  60. while (true)
  61. {
  62. byte[] messageBytes = await channel.RecvAsync();
  63. Opcode opcode = (Opcode)BitConverter.ToUInt16(messageBytes, 0);
  64. if (!OpcodeHelper.IsClientMessage(opcode))
  65. {
  66. continue;
  67. }
  68. ObjectId unitId = channel.GetComponent<ChannelUnitInfoComponent>().UnitId;
  69. Actor actor = World.Instance.GetComponent<ActorComponent>().Get(unitId);
  70. actor.Add(messageBytes);
  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. }