File tree Expand file tree Collapse file tree 5 files changed +62
-17
lines changed Expand file tree Collapse file tree 5 files changed +62
-17
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . Diagnostics ;
4
- using System . Runtime . InteropServices ;
5
- using System . Threading . Tasks ;
6
3
using SystemModule ;
7
4
using SystemModule . Packages ;
8
- using SystemModule . ProtobuffPacket ;
9
5
10
6
namespace GameGate
11
7
{
Original file line number Diff line number Diff line change 1
1
using System ;
2
2
using SystemModule ;
3
3
using SystemModule . Packages ;
4
- using SystemModule . ProtobuffPacket ;
5
4
using SystemModule . Sockets ;
6
5
7
6
namespace GameGate
@@ -408,16 +407,6 @@ public void SendBuffer(byte[] sendBuffer)
408
407
ClientSocket . SendBuff ( sendBuffer ) ;
409
408
}
410
409
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
-
421
410
public void CheckServerIsTimeOut ( )
422
411
{
423
412
if ( ( HUtil32 . GetTickCount ( ) - _checkServerTick ) > GateShare . dwCheckServerTimeOutTime && CheckServerFailCount <= 20 )
Original file line number Diff line number Diff line change 3
3
using System . Threading ;
4
4
using System . Threading . Tasks ;
5
5
using SystemModule ;
6
- using SystemModule . Packages ;
7
- using SystemModule . ProtobuffPacket ;
8
6
9
7
namespace GameGate
10
8
{
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 20
20
<None Remove =" System.Data.SQLite.Core" />
21
21
</ItemGroup >
22
22
<ItemGroup >
23
+ <PackageReference Include =" protobuf-net" Version =" 3.0.101" />
23
24
<PackageReference Include =" System.Text.Encoding.CodePages" Version =" 6.0.0" />
24
25
</ItemGroup >
25
26
</Project >
You can’t perform that action at this time.
0 commit comments