123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using SuperSocket.ClientEngine;
- using System;
- using System.Net;
- using System.Reflection;
- using System.Threading;
- 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 session = new AsyncTcpSession();
- self.Session = session;
- session.NoDelay = true;
- session.ReceiveBufferSize = 1024;
- session.Connected += new EventHandler(OnSessionConnected);
- session.Error += new EventHandler<ErrorEventArgs>(OnSessionError);
- session.Closed += new EventHandler(OnSessionClosed);
- session.DataReceived += new EventHandler<DataEventArgs>(OnSessionDataReceived);
- //IPAddress ipAddress = Dns.GetHostAddresses("localhost")[0];
- IPAddress ip = IPAddress.Parse("127.0.0.1");
- IPEndPoint endpoint = new IPEndPoint(ip, 3370);
- session.Connect(endpoint);
- }
- }
- public class FastStreamComponentDestroySystem : DestroySystem<FastStreamComponent>
- {
- protected override void Destroy(FastStreamComponent self)
- {
- Log.Info($"Ice component destroyed");
- self.Session?.Close();
- self.Session = null;
- }
- }
- public static void SendData(TcpClientSession session, string key, string value)
- {
- ByteBuffer buff = ByteBuffer.Allocate(64, true);
- buff.WriteShort((short)key.Length);
- buff.WriteInt(value.Length);
- buff.WriteBytes(key.ToUtf8());
- buff.WriteBytes(value.ToUtf8());
- session.Send(new ArraySegment<byte>(buff.GetBuffer(), 0, buff.ReadableBytes));
- }
- private static void OnSessionConnected(object sender, EventArgs e)
- {
- Log.Debug("fast stream session connected");
- SendData(sender as AsyncTcpSession, "connetorId", "bs-" + Global.GameServerId.ToString());
- }
- private static void OnSessionDataReceived(object sender, DataEventArgs e)
- {
- var buff = e.Data;
- var len = e.Length;
- Log.Debug("receive fast stream data");
- Log.Debug("======================================");
- }
- private static void OnSessionError(object sender, ErrorEventArgs e)
- {
- Log.Warning("fast stream session error");
- }
- private static void OnSessionClosed(object sender, EventArgs e)
- {
- Log.Warning("fast stream session closed");
- }
- }
-
- }
|