Skip to content

Commit 06c3381

Browse files
committed
GameGate:ProtoBuf测试
1 parent 621bdc6 commit 06c3381

File tree

5 files changed

+62
-17
lines changed

5 files changed

+62
-17
lines changed

src/GameGate/ClientSession.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.Runtime.InteropServices;
5-
using System.Threading.Tasks;
63
using SystemModule;
74
using SystemModule.Packages;
8-
using SystemModule.ProtobuffPacket;
95

106
namespace GameGate
117
{

src/GameGate/Services/ClientThread.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using SystemModule;
33
using SystemModule.Packages;
4-
using SystemModule.ProtobuffPacket;
54
using SystemModule.Sockets;
65

76
namespace GameGate
@@ -408,16 +407,6 @@ public void SendBuffer(byte[] sendBuffer)
408407
ClientSocket.SendBuff(sendBuffer);
409408
}
410409

411-
public void SendBuffer(IProtoBuff protoBuff)
412-
{
413-
if (!ClientSocket.IsConnected) {
414-
return;
415-
}
416-
var bodyBuffer = ProtobuffHelp.Serialize(protoBuff);
417-
SendBytes += bodyBuffer.Length;
418-
ClientSocket.SendBuff(bodyBuffer);
419-
}
420-
421410
public void CheckServerIsTimeOut()
422411
{
423412
if ((HUtil32.GetTickCount() - _checkServerTick) > GateShare.dwCheckServerTimeOutTime && CheckServerFailCount <= 20)

src/GameGate/TimedService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using System.Threading;
44
using System.Threading.Tasks;
55
using SystemModule;
6-
using SystemModule.Packages;
7-
using SystemModule.ProtobuffPacket;
86

97
namespace GameGate
108
{
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace SystemModule.ProtobuffPacket
5+
{
6+
public class ProtobuffHelp
7+
{
8+
/// <summary>
9+
/// 将消息序列化为二进制的方法
10+
/// </summary>
11+
/// <typeparam name="T"></typeparam>
12+
/// <param name="model"></param>
13+
/// <returns></returns>
14+
public static byte[] Serialize<T>(T model)
15+
{
16+
try
17+
{
18+
//涉及格式转换,需要用到流,将二进制序列化到流中
19+
using MemoryStream ms = new MemoryStream();
20+
//使用ProtoBuf工具的序列化方法
21+
ProtoBuf.Serializer.Serialize<T>(ms, model);
22+
//定义二级制数组,保存序列化后的结果
23+
byte[] result = new byte[ms.Length];
24+
//将流的位置设为0,起始点
25+
ms.Position = 0;
26+
//将流中的内容读取到二进制数组中
27+
ms.Read(result, 0, result.Length);
28+
return result;
29+
}
30+
catch (Exception ex)
31+
{
32+
return null;
33+
}
34+
}
35+
36+
/// <summary>
37+
/// 将收到的消息反序列化成对象
38+
/// </summary>
39+
/// <typeparam name="T"></typeparam>
40+
/// <param name="msg"></param>
41+
/// <returns></returns>
42+
public static T DeSerialize<T>(byte[] msg)
43+
{
44+
try
45+
{
46+
using MemoryStream ms = new MemoryStream();
47+
//将消息写入流中
48+
ms.Write(msg, 0, msg.Length);
49+
//将流的位置归0
50+
ms.Position = 0;
51+
//使用工具反序列化对象
52+
T result = ProtoBuf.Serializer.Deserialize<T>(ms);
53+
return result;
54+
}
55+
catch (Exception ex)
56+
{
57+
return default(T);
58+
}
59+
}
60+
}
61+
}

src/SystemModule/SystemModule.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<None Remove="System.Data.SQLite.Core" />
2121
</ItemGroup>
2222
<ItemGroup>
23+
<PackageReference Include="protobuf-net" Version="3.0.101" />
2324
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
2425
</ItemGroup>
2526
</Project>

0 commit comments

Comments
 (0)