mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
Packet & ProtoTools
Packet & ProtoTools
This commit is contained in:
@@ -57,6 +57,7 @@ namespace TEngine.Editor
|
||||
string s = File.ReadAllText(proto);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("using System;\n");
|
||||
sb.Append("using ProtoBuf;\n");
|
||||
sb.Append("using TEngine.Runtime;\n");
|
||||
sb.Append("using System.Collections.Generic;\n");
|
||||
@@ -100,7 +101,8 @@ namespace TEngine.Editor
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
sb.Append($"\tpublic partial class {msgName}: IMemory");
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using TEngineProto;
|
||||
|
||||
namespace TEngine.Runtime
|
||||
{
|
||||
@@ -147,7 +148,7 @@ namespace TEngine.Runtime
|
||||
/// </summary>
|
||||
/// <typeparam name="T">消息包类型。</typeparam>
|
||||
/// <param name="packet">要发送的消息包。</param>
|
||||
bool Send<T>(T packet) where T : Packet;
|
||||
bool Send(MainPack packet);
|
||||
|
||||
/// <summary>
|
||||
/// 向远程主机发送消息包并注册回调
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using System.IO;
|
||||
using TEngineProto;
|
||||
|
||||
namespace TEngine.Runtime
|
||||
{
|
||||
@@ -44,7 +45,7 @@ namespace TEngine.Runtime
|
||||
/// <param name="packet">要序列化的消息包。</param>
|
||||
/// <param name="destination">要序列化的目标流。</param>
|
||||
/// <returns>是否序列化成功。</returns>
|
||||
bool Serialize<T>(T packet, Stream destination) where T : Packet;
|
||||
bool Serialize(MainPack packet, Stream destination);
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化消息包头。
|
||||
@@ -61,6 +62,6 @@ namespace TEngine.Runtime
|
||||
/// <param name="source">要反序列化的来源流。</param>
|
||||
/// <param name="customErrorData">用户自定义错误数据。</param>
|
||||
/// <returns>反序列化后的消息包。</returns>
|
||||
Packet DeserializePacket(IPacketHeader packetHeader, Stream source, out object customErrorData);
|
||||
MainPack DeserializePacket(IPacketHeader packetHeader, Stream source, out object customErrorData);
|
||||
}
|
||||
}
|
@@ -15,11 +15,11 @@ namespace TEngine.Runtime
|
||||
/// </summary>
|
||||
private abstract class NetworkChannelBase : INetworkChannel, IDisposable
|
||||
{
|
||||
private const float DefaultHeartBeatInterval = 30;
|
||||
private const float DefaultHeartBeatInterval = 3;
|
||||
private const int MAX_MSG_HANDLE = 256;
|
||||
|
||||
private readonly string m_Name;
|
||||
protected readonly Queue<Packet> m_SendPacketPool;
|
||||
protected readonly Queue<MainPack> m_SendPacketPool;
|
||||
protected readonly INetworkChannelHelper m_NetworkChannelHelper;
|
||||
protected AddressFamily m_AddressFamily;
|
||||
protected bool m_ResetHeartBeatElapseSecondsWhenReceivePacket;
|
||||
@@ -61,7 +61,7 @@ namespace TEngine.Runtime
|
||||
public NetworkChannelBase(string name, INetworkChannelHelper networkChannelHelper)
|
||||
{
|
||||
m_Name = name ?? string.Empty;
|
||||
m_SendPacketPool = new Queue<Packet>();
|
||||
m_SendPacketPool = new Queue<MainPack>();
|
||||
m_NetworkChannelHelper = networkChannelHelper;
|
||||
m_AddressFamily = AddressFamily.Unknown;
|
||||
m_ResetHeartBeatElapseSecondsWhenReceivePacket = false;
|
||||
@@ -470,7 +470,7 @@ namespace TEngine.Runtime
|
||||
/// </summary>
|
||||
/// <typeparam name="T">消息包类型。</typeparam>
|
||||
/// <param name="packet">要发送的消息包。</param>
|
||||
public bool Send<T>(T packet) where T : Packet
|
||||
public bool Send(MainPack packet)
|
||||
{
|
||||
if (m_Socket == null)
|
||||
{
|
||||
@@ -555,7 +555,7 @@ namespace TEngine.Runtime
|
||||
|
||||
while (m_SendPacketPool.Count > 0)
|
||||
{
|
||||
Packet packet = null;
|
||||
MainPack packet = null;
|
||||
lock (m_SendPacketPool)
|
||||
{
|
||||
packet = m_SendPacketPool.Dequeue();
|
||||
@@ -658,7 +658,7 @@ namespace TEngine.Runtime
|
||||
try
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -667,7 +667,7 @@ namespace TEngine.Runtime
|
||||
|
||||
if (packet != null)
|
||||
{
|
||||
HandleResponse(packet as MainPack);
|
||||
HandleResponse(packet);
|
||||
}
|
||||
|
||||
m_ReceiveState.PrepareForPacketHeader(m_NetworkChannelHelper.PacketHeaderLength);
|
||||
|
@@ -1,10 +0,0 @@
|
||||
namespace TEngine.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 网络消息包基类。
|
||||
/// </summary>
|
||||
public abstract class Packet:IMemory
|
||||
{
|
||||
public abstract void Clear();
|
||||
}
|
||||
}
|
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d54f03c4aed4afbab3b4fc45def97c5
|
||||
timeCreated: 1661771739
|
@@ -64,18 +64,15 @@ namespace TEngine.Runtime
|
||||
/// <param name="packet">要序列化的消息包。</param>
|
||||
/// <param name="destination">要序列化的目标流。</param>
|
||||
/// <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.Position = 0L;
|
||||
|
||||
CSPacketHeader packetHeader = MemoryPool.Acquire<CSPacketHeader>();
|
||||
Serializer.Serialize(m_CachedStream, packetHeader);
|
||||
MemoryPool.Release(packetHeader);
|
||||
|
||||
Serializer.SerializeWithLengthPrefix(m_CachedStream, packet, PrefixStyle.Fixed32);
|
||||
MemoryPool.Release((IMemory)packet);
|
||||
|
||||
m_CachedStream.WriteTo(destination);
|
||||
return true;
|
||||
}
|
||||
@@ -87,7 +84,7 @@ namespace TEngine.Runtime
|
||||
/// <param name="source">要反序列化的来源流。</param>
|
||||
/// <param name="customErrorData">用户自定义错误数据。</param>
|
||||
/// <returns>反序列化后的消息包。</returns>
|
||||
public Packet DeserializePacket(IPacketHeader packetHeader, Stream source, out object customErrorData)
|
||||
public MainPack DeserializePacket(IPacketHeader packetHeader, Stream source, out object customErrorData)
|
||||
{
|
||||
// 注意:此函数并不在主线程调用!
|
||||
customErrorData = null;
|
||||
@@ -99,13 +96,13 @@ namespace TEngine.Runtime
|
||||
return null;
|
||||
}
|
||||
|
||||
Packet packet = null;
|
||||
MainPack packet = null;
|
||||
if (csPacketHeader.IsValid)
|
||||
{
|
||||
Type packetType = typeof(MainPack);
|
||||
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
|
||||
{
|
||||
|
@@ -2,9 +2,9 @@
|
||||
|
||||
namespace TEngineProto
|
||||
{
|
||||
public partial class MainPack:Packet
|
||||
public partial class MainPack:IMemory
|
||||
{
|
||||
public override void Clear()
|
||||
public void Clear()
|
||||
{
|
||||
requestcode = RequestCode.RequestNone;
|
||||
actioncode = ActionCode.ActionNone;
|
||||
|
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using ProtoBuf;
|
||||
using TEngine.Runtime;
|
||||
using System.Collections.Generic;
|
||||
@@ -56,7 +57,7 @@ namespace TEngineProto
|
||||
|
||||
}
|
||||
|
||||
[global::ProtoBuf.ProtoContract()]
|
||||
[Serializable,global::ProtoBuf.ProtoContract(Name = @"MainPack")]
|
||||
public partial class MainPack
|
||||
{
|
||||
[global::ProtoBuf.ProtoMember(1)]
|
||||
@@ -82,10 +83,11 @@ namespace TEngineProto
|
||||
public PlayerPack playerpack { get; set; }
|
||||
|
||||
[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
|
||||
{
|
||||
[global::ProtoBuf.ProtoMember(1)]
|
||||
@@ -98,7 +100,7 @@ namespace TEngineProto
|
||||
|
||||
}
|
||||
|
||||
[global::ProtoBuf.ProtoContract()]
|
||||
[Serializable,global::ProtoBuf.ProtoContract(Name = @"RoomPack")]
|
||||
public partial class RoomPack
|
||||
{
|
||||
[global::ProtoBuf.ProtoMember(1)]
|
||||
@@ -111,28 +113,15 @@ namespace TEngineProto
|
||||
[global::ProtoBuf.ProtoMember(3)]
|
||||
public int curnum { get; set; }
|
||||
|
||||
[global::ProtoBuf.ProtoMember(4)]
|
||||
public int state { get; set; }
|
||||
|
||||
[global::ProtoBuf.ProtoMember(6)]
|
||||
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)]
|
||||
public List<PlayerPack> playerpack = new List<PlayerPack>();
|
||||
|
||||
}
|
||||
|
||||
[global::ProtoBuf.ProtoContract()]
|
||||
[Serializable,global::ProtoBuf.ProtoContract(Name = @"PlayerPack")]
|
||||
public partial class PlayerPack
|
||||
{
|
||||
[global::ProtoBuf.ProtoMember(1)]
|
||||
@@ -151,7 +140,7 @@ namespace TEngineProto
|
||||
|
||||
}
|
||||
|
||||
[global::ProtoBuf.ProtoContract()]
|
||||
[Serializable,global::ProtoBuf.ProtoContract(Name = @"PosPack")]
|
||||
public partial class PosPack
|
||||
{
|
||||
[global::ProtoBuf.ProtoMember(1)]
|
||||
|
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using ProtoBuf;
|
||||
using TEngine.Runtime;
|
||||
using System.Collections.Generic;
|
||||
@@ -56,7 +57,7 @@ namespace TEngineProto
|
||||
|
||||
}
|
||||
|
||||
[global::ProtoBuf.ProtoContract()]
|
||||
[Serializable,global::ProtoBuf.ProtoContract(Name = @"MainPack")]
|
||||
public partial class MainPack
|
||||
{
|
||||
[global::ProtoBuf.ProtoMember(1)]
|
||||
@@ -82,11 +83,11 @@ namespace TEngineProto
|
||||
public PlayerPack playerpack { get; set; }
|
||||
|
||||
[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
|
||||
{
|
||||
[global::ProtoBuf.ProtoMember(1)]
|
||||
@@ -99,7 +100,7 @@ namespace TEngineProto
|
||||
|
||||
}
|
||||
|
||||
[global::ProtoBuf.ProtoContract()]
|
||||
[Serializable,global::ProtoBuf.ProtoContract(Name = @"RoomPack")]
|
||||
public partial class RoomPack
|
||||
{
|
||||
[global::ProtoBuf.ProtoMember(1)]
|
||||
@@ -120,7 +121,7 @@ namespace TEngineProto
|
||||
|
||||
}
|
||||
|
||||
[global::ProtoBuf.ProtoContract()]
|
||||
[Serializable,global::ProtoBuf.ProtoContract(Name = @"PlayerPack")]
|
||||
public partial class PlayerPack
|
||||
{
|
||||
[global::ProtoBuf.ProtoMember(1)]
|
||||
@@ -139,7 +140,7 @@ namespace TEngineProto
|
||||
|
||||
}
|
||||
|
||||
[global::ProtoBuf.ProtoContract()]
|
||||
[Serializable,global::ProtoBuf.ProtoContract(Name = @"PosPack")]
|
||||
public partial class PosPack
|
||||
{
|
||||
[global::ProtoBuf.ProtoMember(1)]
|
||||
|
27
Assets/TEngine/Tools~/Protobuf/Proto_CSharp/msg.cs
Normal file
27
Assets/TEngine/Tools~/Protobuf/Proto_CSharp/msg.cs
Normal 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
|
Reference in New Issue
Block a user