/// <summary> /// A simple example showing use of NNanomsg.Socket /// For further example usage, check out the Test project. /// </summary> static void Main(string[] args) { const string socketAddress = "tcp://127.0.0.1:5088"; if (args[0] == "client") { using (var req = new NanomsgSocket(Domain.SP, Protocol.REQ)) { req.Connect(socketAddress); req.Send(new StringMessage("hello from client").GetBytes()); using (var buf = req.ReceiveStream()) Console.WriteLine("Message from SERVER: " + new StringMessage(new StreamReader(buf).ReadToEnd()).GetString()); Console.WriteLine("CLIENT finished"); } } else if (args[0] == "server") { using (var rep = new NanomsgSocket(Domain.SP, Protocol.REP)) { rep.Bind(socketAddress); using (var buf = rep.ReceiveStream()) Console.WriteLine("Message from CLIENT: " + new StringMessage(new StreamReader(buf).ReadToEnd()).GetString()); rep.Send(new StringMessage("hello from server").GetBytes()); Console.WriteLine("SERVER Finished"); } } else { Console.WriteLine("Unknown argument: " + args[0]); } }
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 RunDevice(CancellationTokenSource cancellor) { var deviceThread = new Thread(() => { var reqSock = new NanomsgSocket(Domain.SP_RAW, Protocol.REP); int reqSockId = reqSock.SocketID; reqSock.Bind("tcp://127.0.0.1:" + reqPort); var repSock = new NanomsgSocket(Domain.SP_RAW, Protocol.REQ); int repSockId = repSock.SocketID; repSock.Bind("tcp://127.0.0.1:" + repPort); // HACK: The nn_device loop is currently only terminated using NN.Term() int repDevice = NN.Device(reqSockId, repSockId); reqSock.Dispose(); repSock.Dispose(); }); deviceThread.Start(); deviceThread.Join(TimeSpan.FromMilliseconds(40)); }
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 static void Execute() { _clientData = new byte[DataSize]; _serverData = new byte[DataSize]; var r = new Random(); r.NextBytes(_clientData); r.NextBytes(_serverData); Console.WriteLine("Executing Socket object test " + NanomsgSymbols.NN_VERSION_CURRENT.ToString()); var clientThread = new Thread( () => { var req = new NanomsgSocket(Domain.SP, Protocol.REQ); req.Connect(InprocAddress); //req.Options.TcpNoDelay = true; /*unsafe { byte* s1 = (byte*)Interop.nn_allocmsg(4, 0), s2 = (byte*) Interop.nn_allocmsg(4,0); *(uint*)s1 = 0x01020304; *(uint*)s2 = 0x05060708; //byte[] scatter1 = new byte[] { 1, 2, 3, 4 }, scatter2 = new byte[] { 5, 6, 7, 8 }; //fixed (byte* s1 = scatter1, s2 = scatter2) { nn_iovec* iovecs = stackalloc nn_iovec[2]; *iovecs = new nn_iovec() { iov_base = s1, iov_len = 4 }; *(iovecs + 1) = new nn_iovec() { iov_base = s2, iov_len = 4 }; nn_msghdr* msghdr = stackalloc nn_msghdr[1]; *msghdr = new nn_msghdr() { msg_control = null, msg_controllen = 0, msg_iov = iovecs, msg_iovlen = 2 }; req.SendMessage(msghdr); } Interop.nn_freemsg((IntPtr)s1); Interop.nn_freemsg((IntPtr)s2); }*/ byte[] streamOutput = new byte[BufferSize]; while (true) { var sw = Stopwatch.StartNew(); for (int i = 0; i < Iter; i++) { var result = req.SendImmediate(_clientData); Trace.Assert(result); int read = 0; using (var stream = req.ReceiveStream()) while (stream.Length != stream.Position) read += stream.Read(streamOutput, 0, streamOutput.Length); Trace.Assert(read == _serverData.Length); } sw.Stop(); var secondsPerSend = sw.Elapsed.TotalSeconds / (double)Iter; Console.WriteLine("Time {0} us, {1} per second, {2} mb/s ", (int)(secondsPerSend * 1000d * 1000d), (int)(1d / secondsPerSend), (int)(DataSize * 2d / (1024d * 1024d * secondsPerSend))); } }); clientThread.Start(); { var rep = new NanomsgSocket(Domain.SP, Protocol.REP); rep.Bind(InprocAddress); //rep.Options.TcpNoDelay = true; byte[] streamOutput = new byte[BufferSize]; var sw = Stopwatch.StartNew(); while (sw.Elapsed.TotalSeconds < 10) { int read = 0; using (var stream = rep.ReceiveStream()) while (stream.Length != stream.Position) read += stream.Read(streamOutput, 0, streamOutput.Length); rep.SendImmediate(_serverData); } /*var listener = new NanomsgListener(); listener.AddSocket(rep.SocketID); listener.ReceivedMessage += delegate(int s) { var stream = rep.ReceiveImmediate(); if (stream == null) { Trace.Fail("receive immediate failed"); return; } int read = 0; while (stream.Length != stream.Position) read += stream.Read(streamOutput, 0, streamOutput.Length); stream.Dispose(); Trace.Assert(read == _clientData.Length); rep.SendImmediate(_serverData); }; var sw = Stopwatch.StartNew(); while (sw.Elapsed.TotalSeconds < 10) listener.Listen(TimeSpan.FromSeconds(5));*/ clientThread.Abort(); } }