public void TestIfItCanSum() { // Arrange Context context = null; Socket slave = null; ZMQ.Socket master = null; using (context = new Context()) { using (master = context.Socket(SocketType.REQ)) { master.Connect(Transport.TCP, CONNECT_HOSTNAME, TEST_PORT); slave = context.Socket(SocketType.REP); slave.Bind(Transport.TCP, BIND_HOSTNAME, TEST_PORT); using (SumadorSendRecvEsclavo target = new SumadorSendRecvEsclavo(slave, slave)) { int[] numeros = new int[] { 1, 2, 4, 8, 16, 32, 64, 128 }; // Act var task = Task.Factory.StartNew(() => target.EjecutarOperacion()); master.Send(numeros); double resultado = master.Recv <double>(); // Assert Assert.That(resultado, Is.EqualTo(numeros.Sum())); Assert.That(target.Resultado, Is.EqualTo(resultado)); // Reset } } } }
static void Main(string[] args) { // allocate a buffer byte[] zmq_buffer = new byte[1024]; // Prepare our context and socket ZMQ.Context context = new ZMQ.Context(1); ZMQ.Socket socket = context.Socket(ZMQ.SocketType.REP); socket.Bind("tcp://*:5555"); while (true) { try { // Wait for next request from client zmq_buffer = socket.Recv(); string request = Encoding.ASCII.GetString(zmq_buffer); // log that we got one Console.WriteLine("Received request: [%s]", request); // Do some 'work' Thread.Sleep(1); // Send reply back to client socket.Send(Encoding.ASCII.GetBytes("World".ToCharArray())); } catch (ZMQ.Exception z) { // report the exception Console.WriteLine("ZMQ Exception occurred : {0}", z.Message); } } }
private void MonitorResponder(Socket monitor, BlockingCollection <byte[]> queue, Encoding encoding) { while (_isRunning) { monitor.Recv(); var queueLength = queue.Count(); Console.WriteLine("MONITOR - Received queue length monitor request - currently at {0}", queueLength); monitor.Send(string.Format("{0}", queueLength), encoding); } }
private void RequestPusher(Socket destination, IEnumerable <byte[]> messages, Encoding encoding) { foreach (var message in messages) { var stringMessage = encoding.GetString(message); Console.WriteLine("PUSHER - Pulled message {0} from queue", stringMessage); destination.Send(message); destination.Recv(); } _isRunning = false; }
private void RecvEventsLoop() { while (true) { byte[] msg = _subscriber.Recv(RecvExpire); if (msg != null) { OnEventReceived(msg); } if (_event.WaitOne(0)) { break; } } }
static void Main(string[] args) { InitErrMessages(); rep = cntx.Socket(ZMQ.SocketType.REP); rep.Bind("tcp://*:1000"); pub = cntx.Socket(ZMQ.SocketType.PUB); pub.Bind("tcp://*:1001"); while (true) { byte[] buf = rep.Recv(0); if (buf != null) { Process(buf); } } }
private void RequestReceiver(Socket source, BlockingCollection <byte[]> queue, Encoding encoding) { while (_isRunning) { var message = source.Recv(); var stringMessage = encoding.GetString(message); Console.WriteLine("RECEIVER - Received message {0} from source", stringMessage); queue.Add(message); source.Send(); if (stringMessage == "Done") { break; } } _queue.CompleteAdding(); }
public Queue <byte[]> RecvAll(Int32 timeout) { byte[] bytes = _zmqSocket.Recv(timeout); if (bytes == null) { return(null); } var queue = new Queue <byte[]>(); queue.Enqueue(bytes); while (_zmqSocket.RcvMore) { queue.Enqueue(_zmqSocket.Recv()); } return(queue); }
/// <summary> /// Receive message from subscriber /// </summary> /// <returns> /// message interface /// </returns> public IMessage Receive() { // TODO: Support decoding of all message types + all meta data (e.g., headers and properties) return(ToNmsMessage(messageSubscriber.Recv(Encoding.ASCII, ZSendRecvOpt.NONE))); }