FastStreamComponentSystem.cs 4.1 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. var client = new EasyClient();
  18. self.FastStreamClient = client;
  19. client.Initialize<FastStreamBuffer>(new FastStreamMessageDecoder(FastStreamMessageDecoder.LEN_HEAD), (package) => { OnSessionDataReceived(package); });
  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-" + Global.GameServerId.ToString()).ToUtf8());
  55. }
  56. private static void OnSessionDataReceived(FastStreamBuffer pack)
  57. {
  58. var usrid = (long)pack.Key;
  59. var data = pack.Data;
  60. //Log.Debug($"receive fast stream data, uid({usrid}), msglen:{data.Length}");
  61. //找到此unit对应的Player(id相同)
  62. var player = PlayerComponentSystem.SearchAll(usrid);
  63. if(player != null )
  64. {
  65. var gatemap = player.GetComponent<GateMapComponent>();
  66. if(gatemap == null)
  67. {
  68. Log.Error($"player({usrid}) not entermap");
  69. return;
  70. }
  71. var unitcomponet = gatemap.Scene.GetComponent<UnitComponent>();
  72. var unit = unitcomponet.GetChild<Unit>(usrid);
  73. if(unit != null)
  74. {
  75. MessageHelper.SendToClient(unit, new BattleEventPush() { key = (ushort)BattlePushCnst.FastStreamPush, data = data});
  76. }
  77. else
  78. {
  79. Log.Error($"unit({usrid}) not exist @scene");
  80. }
  81. }
  82. else
  83. {
  84. Log.Error($"Not found player({usrid}) @faststream data");
  85. return;
  86. }
  87. }
  88. private static void OnSessionError(object sender, ErrorEventArgs e)
  89. {
  90. Log.Warning("fast stream session error");
  91. var self = FastStreamComponent.Instance;
  92. //TODO: ReConnect战斗服
  93. }
  94. private static void OnSessionClosed(object sender, EventArgs e)
  95. {
  96. Log.Warning("fast stream session closed");
  97. //TODO: ReConnect战斗服
  98. }
  99. }
  100. }