FastStreamComponentSystem.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  67. // else
  68. // {
  69. // Log.Error($"Not found player({usrid}) @faststream data");
  70. // }
  71. }
  72. private static void OnSessionError(object sender, ErrorEventArgs e)
  73. {
  74. Log.Warning("fast stream session error");
  75. FastStreamComponent self = FastStreamComponent.Instance;
  76. //TODO: ReConnect战斗服
  77. }
  78. private static void OnSessionClosed(object sender, EventArgs e)
  79. {
  80. Log.Warning("fast stream session closed");
  81. //TODO: ReConnect战斗服
  82. }
  83. }
  84. }