using SuperSocket.ClientEngine; using SuperSocket.ProtoBase; using System; using System.Linq; using System.Net; using ET; namespace ET.Server { [FriendOf(typeof(FastStreamComponent))] public static class FastStreamComponentSystem { public class FastStreamComponentAwakeSystem : AwakeSystem { protected override void Awake(FastStreamComponent self) { FastStreamComponent.Instance = self; var client = new EasyClient(); self.FastStreamClient = client; client.Initialize(new FastStreamMessageDecoder(FastStreamMessageDecoder.LEN_HEAD), (package) => { OnSessionDataReceived(package); }); client.NoDelay = true; client.ReceiveBufferSize = 10* 1024 * 1024; client.Connected += new EventHandler(OnSessionConnected); client.Error += new EventHandler(OnSessionError); client.Closed += new EventHandler(OnSessionClosed); //IPAddress ipAddress = Dns.GetHostAddresses("localhost")[0]; IPAddress ip = IPAddress.Parse("127.0.0.1"); IPEndPoint endpoint = new IPEndPoint(ip, 3370); client.ConnectAsync(endpoint); } } public class FastStreamComponentDestroySystem : DestroySystem { protected override void Destroy(FastStreamComponent self) { Log.Info($"Ice component destroyed"); self.FastStreamClient?.Close(); self.FastStreamClient = null; } } //===component扩展============= public static void SendData(this FastStreamComponent self, string key, byte[] value) { ByteBuffer buff = ByteBuffer.Allocate(2048, true); buff.WriteShort((short)key.Length); buff.WriteInt(value.Length); buff.WriteBytes(key.ToUtf8()); buff.WriteBytes(value); self.FastStreamClient.Send(new ArraySegment(buff.GetBuffer(), 0, buff.ReadableBytes)); } //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private static void OnSessionConnected(object sender, EventArgs e) { Log.Debug("fast stream session connected"); FastStreamComponent.Instance.SendData("connetorId", ("bs-" + Global.GameServerId.ToString()).ToUtf8()); } private static void OnSessionDataReceived(FastStreamBuffer pack) { var usrid = (long)pack.Key; var data = pack.Data; //Log.Debug($"receive fast stream data, uid({usrid}), msglen:{data.Length}"); //找到此unit对应的Player(id相同) var player = PlayerComponentSystem.SearchAll(usrid); if(player != null ) { var gatemap = player.GetComponent(); if(gatemap == null) { Log.Error($"player({usrid}) not entermap"); return; } var unitcomponet = gatemap.Scene.GetComponent(); var unit = unitcomponet.GetChild(usrid); if(unit != null) { MessageHelper.SendToClient(unit, new BattleEventPush() { key = (ushort)BattlePushCnst.FastStreamPush, data = data}); } else { Log.Error($"unit({usrid}) not exist @scene"); } } else { Log.Error($"Not found player({usrid}) @faststream data"); return; } } private static void OnSessionError(object sender, ErrorEventArgs e) { Log.Warning("fast stream session error"); var self = FastStreamComponent.Instance; //TODO: ReConnect战斗服 } private static void OnSessionClosed(object sender, EventArgs e) { Log.Warning("fast stream session closed"); //TODO: ReConnect战斗服 } } }