123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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<FastStreamComponent>
- {
- protected override void Awake(FastStreamComponent self)
- {
- FastStreamComponent.Instance = self;
- var client = new EasyClient();
- self.FastStreamClient = client;
- client.Initialize<FastStreamBuffer>(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<ErrorEventArgs>(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<FastStreamComponent>
- {
- 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<byte>(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<GateMapComponent>();
- if(gatemap == null)
- {
- Log.Error($"player({usrid}) not entermap");
- return;
- }
- var unitcomponet = gatemap.Scene.GetComponent<UnitComponent>();
- var unit = unitcomponet.GetChild<Unit>(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战斗服
- }
- }
-
- }
|