forked from egametang/ET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageSerializeHelper.cs
More file actions
105 lines (85 loc) · 3.3 KB
/
Copy pathMessageSerializeHelper.cs
File metadata and controls
105 lines (85 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.IO;
using MongoDB.Bson.IO;
namespace ET
{
public static class MessageSerializeHelper
{
public const ushort PbMaxOpcode = 40000;
public const ushort JsonMinOpcode = 51000;
public static object DeserializeFrom(ushort opcode, Type type, MemoryStream memoryStream)
{
if (opcode < PbMaxOpcode)
{
return ProtobufHelper.FromStream(type, memoryStream);
}
if (opcode >= JsonMinOpcode)
{
return JsonHelper.FromJson(type, memoryStream.GetBuffer().ToStr((int)memoryStream.Position, (int)(memoryStream.Length - memoryStream.Position)));
}
#if NOT_CLIENT
return MongoHelper.FromStream(type, memoryStream);
#else
throw new Exception($"client no message: {opcode}");
#endif
}
public static void SerializeTo(ushort opcode, object obj, MemoryStream memoryStream)
{
if (opcode < PbMaxOpcode)
{
ProtobufHelper.ToStream(obj, memoryStream);
return;
}
if (opcode >= JsonMinOpcode)
{
string s = JsonHelper.ToJson(obj);
byte[] bytes = s.ToUtf8();
memoryStream.Write(bytes, 0, bytes.Length);
return;
}
#if NOT_CLIENT
MongoHelper.ToStream(obj, memoryStream);
#else
throw new Exception($"client no message: {opcode}");
#endif
}
public static MemoryStream GetStream(int count = 0)
{
MemoryStream stream;
if (count > 0)
{
stream = new MemoryStream(count);
}
else
{
stream = new MemoryStream();
}
return stream;
}
public static (ushort, MemoryStream) MessageToStream(object message, int count = 0)
{
MemoryStream stream = GetStream(Packet.OpcodeLength + count);
ushort opcode = OpcodeTypeComponent.Instance.GetOpcode(message.GetType());
stream.Seek(Packet.OpcodeLength, SeekOrigin.Begin);
stream.SetLength(Packet.OpcodeLength);
stream.GetBuffer().WriteTo(0, opcode);
MessageSerializeHelper.SerializeTo(opcode, message, stream);
stream.Seek(0, SeekOrigin.Begin);
return (opcode, stream);
}
public static (ushort, MemoryStream) MessageToStream(long actorId, object message, int count = 0)
{
int actorSize = sizeof (long);
MemoryStream stream = GetStream(actorSize + Packet.OpcodeLength + count);
ushort opcode = OpcodeTypeComponent.Instance.GetOpcode(message.GetType());
stream.Seek(actorSize + Packet.OpcodeLength, SeekOrigin.Begin);
stream.SetLength(actorSize + Packet.OpcodeLength);
// 写入actorId
stream.GetBuffer().WriteTo(0, actorId);
stream.GetBuffer().WriteTo(actorSize, opcode);
MessageSerializeHelper.SerializeTo(opcode, message, stream);
stream.Seek(0, SeekOrigin.Begin);
return (opcode, stream);
}
}
}