private static void Send(ZSocket responder, PalResponse msg) { Console.WriteLine($"--> {msg.GetType().Name}"); string responseJson = JsonConvert.SerializeObject(msg, settings); responder.Send(new ZFrame(responseJson)); }
public void SetCallback(NotificationCallback callback) { if (notificationCallback != null) { throw new NotImplementedException("Safeguard against the application being designed in a more difficult way than easiest case"); } CheckConfig(); Task.Factory.StartNew(() => { // wait for callback using (var ctx = new ZContext()) using (var subscriber = new ZSocket(ctx, ZSocketType.SUB)) { subscriber.Connect("tcp://127.0.0.1:5556"); subscriber.Subscribe("notificationCallback"); while (true) { using (var replyFrame = subscriber.ReceiveFrame()) { string json = replyFrame.ReadString(); PalResponse deserialised = JsonConvert.DeserializeObject <PalResponse>(json, jsonSettings); if (deserialised is NotificationResponse msg1) { notificationCallback(msg1.Bitmap); } else { throw new NotImplementedException("Bad notificationCallback"); } } } } }); // tell the far side that a callback delegate has been set. Don't actually pass the delegate, since that would be meaningless. var msg = new SetCallbackMessage(); Send(msg); // keep a reference to the delegate, such that when we receive callbacks, by whatever means, this is the method we call. notificationCallback = callback; #warning Only supports a single caller to this method. This might not be adequate, depending on application design. Test. }
private PalResponse Send(PalMessage message) { lock (lockObj) { requester.Send(new ZFrame(JsonConvert.SerializeObject(message, jsonSettings))); /*if (message is SetCallbackMessage) * { * return null; * } * else*/ { using (ZFrame reply = requester.ReceiveFrame()) { string replyJson = reply.ReadString(); PalResponse deserialised = JsonConvert.DeserializeObject <PalResponse>(replyJson, jsonSettings); return(deserialised); } } } }