mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
[+] Network Module
[+] Network Module
This commit is contained in:
3
Assets/GameScripts/HotFix/GameLogic/Network.meta
Normal file
3
Assets/GameScripts/HotFix/GameLogic/Network.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 145b951be40d41dea06e76bd967a5d15
|
||||
timeCreated: 1682045847
|
19
Assets/GameScripts/HotFix/GameLogic/Network/HeartBeat.cs
Normal file
19
Assets/GameScripts/HotFix/GameLogic/Network/HeartBeat.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using ProtoBuf;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
[Serializable, ProtoContract(Name = @"HeartBeat")]
|
||||
public class HeartBeat : PacketBase
|
||||
{
|
||||
public HeartBeat()
|
||||
{
|
||||
}
|
||||
|
||||
public override int Id => 1;
|
||||
|
||||
public override void Clear()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d8b9b60d2abf409ca9dd2ce00506ac79
|
||||
timeCreated: 1682046644
|
@@ -0,0 +1,254 @@
|
||||
using ProtoBuf;
|
||||
using ProtoBuf.Meta;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Reflection;
|
||||
using TEngine;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
public class NetworkChannelHelper : INetworkChannelHelper, IMemory
|
||||
{
|
||||
private readonly Dictionary<int, Type> _serverToClientPacketTypes = new Dictionary<int, Type>();
|
||||
private readonly MemoryStream _cachedStream = new MemoryStream(1024 * 8);
|
||||
private INetworkChannel _networkChannel = null;
|
||||
|
||||
/// <summary>
|
||||
/// 获取消息包头长度。
|
||||
/// <remarks>4。</remarks>
|
||||
/// </summary>
|
||||
public int PacketHeaderLength => sizeof(int);
|
||||
|
||||
/// <summary>
|
||||
/// 初始化网络频道辅助器。
|
||||
/// </summary>
|
||||
/// <param name="networkChannel">网络频道。</param>
|
||||
public void Initialize(INetworkChannel networkChannel)
|
||||
{
|
||||
_networkChannel = networkChannel;
|
||||
|
||||
// 反射注册包和包处理函数。
|
||||
Type packetBaseType = typeof(ProtoPacket);
|
||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||
Type[] types = assembly.GetTypes();
|
||||
for (int i = 0; i < types.Length; i++)
|
||||
{
|
||||
if (!types[i].IsClass || types[i].IsAbstract)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (types[i].BaseType == packetBaseType)
|
||||
{
|
||||
PacketBase packetBase = (PacketBase)Activator.CreateInstance(types[i]);
|
||||
Type packetType = GetServerToClientPacketType(packetBase.Id);
|
||||
if (packetType != null)
|
||||
{
|
||||
Log.Warning("Already exist packet type '{0}', check '{1}' or '{2}'?.", packetBase.Id.ToString(), packetType.Name, packetBase.GetType().Name);
|
||||
continue;
|
||||
}
|
||||
|
||||
_serverToClientPacketTypes.Add(packetBase.Id, types[i]);
|
||||
}
|
||||
}
|
||||
|
||||
GameEvent.AddEventListener<INetworkChannel, object>(NetworkEvent.NetworkConnectedEvent, OnNetworkConnected);
|
||||
GameEvent.AddEventListener<INetworkChannel>(NetworkEvent.NetworkClosedEvent, OnNetworkClosed);
|
||||
GameEvent.AddEventListener<INetworkChannel, int>(NetworkEvent.NetworkMissHeartBeatEvent, OnNetworkMissHeartBeat);
|
||||
GameEvent.AddEventListener<INetworkChannel, NetworkErrorCode, SocketError, string>(NetworkEvent.NetworkErrorEvent, OnNetworkError);
|
||||
GameEvent.AddEventListener<INetworkChannel, object>(NetworkEvent.NetworkCustomErrorEvent, OnNetworkCustomError);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭并清理网络频道辅助器。
|
||||
/// </summary>
|
||||
public void Shutdown()
|
||||
{
|
||||
GameEvent.RemoveEventListener<INetworkChannel, object>(NetworkEvent.NetworkConnectedEvent, OnNetworkConnected);
|
||||
GameEvent.RemoveEventListener<INetworkChannel>(NetworkEvent.NetworkClosedEvent, OnNetworkClosed);
|
||||
GameEvent.RemoveEventListener<INetworkChannel, int>(NetworkEvent.NetworkMissHeartBeatEvent, OnNetworkMissHeartBeat);
|
||||
GameEvent.RemoveEventListener<INetworkChannel, NetworkErrorCode, SocketError, string>(NetworkEvent.NetworkErrorEvent, OnNetworkError);
|
||||
GameEvent.RemoveEventListener<INetworkChannel, object>(NetworkEvent.NetworkCustomErrorEvent, OnNetworkCustomError);
|
||||
|
||||
_networkChannel = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 准备进行连接。
|
||||
/// </summary>
|
||||
public void PrepareForConnecting()
|
||||
{
|
||||
_networkChannel.Socket.ReceiveBufferSize = 1024 * 64;
|
||||
_networkChannel.Socket.SendBufferSize = 1024 * 64;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送心跳消息包。
|
||||
/// </summary>
|
||||
/// <returns>是否发送心跳消息包成功。</returns>
|
||||
public bool SendHeartBeat()
|
||||
{
|
||||
_networkChannel.Send(MemoryPool.Acquire<HeartBeat>());
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 序列化消息包。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">消息包类型。</typeparam>
|
||||
/// <param name="packet">要序列化的消息包。</param>
|
||||
/// <param name="destination">要序列化的目标流。</param>
|
||||
/// <returns>是否序列化成功。</returns>
|
||||
public bool Serialize<T>(T packet, Stream destination) where T : Packet
|
||||
{
|
||||
PacketBase packetImpl = packet as PacketBase;
|
||||
if (packetImpl == null)
|
||||
{
|
||||
Log.Warning("Packet is invalid.");
|
||||
return false;
|
||||
}
|
||||
|
||||
_cachedStream.SetLength(_cachedStream.Capacity); // 此行防止 Array.Copy 的数据无法写入
|
||||
_cachedStream.Position = 0L;
|
||||
|
||||
PacketHeader packetHeader = MemoryPool.Acquire<PacketHeader>();
|
||||
Serializer.Serialize(_cachedStream, packetHeader);
|
||||
MemoryPool.Release(packetHeader);
|
||||
|
||||
Serializer.SerializeWithLengthPrefix(_cachedStream, packet, PrefixStyle.Fixed32);
|
||||
MemoryPool.Release((IMemory)packet);
|
||||
|
||||
_cachedStream.WriteTo(destination);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化消息包头。
|
||||
/// </summary>
|
||||
/// <param name="source">要反序列化的来源流。</param>
|
||||
/// <param name="customErrorData">用户自定义错误数据。</param>
|
||||
/// <returns>反序列化后的消息包头。</returns>
|
||||
public IPacketHeader DeserializePacketHeader(Stream source, out object customErrorData)
|
||||
{
|
||||
// 注意:此函数并不在主线程调用!
|
||||
customErrorData = null;
|
||||
return (IPacketHeader)RuntimeTypeModel.Default.Deserialize(source, MemoryPool.Acquire<PacketHeader>(), typeof(PacketHeader));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化消息包。
|
||||
/// </summary>
|
||||
/// <param name="packetHeader">消息包头。</param>
|
||||
/// <param name="source">要反序列化的来源流。</param>
|
||||
/// <param name="customErrorData">用户自定义错误数据。</param>
|
||||
/// <returns>反序列化后的消息包。</returns>
|
||||
public Packet DeserializePacket(IPacketHeader packetHeader, Stream source, out object customErrorData)
|
||||
{
|
||||
// 注意:此函数并不在主线程调用!
|
||||
customErrorData = null;
|
||||
|
||||
PacketHeader scPacketHeader = packetHeader as PacketHeader;
|
||||
if (scPacketHeader == null)
|
||||
{
|
||||
Log.Warning("Packet header is invalid.");
|
||||
return null;
|
||||
}
|
||||
|
||||
Packet packet = null;
|
||||
if (scPacketHeader.IsValid)
|
||||
{
|
||||
Type packetType = GetServerToClientPacketType(scPacketHeader.Id);
|
||||
if (packetType != null)
|
||||
{
|
||||
packet = (Packet)RuntimeTypeModel.Default.DeserializeWithLengthPrefix(source, MemoryPool.Acquire(packetType), packetType, PrefixStyle.Fixed32, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Warning("Can not deserialize packet for packet id '{0}'.", scPacketHeader.Id.ToString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Warning("Packet header is invalid.");
|
||||
}
|
||||
|
||||
MemoryPool.Release(scPacketHeader);
|
||||
return packet;
|
||||
}
|
||||
|
||||
private Type GetServerToClientPacketType(int id)
|
||||
{
|
||||
if (_serverToClientPacketTypes.TryGetValue(id, out var type))
|
||||
{
|
||||
return type;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void OnNetworkConnected(INetworkChannel channel, object userdata)
|
||||
{
|
||||
if (channel != _networkChannel)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Info("Network channel '{0}' connected, local address '{1}', remote address '{2}'.",
|
||||
channel.Name, channel.Socket.LocalEndPoint.ToString(),
|
||||
channel.Socket.RemoteEndPoint.ToString());
|
||||
}
|
||||
|
||||
private void OnNetworkClosed(INetworkChannel channel)
|
||||
{
|
||||
if (channel != _networkChannel)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Info("Network channel '{0}' closed.", channel.Name);
|
||||
}
|
||||
|
||||
private void OnNetworkMissHeartBeat(INetworkChannel channel, int missCount)
|
||||
{
|
||||
if (channel != _networkChannel)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Info("Network channel '{0}' miss heart beat '{1}' times.", channel.Name, missCount.ToString());
|
||||
|
||||
if (missCount < 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
channel.Close();
|
||||
}
|
||||
|
||||
private void OnNetworkError(INetworkChannel channel, NetworkErrorCode networkErrorCode, SocketError socketError, string errorMessage)
|
||||
{
|
||||
if (channel != _networkChannel)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Info("Network channel '{0}' error, error code is '{1}', error message is '{2}'.", channel.Name, networkErrorCode.ToString(), errorMessage);
|
||||
|
||||
channel.Close();
|
||||
}
|
||||
|
||||
private void OnNetworkCustomError(INetworkChannel channel, object userData)
|
||||
{
|
||||
if (channel != _networkChannel)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bf86ce2ddfb5429abecfb06257c86acd
|
||||
timeCreated: 1682045961
|
24
Assets/GameScripts/HotFix/GameLogic/Network/PacketBase.cs
Normal file
24
Assets/GameScripts/HotFix/GameLogic/Network/PacketBase.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using TEngine;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
/// <summary>
|
||||
/// 网络消息包基类。
|
||||
/// </summary>
|
||||
public abstract class PacketBase : Packet
|
||||
{
|
||||
/// <summary>
|
||||
/// 网络消息包Id。
|
||||
/// </summary>
|
||||
public int ProtoId;
|
||||
|
||||
/// <summary>
|
||||
/// 网络消息包包体。
|
||||
/// </summary>
|
||||
public byte[] ProtoBody;
|
||||
|
||||
public void Close()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: acea4283f57644b6b4452354d23f2803
|
||||
timeCreated: 1682045887
|
47
Assets/GameScripts/HotFix/GameLogic/Network/PacketHeader.cs
Normal file
47
Assets/GameScripts/HotFix/GameLogic/Network/PacketHeader.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using TEngine;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
/// <summary>
|
||||
/// 网络消息包头。
|
||||
/// </summary>
|
||||
public class PacketHeader : IPacketHeader, IMemory
|
||||
{
|
||||
/// <summary>
|
||||
/// 网络消息包Id。
|
||||
/// </summary>
|
||||
public short Id
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 网络消息包长度。
|
||||
/// </summary>
|
||||
public int PacketLength
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 网络消息包是否合法。
|
||||
/// </summary>
|
||||
public bool IsValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return PacketLength >= 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除网络消息包头。
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
PacketLength = 0;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30edc133b73141598b5351d4afc2ad93
|
||||
timeCreated: 1682046771
|
15
Assets/GameScripts/HotFix/GameLogic/Network/ProtoPacket.cs
Normal file
15
Assets/GameScripts/HotFix/GameLogic/Network/ProtoPacket.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace GameLogic
|
||||
{
|
||||
/// <summary>
|
||||
/// 网络消息包。
|
||||
/// </summary>
|
||||
public partial class ProtoPacket : PacketBase
|
||||
{
|
||||
public override int Id => 1;
|
||||
|
||||
public override void Clear()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: da70995486db4799baba570b1767ab86
|
||||
timeCreated: 1682045865
|
138
Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtils.cs
Normal file
138
Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtils.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
// using System;
|
||||
// using System.ComponentModel;
|
||||
// using System.IO;
|
||||
// using ProtoBuf;
|
||||
// using ProtoBuf.Meta;
|
||||
//
|
||||
// /// <summary>
|
||||
// /// ProtoBuf工具
|
||||
// /// </summary>
|
||||
// public class ProtobufUtils
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 消息压入内存流。
|
||||
// /// </summary>
|
||||
// /// <param name="message"></param>
|
||||
// /// <param name="stream"></param>
|
||||
// public static void ToStream(object message, MemoryStream stream)
|
||||
// {
|
||||
// ((IMessage)message).WriteTo(stream);
|
||||
// }
|
||||
//
|
||||
// /// <summary>
|
||||
// /// 比特流解析。
|
||||
// /// </summary>
|
||||
// /// <param name="type"></param>
|
||||
// /// <param name="bytes"></param>
|
||||
// /// <param name="index"></param>
|
||||
// /// <param name="count"></param>
|
||||
// /// <returns></returns>
|
||||
// public static object FromBytes(Type type, byte[] bytes, int index, int count)
|
||||
// {
|
||||
// object message = Activator.CreateInstance(type);
|
||||
// ((IMessage)message).MergeFrom(bytes, index, count);
|
||||
// ISupportInitialize iSupportInitialize = message as ISupportInitialize;
|
||||
// if (iSupportInitialize == null)
|
||||
// {
|
||||
// return message;
|
||||
// }
|
||||
//
|
||||
// iSupportInitialize.EndInit();
|
||||
// return message;
|
||||
// }
|
||||
//
|
||||
// /// <summary>
|
||||
// /// 比特流解析。
|
||||
// /// </summary>
|
||||
// /// <param name="instance"></param>
|
||||
// /// <param name="bytes"></param>
|
||||
// /// <param name="index"></param>
|
||||
// /// <param name="count"></param>
|
||||
// /// <returns></returns>
|
||||
// public static object FromBytes(object instance, byte[] bytes, int index, int count)
|
||||
// {
|
||||
// object message = instance;
|
||||
// ((IMessage)message).MergeFrom(bytes, index, count);
|
||||
// ISupportInitialize iSupportInitialize = message as ISupportInitialize;
|
||||
// if (iSupportInitialize == null)
|
||||
// {
|
||||
// return message;
|
||||
// }
|
||||
//
|
||||
// iSupportInitialize.EndInit();
|
||||
// return message;
|
||||
// }
|
||||
//
|
||||
// /// <summary>
|
||||
// /// 从内存流取出。
|
||||
// /// </summary>
|
||||
// /// <param name="type"></param>
|
||||
// /// <param name="stream"></param>
|
||||
// /// <returns></returns>
|
||||
// public static object FromStream(Type type, MemoryStream stream)
|
||||
// {
|
||||
// object message = Activator.CreateInstance(type);
|
||||
// ((IMessage)message).MergeFrom(stream.GetBuffer(), (int)stream.Position, (int)stream.Length);
|
||||
// ISupportInitialize iSupportInitialize = message as ISupportInitialize;
|
||||
// if (iSupportInitialize == null)
|
||||
// {
|
||||
// return message;
|
||||
// }
|
||||
//
|
||||
// iSupportInitialize.EndInit();
|
||||
// return message;
|
||||
// }
|
||||
//
|
||||
// /// <summary>
|
||||
// /// 从内存流取出。
|
||||
// /// </summary>
|
||||
// /// <param name="message"></param>
|
||||
// /// <param name="stream"></param>
|
||||
// /// <returns></returns>
|
||||
// public static object FromStream(object message, MemoryStream stream)
|
||||
// {
|
||||
// // TODO 这个message最好从池中获取,减少gc
|
||||
// ((IMessage)message).MergeFrom(stream.GetBuffer(), (int)stream.Position, (int)stream.Length);
|
||||
// ISupportInitialize iSupportInitialize = message as ISupportInitialize;
|
||||
// if (iSupportInitialize == null)
|
||||
// {
|
||||
// return message;
|
||||
// }
|
||||
//
|
||||
// iSupportInitialize.EndInit();
|
||||
// return message;
|
||||
// }
|
||||
//
|
||||
// /// <summary>
|
||||
// /// 序列化protobuf
|
||||
// /// </summary>
|
||||
// /// <param name="message"></param>
|
||||
// /// <returns></returns>
|
||||
// public static byte[] Serialize(object message)
|
||||
// {
|
||||
// return ((IMessage)message).ToByteArray();
|
||||
// }
|
||||
//
|
||||
// /// <summary>
|
||||
// /// 反序列化protobuf
|
||||
// /// </summary>
|
||||
// /// <typeparam name="T"></typeparam>
|
||||
// /// <param name="dataBytes"></param>
|
||||
// /// <returns></returns>
|
||||
// public static T Deserialize<T>(byte[] dataBytes) where T : IMessage, new()
|
||||
// {
|
||||
// T msg = new T();
|
||||
// msg = (T)msg.Descriptor.Parser.ParseFrom(dataBytes);
|
||||
// return msg;
|
||||
// }
|
||||
//
|
||||
// public static int GetHighOrder(int cmdMerge)
|
||||
// {
|
||||
// return cmdMerge >> 16;
|
||||
// }
|
||||
//
|
||||
// public static int GetLowOrder(int cmdMerge)
|
||||
// {
|
||||
// return cmdMerge & 65535;
|
||||
// }
|
||||
// }
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 972ca4545003463d8710de956f0fde66
|
||||
timeCreated: 1682047511
|
@@ -129,6 +129,13 @@ namespace TEngine
|
||||
/// <param name="msgDelegate">要注册的网络消息包处理函数。</param>
|
||||
void RemoveMsgHandler(int msgId, CsMsgDelegate msgDelegate);
|
||||
|
||||
/// <summary>
|
||||
/// 连接到远程主机。
|
||||
/// </summary>
|
||||
/// <param name="ipAddress">远程主机的 IP 地址。</param>
|
||||
/// <param name="port">远程主机的端口号。</param>
|
||||
void Connect(string ipAddress, int port);
|
||||
|
||||
/// <summary>
|
||||
/// 连接到远程主机。
|
||||
/// </summary>
|
||||
|
@@ -275,6 +275,17 @@ namespace TEngine
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接到远程主机。
|
||||
/// </summary>
|
||||
/// <param name="ipAddress">远程主机的 IP 地址。</param>
|
||||
/// <param name="port">远程主机的端口号。</param>
|
||||
public void Connect(string ipAddress, int port)
|
||||
{
|
||||
IPAddress address = IPAddress.Parse(ipAddress);
|
||||
Connect(address, port, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接到远程主机。
|
||||
/// </summary>
|
||||
|
Reference in New Issue
Block a user