-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleProtoStream.cs
More file actions
96 lines (83 loc) · 2.49 KB
/
Copy pathExampleProtoStream.cs
File metadata and controls
96 lines (83 loc) · 2.49 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
using ProtoBuf;
using System;
using System.Buffers;
using System.IO;
namespace EnetWrappers
{
public class ExampleProtoStream : IEnetProtoStream, IDisposable
{
public const int Size = 1024 * 1024 * 2;
private MemoryStream _stream;
private byte[] _buffer;
public int Length => _buffer.Length;
public Stream Stream => _stream;
public byte[] Buffer => _buffer;
public ReadOnlySequence<byte> Sequence
{
get
{
return new ReadOnlySequence<byte>(_buffer);
}
}
public Memory<byte> Memory
{
get
{
return new Memory<byte>(_buffer);
}
}
public Span<byte> Span
{
get
{
return new Span<byte>(_buffer);
}
}
public ExampleProtoStream(int bufferSize)
{
_buffer = new byte[bufferSize];
_stream = new MemoryStream(_buffer, true);
}
public ExampleProtoStream(byte[] buffer)
{
_buffer = buffer;
_stream = new MemoryStream(_buffer, true);
}
public void Dispose()
{
}
public int Serialize(object message, int offset = 0)
{
_stream.Position = offset;
Serializer.Serialize(_stream, message);
return (int)_stream.Position;
}
public int SerializeWithLengthPrefix<T>(T message)
{
_stream.Position = 0;
Serializer.SerializeWithLengthPrefix(_stream, message, PrefixStyle.Base128);
return (int)_stream.Position;
}
public T Deserialize<T>(byte[] data, int length, int offset = 0)
{
_stream.SetLength(0);
_stream.Write(data, offset, length);
_stream.Position = 0;
return Serializer.Deserialize<T>(_stream);
}
public T Deserialize<T>(T value, byte[] data, int length, int offset = 0)
{
_stream.SetLength(0);
_stream.Write(data, offset, length);
_stream.Position = 0;
return Serializer.Deserialize<T>(_stream);
}
public object Deserialize(Type type, byte[] data, int length, int offset = 0)
{
_stream.SetLength(0);
_stream.Write(data, offset, length);
_stream.Position = 0;
return Serializer.Deserialize(type, _stream);
}
}
}