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;
- EasyClient client = new EasyClient();
- self.FastStreamClient = client;
- client.Initialize<FastStreamBuffer>(new FastStreamMessageDecoder(FastStreamMessageDecoder.LEN_HEAD), OnSessionDataReceived);
- 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 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;
- }
- }
-
- 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-" + ConstGame.GameServerId.ToString()).ToUtf8());
- }
- private static void OnSessionDataReceived(FastStreamBuffer pack)
- {
- long usrid = (long)pack.Key;
- byte[] data = pack.Data;
-
-
- WNPlayer player = FastStreamComponent.Instance.DomainScene().GetComponent<GamePlayerComponent>().Get(usrid);
- if (player != null )
- {
- MessageHelper.SendToClient(player, new BattleEventPush() { key = (ushort)BattlePushCnst.FastStreamPush, data = data});
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- else
- {
- Log.Error($"Not found player({usrid}) @faststream data");
- }
- }
- private static void OnSessionError(object sender, ErrorEventArgs e)
- {
- Log.Warning("fast stream session error");
- FastStreamComponent self = FastStreamComponent.Instance;
-
- }
- private static void OnSessionClosed(object sender, EventArgs e)
- {
- Log.Warning("fast stream session closed");
-
- }
- }
- }
|