Packet & ProtoTools

Packet & ProtoTools
This commit is contained in:
ALEXTANG
2022-09-01 11:30:38 +08:00
parent 6d38c5ae68
commit b9e68ed948
11 changed files with 63 additions and 58 deletions

View File

@@ -57,6 +57,7 @@ namespace TEngine.Editor
string s = File.ReadAllText(proto); string s = File.ReadAllText(proto);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append("using System;\n");
sb.Append("using ProtoBuf;\n"); sb.Append("using ProtoBuf;\n");
sb.Append("using TEngine.Runtime;\n"); sb.Append("using TEngine.Runtime;\n");
sb.Append("using System.Collections.Generic;\n"); sb.Append("using System.Collections.Generic;\n");
@@ -100,7 +101,8 @@ namespace TEngine.Editor
} }
msgOpcode.Add(new OpcodeInfo() { Name = msgName, Opcode = ++startOpcode }); msgOpcode.Add(new OpcodeInfo() { Name = msgName, Opcode = ++startOpcode });
sb.Append($"\t[global::ProtoBuf.ProtoContract()]\n"); sb.Append($"\t[Serializable,global::ProtoBuf.ProtoContract(Name = @\"{msgName}\")]\n");
// sb.Append($"\t[global::ProtoBuf.ProtoContract()]\n");
if (useMemoryPool) if (useMemoryPool)
{ {
sb.Append($"\tpublic partial class {msgName}: IMemory"); sb.Append($"\tpublic partial class {msgName}: IMemory");

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using TEngineProto;
namespace TEngine.Runtime namespace TEngine.Runtime
{ {
@@ -147,7 +148,7 @@ namespace TEngine.Runtime
/// </summary> /// </summary>
/// <typeparam name="T">消息包类型。</typeparam> /// <typeparam name="T">消息包类型。</typeparam>
/// <param name="packet">要发送的消息包。</param> /// <param name="packet">要发送的消息包。</param>
bool Send<T>(T packet) where T : Packet; bool Send(MainPack packet);
/// <summary> /// <summary>
/// 向远程主机发送消息包并注册回调 /// 向远程主机发送消息包并注册回调

View File

@@ -1,4 +1,5 @@
using System.IO; using System.IO;
using TEngineProto;
namespace TEngine.Runtime namespace TEngine.Runtime
{ {
@@ -44,7 +45,7 @@ namespace TEngine.Runtime
/// <param name="packet">要序列化的消息包。</param> /// <param name="packet">要序列化的消息包。</param>
/// <param name="destination">要序列化的目标流。</param> /// <param name="destination">要序列化的目标流。</param>
/// <returns>是否序列化成功。</returns> /// <returns>是否序列化成功。</returns>
bool Serialize<T>(T packet, Stream destination) where T : Packet; bool Serialize(MainPack packet, Stream destination);
/// <summary> /// <summary>
/// 反序列化消息包头。 /// 反序列化消息包头。
@@ -61,6 +62,6 @@ namespace TEngine.Runtime
/// <param name="source">要反序列化的来源流。</param> /// <param name="source">要反序列化的来源流。</param>
/// <param name="customErrorData">用户自定义错误数据。</param> /// <param name="customErrorData">用户自定义错误数据。</param>
/// <returns>反序列化后的消息包。</returns> /// <returns>反序列化后的消息包。</returns>
Packet DeserializePacket(IPacketHeader packetHeader, Stream source, out object customErrorData); MainPack DeserializePacket(IPacketHeader packetHeader, Stream source, out object customErrorData);
} }
} }

View File

@@ -15,11 +15,11 @@ namespace TEngine.Runtime
/// </summary> /// </summary>
private abstract class NetworkChannelBase : INetworkChannel, IDisposable private abstract class NetworkChannelBase : INetworkChannel, IDisposable
{ {
private const float DefaultHeartBeatInterval = 30; private const float DefaultHeartBeatInterval = 3;
private const int MAX_MSG_HANDLE = 256; private const int MAX_MSG_HANDLE = 256;
private readonly string m_Name; private readonly string m_Name;
protected readonly Queue<Packet> m_SendPacketPool; protected readonly Queue<MainPack> m_SendPacketPool;
protected readonly INetworkChannelHelper m_NetworkChannelHelper; protected readonly INetworkChannelHelper m_NetworkChannelHelper;
protected AddressFamily m_AddressFamily; protected AddressFamily m_AddressFamily;
protected bool m_ResetHeartBeatElapseSecondsWhenReceivePacket; protected bool m_ResetHeartBeatElapseSecondsWhenReceivePacket;
@@ -61,7 +61,7 @@ namespace TEngine.Runtime
public NetworkChannelBase(string name, INetworkChannelHelper networkChannelHelper) public NetworkChannelBase(string name, INetworkChannelHelper networkChannelHelper)
{ {
m_Name = name ?? string.Empty; m_Name = name ?? string.Empty;
m_SendPacketPool = new Queue<Packet>(); m_SendPacketPool = new Queue<MainPack>();
m_NetworkChannelHelper = networkChannelHelper; m_NetworkChannelHelper = networkChannelHelper;
m_AddressFamily = AddressFamily.Unknown; m_AddressFamily = AddressFamily.Unknown;
m_ResetHeartBeatElapseSecondsWhenReceivePacket = false; m_ResetHeartBeatElapseSecondsWhenReceivePacket = false;
@@ -470,7 +470,7 @@ namespace TEngine.Runtime
/// </summary> /// </summary>
/// <typeparam name="T">消息包类型。</typeparam> /// <typeparam name="T">消息包类型。</typeparam>
/// <param name="packet">要发送的消息包。</param> /// <param name="packet">要发送的消息包。</param>
public bool Send<T>(T packet) where T : Packet public bool Send(MainPack packet)
{ {
if (m_Socket == null) if (m_Socket == null)
{ {
@@ -555,7 +555,7 @@ namespace TEngine.Runtime
while (m_SendPacketPool.Count > 0) while (m_SendPacketPool.Count > 0)
{ {
Packet packet = null; MainPack packet = null;
lock (m_SendPacketPool) lock (m_SendPacketPool)
{ {
packet = m_SendPacketPool.Dequeue(); packet = m_SendPacketPool.Dequeue();
@@ -658,7 +658,7 @@ namespace TEngine.Runtime
try try
{ {
object customErrorData = null; object customErrorData = null;
Packet packet = m_NetworkChannelHelper.DeserializePacket(m_ReceiveState.PacketHeader, m_ReceiveState.Stream, out customErrorData); MainPack packet = m_NetworkChannelHelper.DeserializePacket(m_ReceiveState.PacketHeader, m_ReceiveState.Stream, out customErrorData);
if (customErrorData != null && NetworkChannelCustomError != null) if (customErrorData != null && NetworkChannelCustomError != null)
{ {
@@ -667,7 +667,7 @@ namespace TEngine.Runtime
if (packet != null) if (packet != null)
{ {
HandleResponse(packet as MainPack); HandleResponse(packet);
} }
m_ReceiveState.PrepareForPacketHeader(m_NetworkChannelHelper.PacketHeaderLength); m_ReceiveState.PrepareForPacketHeader(m_NetworkChannelHelper.PacketHeaderLength);

View File

@@ -1,10 +0,0 @@
namespace TEngine.Runtime
{
/// <summary>
/// 网络消息包基类。
/// </summary>
public abstract class Packet:IMemory
{
public abstract void Clear();
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 4d54f03c4aed4afbab3b4fc45def97c5
timeCreated: 1661771739

View File

@@ -64,18 +64,15 @@ namespace TEngine.Runtime
/// <param name="packet">要序列化的消息包。</param> /// <param name="packet">要序列化的消息包。</param>
/// <param name="destination">要序列化的目标流。</param> /// <param name="destination">要序列化的目标流。</param>
/// <returns>是否序列化成功。</returns> /// <returns>是否序列化成功。</returns>
public bool Serialize<T>(T packet, Stream destination) where T : Packet public bool Serialize(MainPack packet, Stream destination)
{ {
m_CachedStream.SetLength(m_CachedStream.Capacity); // 此行防止 Array.Copy 的数据无法写入 m_CachedStream.SetLength(m_CachedStream.Capacity); // 此行防止 Array.Copy 的数据无法写入
m_CachedStream.Position = 0L; m_CachedStream.Position = 0L;
CSPacketHeader packetHeader = MemoryPool.Acquire<CSPacketHeader>(); CSPacketHeader packetHeader = MemoryPool.Acquire<CSPacketHeader>();
Serializer.Serialize(m_CachedStream, packetHeader); Serializer.Serialize(m_CachedStream, packetHeader);
MemoryPool.Release(packetHeader); MemoryPool.Release(packetHeader);
Serializer.SerializeWithLengthPrefix(m_CachedStream, packet, PrefixStyle.Fixed32); Serializer.SerializeWithLengthPrefix(m_CachedStream, packet, PrefixStyle.Fixed32);
MemoryPool.Release((IMemory)packet); MemoryPool.Release((IMemory)packet);
m_CachedStream.WriteTo(destination); m_CachedStream.WriteTo(destination);
return true; return true;
} }
@@ -87,7 +84,7 @@ namespace TEngine.Runtime
/// <param name="source">要反序列化的来源流。</param> /// <param name="source">要反序列化的来源流。</param>
/// <param name="customErrorData">用户自定义错误数据。</param> /// <param name="customErrorData">用户自定义错误数据。</param>
/// <returns>反序列化后的消息包。</returns> /// <returns>反序列化后的消息包。</returns>
public Packet DeserializePacket(IPacketHeader packetHeader, Stream source, out object customErrorData) public MainPack DeserializePacket(IPacketHeader packetHeader, Stream source, out object customErrorData)
{ {
// 注意:此函数并不在主线程调用! // 注意:此函数并不在主线程调用!
customErrorData = null; customErrorData = null;
@@ -99,13 +96,13 @@ namespace TEngine.Runtime
return null; return null;
} }
Packet packet = null; MainPack packet = null;
if (csPacketHeader.IsValid) if (csPacketHeader.IsValid)
{ {
Type packetType = typeof(MainPack); Type packetType = typeof(MainPack);
if (packetType != null) if (packetType != null)
{ {
packet = (Packet)RuntimeTypeModel.Default.DeserializeWithLengthPrefix(source, MemoryPool.Acquire(packetType), packetType, PrefixStyle.Fixed32, 0); packet = (MainPack)RuntimeTypeModel.Default.DeserializeWithLengthPrefix(source, MemoryPool.Acquire(packetType), packetType, PrefixStyle.Fixed32, 0);
} }
else else
{ {

View File

@@ -2,9 +2,9 @@
namespace TEngineProto namespace TEngineProto
{ {
public partial class MainPack:Packet public partial class MainPack:IMemory
{ {
public override void Clear() public void Clear()
{ {
requestcode = RequestCode.RequestNone; requestcode = RequestCode.RequestNone;
actioncode = ActionCode.ActionNone; actioncode = ActionCode.ActionNone;

View File

@@ -1,3 +1,4 @@
using System;
using ProtoBuf; using ProtoBuf;
using TEngine.Runtime; using TEngine.Runtime;
using System.Collections.Generic; using System.Collections.Generic;
@@ -56,7 +57,7 @@ namespace TEngineProto
} }
[global::ProtoBuf.ProtoContract()] [Serializable,global::ProtoBuf.ProtoContract(Name = @"MainPack")]
public partial class MainPack public partial class MainPack
{ {
[global::ProtoBuf.ProtoMember(1)] [global::ProtoBuf.ProtoMember(1)]
@@ -82,10 +83,11 @@ namespace TEngineProto
public PlayerPack playerpack { get; set; } public PlayerPack playerpack { get; set; }
[global::ProtoBuf.ProtoMember(8)] [global::ProtoBuf.ProtoMember(8)]
public long HeatEchoTime { get; set; } public float HeatEchoTime { get; set; }
} }
[global::ProtoBuf.ProtoContract()] [Serializable,global::ProtoBuf.ProtoContract(Name = @"LoginPack")]
public partial class LoginPack public partial class LoginPack
{ {
[global::ProtoBuf.ProtoMember(1)] [global::ProtoBuf.ProtoMember(1)]
@@ -98,7 +100,7 @@ namespace TEngineProto
} }
[global::ProtoBuf.ProtoContract()] [Serializable,global::ProtoBuf.ProtoContract(Name = @"RoomPack")]
public partial class RoomPack public partial class RoomPack
{ {
[global::ProtoBuf.ProtoMember(1)] [global::ProtoBuf.ProtoMember(1)]
@@ -111,28 +113,15 @@ namespace TEngineProto
[global::ProtoBuf.ProtoMember(3)] [global::ProtoBuf.ProtoMember(3)]
public int curnum { get; set; } public int curnum { get; set; }
[global::ProtoBuf.ProtoMember(4)]
public int state { get; set; }
[global::ProtoBuf.ProtoMember(6)] [global::ProtoBuf.ProtoMember(6)]
public int roomID { get; set; } public int roomID { get; set; }
[global::ProtoBuf.ProtoMember(9)]
public int visable { get; set; }
[global::ProtoBuf.ProtoMember(10)]
public int usePassword { get; set; }
[global::ProtoBuf.ProtoMember(11)]
[global::System.ComponentModel.DefaultValue("")]
public string password { get; set; }
[global::ProtoBuf.ProtoMember(12)] [global::ProtoBuf.ProtoMember(12)]
public List<PlayerPack> playerpack = new List<PlayerPack>(); public List<PlayerPack> playerpack = new List<PlayerPack>();
} }
[global::ProtoBuf.ProtoContract()] [Serializable,global::ProtoBuf.ProtoContract(Name = @"PlayerPack")]
public partial class PlayerPack public partial class PlayerPack
{ {
[global::ProtoBuf.ProtoMember(1)] [global::ProtoBuf.ProtoMember(1)]
@@ -151,7 +140,7 @@ namespace TEngineProto
} }
[global::ProtoBuf.ProtoContract()] [Serializable,global::ProtoBuf.ProtoContract(Name = @"PosPack")]
public partial class PosPack public partial class PosPack
{ {
[global::ProtoBuf.ProtoMember(1)] [global::ProtoBuf.ProtoMember(1)]

View File

@@ -1,3 +1,4 @@
using System;
using ProtoBuf; using ProtoBuf;
using TEngine.Runtime; using TEngine.Runtime;
using System.Collections.Generic; using System.Collections.Generic;
@@ -56,7 +57,7 @@ namespace TEngineProto
} }
[global::ProtoBuf.ProtoContract()] [Serializable,global::ProtoBuf.ProtoContract(Name = @"MainPack")]
public partial class MainPack public partial class MainPack
{ {
[global::ProtoBuf.ProtoMember(1)] [global::ProtoBuf.ProtoMember(1)]
@@ -82,11 +83,11 @@ namespace TEngineProto
public PlayerPack playerpack { get; set; } public PlayerPack playerpack { get; set; }
[global::ProtoBuf.ProtoMember(8)] [global::ProtoBuf.ProtoMember(8)]
public long HeatEchoTime { get; set; } public float HeatEchoTime { get; set; }
} }
[global::ProtoBuf.ProtoContract()] [Serializable,global::ProtoBuf.ProtoContract(Name = @"LoginPack")]
public partial class LoginPack public partial class LoginPack
{ {
[global::ProtoBuf.ProtoMember(1)] [global::ProtoBuf.ProtoMember(1)]
@@ -99,7 +100,7 @@ namespace TEngineProto
} }
[global::ProtoBuf.ProtoContract()] [Serializable,global::ProtoBuf.ProtoContract(Name = @"RoomPack")]
public partial class RoomPack public partial class RoomPack
{ {
[global::ProtoBuf.ProtoMember(1)] [global::ProtoBuf.ProtoMember(1)]
@@ -120,7 +121,7 @@ namespace TEngineProto
} }
[global::ProtoBuf.ProtoContract()] [Serializable,global::ProtoBuf.ProtoContract(Name = @"PlayerPack")]
public partial class PlayerPack public partial class PlayerPack
{ {
[global::ProtoBuf.ProtoMember(1)] [global::ProtoBuf.ProtoMember(1)]
@@ -139,7 +140,7 @@ namespace TEngineProto
} }
[global::ProtoBuf.ProtoContract()] [Serializable,global::ProtoBuf.ProtoContract(Name = @"PosPack")]
public partial class PosPack public partial class PosPack
{ {
[global::ProtoBuf.ProtoMember(1)] [global::ProtoBuf.ProtoMember(1)]

View File

@@ -0,0 +1,27 @@
// This file was generated by a tool; you should avoid making direct changes.
// Consider using 'partial classes' to extend these types
// Input: msg.proto
#pragma warning disable CS1591, CS0612, CS3021, IDE1006
namespace Msg
{
[global::ProtoBuf.ProtoContract()]
public partial class Msg : global::ProtoBuf.IExtensible
{
private global::ProtoBuf.IExtension __pbn__extensionData;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
=> global::ProtoBuf.Extensible.GetExtensionObject(ref __pbn__extensionData, createIfMissing);
[global::ProtoBuf.ProtoMember(1)]
[global::System.ComponentModel.DefaultValue("")]
public string msgId { get; set; } = "";
[global::ProtoBuf.ProtoMember(2)]
public byte[] msgContent { get; set; }
}
}
#pragma warning restore CS1591, CS0612, CS3021, IDE1006