private void PrintMessage(String address, ZMessage message) { ZMessage msg = (ZMessage)message.Clone(); string contents = ""; msg.RemoveAt(0); msg.RemoveAt(0); for (int i = 0; i < msg.Count; i++) { if (!(msg[i] == msg[msg.Count - 1])) { contents = contents + msg[i] + " "; } else { contents = contents + msg[i]; } } Console.WriteLine("[{0}] >> {1}", address, contents); }
/// <summary> /// Forwards replies from the backend socket to the frontend socket. /// </summary> protected override bool BackendHandler(ZSocket sock, out ZMessage message, out ZError error) { error = default(ZError); message = null; // receiving scope // DEALER: normal movemsg ZMessage incoming = null; if (!sock.ReceiveMessage(ref incoming, /* ZSocketFlags.DontWait */ ZSocketFlags.None, out error)) { return(false); } using (incoming) { // STREAM: write frames: identity, body, identity, empty // Read identity int ic = (int)incoming[0].Length; var identityBytes = new byte[ic]; incoming[0].Read(identityBytes, 0, ic); // Remove DEALER's delimiter incoming.RemoveAt(1); // Append Identity frame var identity0 = new ZFrame(identityBytes); incoming.Add(identity0); // Append STREAM's empty delimiter frame incoming.Add(new ZFrame()); if (!SendMsg(FrontendSocket, incoming, out error)) { return(false); } } return(true); }
public ZMessage Request(ZMessage request) { // This method does the hard work. It sends a request to all // connected servers in parallel (for this to work, all connections // must be successful and completed by this time). It then waits // for a single successful reply, and returns that to the caller. // Any other replies are just dropped: ZMessage reply = null; using (request) { // Prefix request with sequence number and empty envelope this.sequence++; request.Prepend(new ZFrame(this.sequence)); request.Prepend(new ZFrame()); // Blast the request to all connected servers for (int server = 0; server < this.servers; ++server) { using (var outgoing = request.Duplicate()) { this.socket.Send(outgoing); } } // Wait for a matching reply to arrive from anywhere // Since we can poll several times, calculate each one ZError error; DateTime endtime = DateTime.UtcNow + GLOBAL_TIMEOUT; var poll = ZPollItem.CreateReceiver(); while (endtime > DateTime.UtcNow) { if (this.socket.PollIn(poll, out reply, out error, endtime - DateTime.UtcNow)) { // Reply is [empty][sequence][OK] if (reply.Count < 3) { throw new InvalidOperationException(); } reply.RemoveAt(0); using (ZFrame sequenceFrame = reply.RemoveAt(0, false)) { int sequence = sequenceFrame.ReadInt32(); if (sequence == this.sequence) { break; // Reply is ok } } reply.Dispose(); } else { if (error == ZError.ETERM) { break; // Interrupted } if (error != ZError.EAGAIN) { throw new ZException(error); } } } } return(reply); }