Created
June 14, 2017 12:47
-
-
Save jagt/33c31669553879630a802e9637ea9422 to your computer and use it in GitHub Desktop.
Example usage of MessagePackBinary from MessagePack-CSharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using MessagePack; | |
using NUnit.Framework; | |
public class MessagePackBasics | |
{ | |
[Test] | |
public void Basic() | |
{ | |
int offset = 0; | |
int readSize = 0; | |
byte[] pack = new byte[256]; | |
string str = "these, are my twisted words"; | |
// write | |
offset += MessagePackBinary.WriteString(ref pack, offset, str); | |
offset += MessagePackBinary.WriteInt32(ref pack, offset, 253); | |
Console.WriteLine("offset is: " + offset); | |
// read | |
offset = 0; | |
Assert.AreEqual(str, MessagePackBinary.ReadString(pack, offset, out readSize)); | |
offset += readSize; | |
Assert.AreEqual(253, MessagePackBinary.ReadInt32(pack, offset, out readSize)); | |
return; | |
} | |
[Test] | |
public void Array() | |
{ | |
int offset = 0; | |
int readSize = 0; | |
byte[] pack = new byte[256]; | |
// write | |
offset += MessagePackBinary.WriteArrayHeader(ref pack, offset, 5); | |
offset += MessagePackBinary.WriteInt32(ref pack, offset, 1); | |
offset += MessagePackBinary.WriteInt32(ref pack, offset, 2); | |
offset += MessagePackBinary.WriteInt32(ref pack, offset, 3); | |
offset += MessagePackBinary.WriteInt32(ref pack, offset, 4); | |
offset += MessagePackBinary.WriteInt32(ref pack, offset, 5); | |
Console.WriteLine("offset is: " + offset); | |
// read | |
offset = 0; | |
Assert.AreEqual(5, MessagePackBinary.ReadArrayHeader(pack, offset, out readSize)); | |
offset += readSize; | |
for (int ix = 0; ix < 5; ix++) | |
{ | |
Assert.AreEqual(ix + 1, MessagePackBinary.ReadInt32(pack, offset, out readSize)); | |
offset += readSize; | |
} | |
return; | |
} | |
[Test] | |
public void Map() | |
{ | |
int offset = 0; | |
int readSize = 0; | |
byte[] pack = new byte[256]; | |
var dic = new Dictionary<int, float>() | |
{ | |
{ 1, 1.0f }, | |
{ 2, 2.0f }, | |
{ 3, 3.0f }, | |
{ 4, 4.0f }, | |
{ 5, 5.0f }, | |
}; | |
// write | |
offset += MessagePackBinary.WriteMapHeader(ref pack, offset, dic.Count); | |
foreach (var tuple in dic) | |
{ | |
offset += MessagePackBinary.WriteInt32(ref pack, offset, tuple.Key); | |
offset += MessagePackBinary.WriteSingle(ref pack, offset, tuple.Value); | |
} | |
Console.WriteLine("offset is: " + offset); | |
// read | |
offset = 0; | |
Assert.AreEqual(5, MessagePackBinary.ReadMapHeader(pack, offset, out readSize)); | |
offset += readSize; | |
for (int ix = 0; ix < 5; ix++) | |
{ | |
Assert.AreEqual(ix + 1, MessagePackBinary.ReadInt32(pack, offset, out readSize)); | |
offset += readSize; | |
Assert.AreEqual((float)(ix + 1), MessagePackBinary.ReadSingle(pack, offset, out readSize)); | |
offset += readSize; | |
} | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment