FastStreamComponentSystem.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using SuperSocket.ClientEngine;
  2. using System;
  3. using System.Net;
  4. using System.Reflection;
  5. using System.Threading;
  6. namespace ET.Server
  7. {
  8. [FriendOf(typeof(FastStreamComponent))]
  9. public static class FastStreamComponentSystem
  10. {
  11. public class FastStreamComponentAwakeSystem : AwakeSystem<FastStreamComponent>
  12. {
  13. protected override void Awake(FastStreamComponent self)
  14. {
  15. FastStreamComponent.Instance = self;
  16. var session = new AsyncTcpSession();
  17. self.Session = session;
  18. session.NoDelay = true;
  19. session.ReceiveBufferSize = 1024;
  20. session.Connected += new EventHandler(OnSessionConnected);
  21. session.Error += new EventHandler<ErrorEventArgs>(OnSessionError);
  22. session.Closed += new EventHandler(OnSessionClosed);
  23. session.DataReceived += new EventHandler<DataEventArgs>(OnSessionDataReceived);
  24. //IPAddress ipAddress = Dns.GetHostAddresses("localhost")[0];
  25. IPAddress ip = IPAddress.Parse("127.0.0.1");
  26. IPEndPoint endpoint = new IPEndPoint(ip, 3370);
  27. session.Connect(endpoint);
  28. }
  29. }
  30. public class FastStreamComponentDestroySystem : DestroySystem<FastStreamComponent>
  31. {
  32. protected override void Destroy(FastStreamComponent self)
  33. {
  34. Log.Info($"Ice component destroyed");
  35. self.Session?.Close();
  36. self.Session = null;
  37. }
  38. }
  39. public static void SendData(TcpClientSession session, string key, string value)
  40. {
  41. ByteBuffer buff = ByteBuffer.Allocate(64, true);
  42. buff.WriteShort((short)key.Length);
  43. buff.WriteInt(value.Length);
  44. buff.WriteBytes(key.ToUtf8());
  45. buff.WriteBytes(value.ToUtf8());
  46. session.Send(new ArraySegment<byte>(buff.GetBuffer(), 0, buff.ReadableBytes));
  47. }
  48. private static void OnSessionConnected(object sender, EventArgs e)
  49. {
  50. Log.Debug("fast stream session connected");
  51. SendData(sender as AsyncTcpSession, "connetorId", "bs-" + Global.GameServerId.ToString());
  52. }
  53. private static void OnSessionDataReceived(object sender, DataEventArgs e)
  54. {
  55. var buff = e.Data;
  56. var len = e.Length;
  57. Log.Debug("receive fast stream data");
  58. Log.Debug("======================================");
  59. }
  60. private static void OnSessionError(object sender, ErrorEventArgs e)
  61. {
  62. Log.Warning("fast stream session error");
  63. }
  64. private static void OnSessionClosed(object sender, EventArgs e)
  65. {
  66. Log.Warning("fast stream session closed");
  67. }
  68. }
  69. }