FastStreamComponentSystem.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. self.FastStreamClient.ConnectAsync(endpoint);
  24. }
  25. }
  26. public class FastStreamComponentDestroySystem: DestroySystem<FastStreamComponent>
  27. {
  28. protected override void Destroy(FastStreamComponent self)
  29. {
  30. Log.Info($"Ice component destroyed");
  31. self.FastStreamClient?.Close();
  32. self.FastStreamClient = null;
  33. }
  34. }
  35. //===component扩展=============
  36. public static void SendData(this FastStreamComponent self, string key, byte[] value)
  37. {
  38. ByteBuffer buff = ByteBuffer.Allocate(2048, true);
  39. buff.WriteShort((short)key.Length);
  40. buff.WriteInt(value.Length);
  41. buff.WriteBytes(key.ToUtf8());
  42. buff.WriteBytes(value);
  43. self.FastStreamClient.Send(new ArraySegment<byte>(buff.GetBuffer(), 0, buff.ReadableBytes));
  44. }
  45. //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  46. private static void OnSessionConnected(object sender, EventArgs e)
  47. {
  48. Log.Debug("fast stream session connected");
  49. FastStreamComponent.Instance.SendData("connetorId", ("bs-" + ConstGame.GameServerId.ToString()).ToUtf8());
  50. }
  51. private static void OnSessionDataReceived(FastStreamBuffer pack)
  52. {
  53. long usrid = (long)pack.Key;
  54. byte[] data = pack.Data;
  55. //Log.Debug($"receive fast stream data, uid({usrid}), msglen:{data.Length}");
  56. //找到此unit对应的Player(id相同)
  57. WNPlayer player = FastStreamComponent.Instance.DomainScene().GetComponent<GamePlayerComponent>().Get(usrid);
  58. if (player != null)
  59. {
  60. MessageHelper.SendToClient(player, new BattleEventPush() { key = (ushort)BattlePushCnst.FastStreamPush, data = data});
  61. }
  62. // else
  63. // {
  64. // Log.Error($"Not found player({usrid}) @faststream data");
  65. // }
  66. }
  67. private static void OnSessionError(object sender, ErrorEventArgs e)
  68. {
  69. Log.Error("fast stream session error");
  70. }
  71. private static void OnSessionClosed(object sender, EventArgs e)
  72. {
  73. Log.Warning("fast stream session closed");
  74. }
  75. }
  76. }