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 static void Process(byte[] buf) { Message msg = Message.CreateMessage(buf); if (msg == null) { rep.Send("Unrecognized command", System.Text.Encoding.ASCII); return; } if (msg is RegisterMessage) { if (ex.RegisterUser(msg as RegisterMessage)) { rep.Send(OK); } else { rep.Send(ALREADY_HERE); } } if (msg is ShutDownMessage) { ex.ShutDown(); rep.Send(OK); } if (msg is PlaceMessage) { Tuple <byte[], List <byte[]> > send = ex.Place(msg as PlaceMessage); rep.Send(send.Item1); foreach (byte[] b in send.Item2) { pub.Send(b); } } if (msg is CancelMessage) { int r = ex.CancelOrder(msg as CancelMessage); switch (r) { case -1: rep.Send(USER_NOT_FOUND); break; case -2: rep.Send(ORDER_NOT_FOUND); break; default: rep.Send(OK); break; } } }
public void Send(IDestination destination, IMessage message, MsgDeliveryMode deliveryMode, MsgPriority priority, TimeSpan timeToLive) { if (null != this.ProducerTransformer) { IMessage transformedMessage = ProducerTransformer(this.session, this, message); if (null != transformedMessage) { message = transformedMessage; } } // TODO: Support encoding of all message types + all meta data (e.g., headers and properties) messageProducer.Send(((ITextMessage)message).Text, Encoding.ASCII); }
private void sendProc() { ZMQ.Socket resp = CTX.Socket(ZMQ.SocketType.PUB); if (!string.IsNullOrEmpty(SenderId)) { resp.Identity = Encoding.ASCII.GetBytes(SenderId); } resp.Connect(pub_addr); while (isRunning) { itemsReadyToSend.WaitOne(); lock (sendQ) { while (sendQ.Count != 0) { byte[] stuffToSend = sendQ.Dequeue(); bool sentOk = false; while (!sentOk) { try { resp.Send(stuffToSend); sentOk = true; } catch (ZMQ.Exception ex) { if (ex.Errno == (int)ZMQ.ERRNOS.EAGAIN) { sentOk = false; } else { throw ex; } } } } } } resp.Dispose(); itemsReadyToSend.Close(); Interlocked.Decrement(ref threadStillRunning); }
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 void Send(byte[] data) { _zmqSocket.Send(data); }