mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
Update ProtobufUtility.cs
This commit is contained in:
@@ -2,8 +2,10 @@
|
|||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using GameProto;
|
using GameProto;
|
||||||
using Google.Protobuf;
|
using Google.Protobuf;
|
||||||
|
using TEngine;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -22,7 +24,7 @@ public partial class ProtobufUtility
|
|||||||
{
|
{
|
||||||
((IMessage)message).WriteTo(stream);
|
((IMessage)message).WriteTo(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 消息压入内存流。
|
/// 消息压入内存流。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -32,7 +34,7 @@ public partial class ProtobufUtility
|
|||||||
{
|
{
|
||||||
((IMessage)message).WriteTo(stream);
|
((IMessage)message).WriteTo(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 消息压入内存流。
|
/// 消息压入内存流。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -135,7 +137,7 @@ public partial class ProtobufUtility
|
|||||||
{
|
{
|
||||||
return ((IMessage)message).ToByteArray();
|
return ((IMessage)message).ToByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 序列化protobuf
|
/// 序列化protobuf
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -151,11 +153,26 @@ public partial class ProtobufUtility
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <param name="dataBytes"></param>
|
/// <param name="dataBytes"></param>
|
||||||
|
/// <param name="offset"></param>
|
||||||
|
/// <param name="bodyCount"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static T Deserialize<T>(byte[] dataBytes) where T : IMessage, new()
|
public static T Deserialize<T>(byte[] dataBytes, int offset, int bodyCount) where T : IMessage, new()
|
||||||
{
|
{
|
||||||
T msg = new T();
|
T msg = new T();
|
||||||
msg = (T)msg.Descriptor.Parser.ParseFrom(dataBytes);
|
msg = (T)msg.Descriptor.Parser.ParseFrom(dataBytes, offset, bodyCount);
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 反序列化protobuf
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataBytes"></param>
|
||||||
|
/// <param name="offset"></param>
|
||||||
|
/// <param name="bodyCount"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static CSPkg Deserialize(byte[] dataBytes, int offset, int bodyCount)
|
||||||
|
{
|
||||||
|
var msg = (CSPkg)CSPkg.Descriptor.Parser.ParseFrom(dataBytes, offset, bodyCount);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,4 +185,33 @@ public partial class ProtobufUtility
|
|||||||
{
|
{
|
||||||
return cmdMerge & 65535;
|
return cmdMerge & 65535;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static readonly StringBuilder StringBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
public static string GetBuffer(byte[] buffer)
|
||||||
|
{
|
||||||
|
StringBuilder.Length = 0;
|
||||||
|
StringBuilder.Append("[");
|
||||||
|
for (int i = 0; i < buffer.Length; i++)
|
||||||
|
{
|
||||||
|
if (i == buffer.Length - 1)
|
||||||
|
{
|
||||||
|
StringBuilder.Append(buffer[i]);
|
||||||
|
StringBuilder.Append("]");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
StringBuilder.Append(buffer[i]);
|
||||||
|
StringBuilder.Append(",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return StringBuilder.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void PrintBuffer(byte[] buffer)
|
||||||
|
{
|
||||||
|
Log.Debug(GetBuffer(buffer));
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user