forked from hiram3512/HiSocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMsgProtobuf.cs
49 lines (44 loc) · 1.21 KB
/
MsgProtobuf.cs
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
//****************************************************************************
// Description:
// Author: [email protected]
//***************************************************************************
using ProtoBuf;
using System.IO;
namespace HiSocket.Message
{
public class MsgProtobuf : MsgBase
{
public MsgProtobuf() : base()
{
}
public MsgProtobuf(IByteArray byteArray) : base(byteArray)
{
}
public void Write<T>(T t)
{
var bytes = Serialize(t);
ByteArray.Write(bytes);
}
public T Read<T>()
{
return Deserialize<T>(ByteArray.Read(ByteArray.Length));
}
private byte[] Serialize<T>(T t)
{
using (MemoryStream stream = new MemoryStream())
{
Serializer.Serialize<T>(stream, t);
return stream.ToArray();
}
}
private T Deserialize<T>(byte[] param)
{
using (MemoryStream stream = new MemoryStream(param))
{
T obj = default(T);
obj = Serializer.Deserialize<T>(stream);
return obj;
}
}
}
}