FastStreamComponentSystem.cs 3.5 KB

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