FastStreamComponentSystem.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using SuperSocket.ClientEngine;
  2. using SuperSocket.ProtoBase;
  3. using System;
  4. using System.Linq;
  5. using System.Net;
  6. using ET;
  7. namespace ET.Server
  8. {
  9. [FriendOf(typeof(FastStreamComponent))]
  10. public static class FastStreamComponentSystem
  11. {
  12. public class FastStreamComponentAwakeSystem: AwakeSystem<FastStreamComponent>
  13. {
  14. protected override void Awake(FastStreamComponent self)
  15. {
  16. FastStreamComponent.Instance = self;
  17. EasyClient client = new EasyClient();
  18. self.FastStreamClient = client;
  19. client.Initialize<FastStreamBuffer>(new FastStreamMessageDecoder(FastStreamMessageDecoder.LEN_HEAD), OnSessionDataReceived);
  20. client.NoDelay = true;
  21. client.ReceiveBufferSize = 10* 1024 * 1024;
  22. client.Connected += new EventHandler(OnSessionConnected);
  23. client.Error += new EventHandler<ErrorEventArgs>(OnSessionError);
  24. client.Closed += new EventHandler(OnSessionClosed);
  25. //IPAddress ipAddress = Dns.GetHostAddresses("localhost")[0];
  26. IPAddress ip = IPAddress.Parse("127.0.0.1");
  27. IPEndPoint endpoint = new IPEndPoint(ip, 3370);
  28. client.ConnectAsync(endpoint);
  29. }
  30. }
  31. public class FastStreamComponentDestroySystem: DestroySystem<FastStreamComponent>
  32. {
  33. protected override void Destroy(FastStreamComponent self)
  34. {
  35. Log.Info($"Ice component destroyed");
  36. self.FastStreamClient?.Close();
  37. self.FastStreamClient = null;
  38. }
  39. }
  40. //===component扩展=============
  41. public static void SendData(this FastStreamComponent self, string key, byte[] value)
  42. {
  43. ByteBuffer buff = ByteBuffer.Allocate(2048, true);
  44. buff.WriteShort((short)key.Length);
  45. buff.WriteInt(value.Length);
  46. buff.WriteBytes(key.ToUtf8());
  47. buff.WriteBytes(value);
  48. self.FastStreamClient.Send(new ArraySegment<byte>(buff.GetBuffer(), 0, buff.ReadableBytes));
  49. }
  50. //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  51. private static void OnSessionConnected(object sender, EventArgs e)
  52. {
  53. Log.Debug("fast stream session connected");
  54. FastStreamComponent.Instance.SendData("connetorId", ("bs-" + ConstGame.GameServerId.ToString()).ToUtf8());
  55. }
  56. private static void OnSessionDataReceived(FastStreamBuffer pack)
  57. {
  58. long usrid = (long)pack.Key;
  59. byte[] data = pack.Data;
  60. //Log.Debug($"receive fast stream data, uid({usrid}), msglen:{data.Length}");
  61. //找到此unit对应的Player(id相同)
  62. WNPlayer player = FastStreamComponent.Instance.DomainScene().GetComponent<GamePlayerComponent>().Get(usrid);
  63. if (player != null )
  64. {
  65. MessageHelper.SendToClient(player, new BattleEventPush() { key = (ushort)BattlePushCnst.FastStreamPush, data = data});
  66. // var gatemap = player.GetComponent<GateMapComponent>();
  67. // if(gatemap == null)
  68. // {
  69. // Log.Error($"player({usrid}) not entermap");
  70. // return;
  71. // }
  72. //
  73. // var unitcomponet = gatemap.Scene.GetComponent<UnitComponent>();
  74. // var unit = unitcomponet.GetChild<Unit>(usrid);
  75. // if(unit != null)
  76. // {
  77. // MessageHelper.SendToClient(unit, new BattleEventPush() { key = (ushort)BattlePushCnst.FastStreamPush, data = data});
  78. // }
  79. // else
  80. // {
  81. // Log.Error($"unit({usrid}) not exist @scene");
  82. // }
  83. }
  84. else
  85. {
  86. Log.Error($"Not found player({usrid}) @faststream data");
  87. }
  88. }
  89. private static void OnSessionError(object sender, ErrorEventArgs e)
  90. {
  91. Log.Warning("fast stream session error");
  92. FastStreamComponent self = FastStreamComponent.Instance;
  93. //TODO: ReConnect战斗服
  94. }
  95. private static void OnSessionClosed(object sender, EventArgs e)
  96. {
  97. Log.Warning("fast stream session closed");
  98. //TODO: ReConnect战斗服
  99. }
  100. }
  101. }