public static string Request(string input) { // prepare Socket var nanoSock = new NanomsgSocket(Domain.SP, Protocol.REQ); string result = null; bool messageReceived = false; // prepare Listener var listener = new NanomsgListener(); listener.ReceivedMessage += (sockedId) => { using (NanomsgReadStream inStream = nanoSock.ReceiveStream()) using (var unpacker = Unpacker.Create(inStream)) { string output; if (!unpacker.ReadString(out output)) { throw new Exception("REP invalid"); } result = output; } messageReceived = true; }; // Connect and Send Request listener.AddSocket(nanoSock); nanoSock.Connect("tcp://127.0.0.1:" + reqPort); using (NanomsgWriteStream outStream = nanoSock.CreateSendStream()) using (var packer = Packer.Create(outStream)) { packer.PackString(input); nanoSock.SendStream(outStream); } var start = DateTime.Now; while (!messageReceived && start + TimeSpan.FromSeconds(30) > DateTime.Now) { listener.Listen(TimeSpan.FromMilliseconds(250)); } listener.RemoveSocket(nanoSock); nanoSock.Dispose(); if (!messageReceived) { throw new Exception("REQ timed out"); } return(result); }
public static void RunWorker(CancellationTokenSource cancellor, int workerId) { var serverThread = new Thread(() => { var nanoSock = new NanomsgSocket(Domain.SP, Protocol.REP); var nanoListener = new NanomsgListener(); nanoListener.ReceivedMessage += (socketId) => { string input; using (NanomsgReadStream inStream = nanoSock.ReceiveStream()) using (var unpacker = Unpacker.Create(inStream)) { if (!unpacker.ReadString(out input)) { throw new Exception("REQ invalid"); } Console.WriteLine(input); } using (NanomsgWriteStream outStream = nanoSock.CreateSendStream()) using (var packer = Packer.Create(outStream)) { packer.PackString("Hello " + input); nanoSock.SendStream(outStream); } }; nanoListener.AddSocket(nanoSock); nanoSock.Connect("tcp://127.0.0.1:" + repPort); while (!cancellor.IsCancellationRequested) { nanoListener.Listen(TimeSpan.FromMilliseconds(250)); } nanoListener.RemoveSocket(nanoSock); nanoSock.Dispose(); }); serverThread.Start(); serverThread.Join(TimeSpan.FromMilliseconds(40)); }
public bool SendStreamImmediate(NanomsgWriteStream stream) { return(SendStreamImmediateImpl(stream)); }
public void SendStream(NanomsgWriteStream stream) { SendStreamImpl(stream); }
public bool SendStreamImmediate(NanomsgWriteStream stream) { return SendStreamImmediateImpl(stream); }
public void SendStream(NanomsgWriteStream stream) { SendStreamImpl(stream); }
internal static void Execute() { src = new byte[TestConstants.DataSize]; new Random().NextBytes(src); var bufferSizes = Enumerable.Range(8, 7).Select(i => 1 << i); const int IterationCount = 1000; foreach (var bufferSize in bufferSizes) { Console.WriteLine("Buffers: " + string.Join(" ", bufferSize)); Test("msgstream", () => { fixed(byte *srcptr = src) { byte[] buffer = new byte[bufferSize]; for (int i = 0; i < IterationCount; i++) { using (var stream = new NanomsgReadStream((IntPtr)srcptr, src.Length, null)) { int read; do { read = stream.Read(buffer, 0, buffer.Length); } while (read > 0); } } } }); /*Test("msgstream_bytes", () => * { * fixed (byte* srcptr = src) * //foreach (var bufferSize in bufferSizes) * { * for (int i = 0; i < IterationCount; i++) * using (var stream = new NanomsgReadStream((IntPtr)srcptr, src.Length, null)) * while (stream.ReadByte() >= 0) { } * } * }); * * Test("msgstream_int32", () => * { * fixed (byte* srcptr = src) * //foreach (var bufferSize in bufferSizes) * { * for (int i = 0; i < IterationCount; i++) * using (var stream = new NanomsgReadStream((IntPtr)srcptr, src.Length, null)) * while (stream.ReadInt32().HasValue) { } * } * }); * * * Test("msgstream_int64", () => * { * fixed (byte* srcptr = src) * //foreach (var bufferSize in bufferSizes) * { * for (int i = 0; i < IterationCount; i++) * using (var stream = new NanomsgReadStream((IntPtr)srcptr, src.Length, null)) * while (stream.ReadInt64().HasValue) { } * } * });*/ Test("writestream", () => { for (int i = 0; i < IterationCount; i++) { using (var dest = new NanomsgWriteStream(null)) dest.Write(src, 0, src.Length); } }); } }