forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessagePacker.cs
More file actions
43 lines (36 loc) · 1.44 KB
/
MessagePacker.cs
File metadata and controls
43 lines (36 loc) · 1.44 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
using MLAPI.Logging;
using MLAPI.Serialization;
using MLAPI.Configuration;
using MLAPI.Serialization.Pooled;
namespace MLAPI.Internal
{
internal static class MessagePacker
{
// This method is responsible for unwrapping a message, that is extracting the messagebody.
internal static NetworkBuffer UnwrapMessage(NetworkBuffer inputBuffer, out byte messageType)
{
using (var inputHeaderReader = PooledNetworkReader.Get(inputBuffer))
{
if (inputBuffer.Length < 1)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) NetworkLog.LogError("The incoming message was too small");
messageType = NetworkConstants.INVALID;
return null;
}
messageType = inputHeaderReader.ReadByteDirect();
// The input stream is now ready to be read from. It's "safe" and has the correct position
return inputBuffer;
}
}
internal static NetworkBuffer WrapMessage(byte messageType, NetworkBuffer messageBody)
{
var outStream = PooledNetworkBuffer.Get();
using (var outWriter = PooledNetworkWriter.Get(outStream))
{
outWriter.WriteByte(messageType);
outStream.Write(messageBody.GetBuffer(), 0, (int)messageBody.Length);
}
return outStream;
}
}
}