Created
April 11, 2013 16:50
-
-
Save dkuppitz/5365081 to your computer and use it in GitHub Desktop.
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
namespace TitanTest | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Net.Sockets; | |
using MsgPack; | |
internal class Program | |
{ | |
private static void Main() | |
{ | |
var meta = new Dictionary<string, object> | |
{ | |
{ "channel", 2 }, | |
{ "graphName", "graph" } | |
}; | |
var messageArray = new object[] | |
{ | |
Guid.Empty.ToByteArray(), | |
Guid.NewGuid().ToByteArray(), | |
meta, | |
"groovy", | |
"g.V", //"g.V.count()", | |
new Dictionary<string, object>() | |
}; | |
var tcpClient = new TcpClient("192.168.2.105", 8184); | |
using (var stream = tcpClient.GetStream()) | |
{ | |
using (var packer = Packer.Create(stream)) | |
{ | |
byte[] messageBytes; | |
packer.Pack((byte) 0); | |
packer.Pack((byte) 3); | |
using (var messageStream = new MemoryStream()) | |
using (var messagePacker = Packer.Create(messageStream)) | |
{ | |
messagePacker.Pack(messageArray); | |
messageBytes = messageStream.ToArray(); | |
} | |
var lengthBytes = BitConverter.GetBytes(messageBytes.Length).Reverse().ToArray(); | |
stream.Write(lengthBytes, 0, lengthBytes.Length); | |
stream.Write(messageBytes, 0, messageBytes.Length); | |
using (var unpacker = Unpacker.Create(stream, false)) | |
{ | |
var maxWaitTime = TimeSpan.FromMilliseconds(100); | |
var sw = Stopwatch.StartNew(); | |
while (!stream.DataAvailable && sw.Elapsed <= maxWaitTime) | |
System.Threading.Thread.SpinWait(1); | |
while (stream.DataAvailable) | |
{ | |
MessagePackObject obj; | |
if (unpacker.ReadObject(out obj)) | |
{ | |
if (unpacker.IsArrayHeader) Console.Write("ARRAY HEADER :: "); | |
if (unpacker.IsMapHeader) Console.Write("MAP HEADER :: "); | |
Console.WriteLine(obj.ToString()); | |
} | |
} | |
} | |
Debugger.Break(); | |
} | |
} | |
} | |
} | |
} |
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
0 | |
5 | |
0 | |
0 | |
0 | |
120 | |
ARRAY HEADER :: 5 | |
0xE8012B0D20164641A53B8F58300B883B | |
MAP HEADER :: 0 | |
ARRAY HEADER :: 3 | |
MAP HEADER :: 2 | |
_id | |
4 | |
_type | |
vertex | |
MAP HEADER :: 2 | |
_id | |
8 | |
_type | |
vertex | |
MAP HEADER :: 3 | |
_id | |
12 | |
_type | |
vertex | |
_properties | |
MAP HEADER :: 1 | |
foo | |
bar | |
MAP HEADER :: 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment