protected int onReceive(object cbArg, LBMMessage msg) { switch (msg.type()) { case LBM.MSG_DATA: IntPtr messagePtr = msg.dataPointerSafe(); if (bufferAcquiredSize != msg.length()) { if (bufferAcquiredSize >= 0) { source.buffsCancel(); } // assume that the payload size is correctly checked on the pinger size bufferAcquiredSize = (source.buffAcquire(out bufferAcquired, (uint)msg.length(), 0) == 0) ? (int)msg.length() : -1; } #if RunUnsafeMode dqwordBlockCopy(bufferAcquired.ToPointer(), messagePtr.ToPointer(), (int)msg.length()); #else memcpy(bufferAcquired, messagePtr, (int)msg.length()); #endif source.buffsCompleteAndAcquire(out bufferAcquired, (uint)bufferAcquiredSize, 0); break; case LBM.MSG_BOS: System.Console.Error.WriteLine("[" + msg.topicName() + "][" + msg.source() + "], Beginning of Transport Session"); break; case LBM.MSG_EOS: System.Console.Error.WriteLine("[" + msg.topicName() + "][" + msg.source() + "], End of Transport Session"); break; case LBM.MSG_UNRECOVERABLE_LOSS: System.Console.Error.WriteLine("[" + msg.topicName() + "][" + msg.source() + "][" + msg.sequenceNumber() + "], LOST"); /* Any kind of loss makes this test invalid */ System.Console.WriteLine("Unrecoverable loss occurred. Quitting..."); System.Environment.Exit(1); break; case LBM.MSG_UNRECOVERABLE_LOSS_BURST: System.Console.Error.WriteLine("[" + msg.topicName() + "][" + msg.source() + "], LOST BURST"); /* Any kind of loss makes this test invalid */ System.Console.WriteLine("Unrecoverable loss occurred. Quitting..."); System.Environment.Exit(1); break; default: System.Console.Out.WriteLine("Unhandled receiver event [" + msg.type() + "] from source [" + msg.source() + "] with topic [" + msg.topicName() + "]. Refer to https://ultramessaging.github.io/currdoc/doc/dotnet_example/index.html#unhandledcsevents for a detailed description."); break; } msg.dispose(); return(0); }
public lbmlatpingreceiver(LBMContext lbmctx, LBMTopic lbmtopic, lbmlatping mylatping) { latping = mylatping; source = latping.ping_src; buffer = latping.msgbuf; bufferptr = latping.msgbufPtr; if (source.buffAcquire(out bufferAcquired, (uint)buffer.Length, 0) == 0) { bufferAcquiredSize = buffer.Length; } receiver = new LBMReceiver(lbmctx, lbmtopic, new LBMReceiverCallback(onReceive), null, null); }
public static int Main(string[] args) { lbmlatpong latpong = null; LBMContext ctx = null; LBMContextAttributes ctx_attr = null; LBMTopic pong_src_topic = null; LBMSourceAttributes pong_src_topic_attr = null; LBMSource pong_src = null; LBMTopic ping_rcv_topic = null; LBMReceiverAttributes ping_rcv_topic_attr = null; lbmlatpongreceiver ping_rcv = null; latpong = new lbmlatpong(args); if (latpong.cpu >= 0) { Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(latpong.cpu); } /* Create the context. */ ctx_attr = new LBMContextAttributes(); ctx_attr.setValue("resolver_cache", "0"); ctx_attr.setValue("operational_mode", "sequential"); ctx_attr.setValue("request_tcp_port_high", "50000"); ctx_attr.setValue("transport_lbtipc_receiver_thread_behavior", "busy_wait"); ctx = new LBMContext(ctx_attr); ctx_attr.dispose(); /* Create the pong source. */ pong_src_topic_attr = new LBMSourceAttributes(); pong_src_topic_attr.setValue("resolver_advertisement_sustain_interval", "0"); pong_src_topic_attr.setValue("transport", "lbtsmx"); pong_src_topic = new LBMTopic(ctx, "lbmlat-pong", pong_src_topic_attr); pong_src_topic_attr.dispose(); pong_src = new LBMSource(ctx, pong_src_topic); latpong.pong_src = pong_src; /* Create the ping receiver. */ ping_rcv_topic_attr = new LBMReceiverAttributes(); ping_rcv_topic_attr.enableSingleReceiverCallback(true); ping_rcv_topic_attr.setSourceNotificationCallbacks( new LBMSourceCreationCallback(latpong.onNewSource), new LBMSourceDeletionCallback(latpong.onSourceDelete), null); ping_rcv_topic = new LBMTopic(ctx, "lbmlat-ping", ping_rcv_topic_attr); ping_rcv_topic_attr.dispose(); ping_rcv = new lbmlatpongreceiver(ctx, ping_rcv_topic, latpong, pong_src); /* Wait a bit for things to get set up. */ System.Threading.Thread.Sleep(1000); /* Run the context until we've discovered the pinger's source. */ while (!latpong.found_pinger) { ctx.processEvents(1000); } /* Wait a bit for things to get set up. */ System.Threading.Thread.Sleep(1000); /* Send in a dummy pong message to kick things off. */ IntPtr writeBuff; if (pong_src.buffAcquire(out writeBuff, (uint)16, 0) == 0) { Marshal.WriteInt64(writeBuff, 0, 1234567890); pong_src.buffsComplete(); } /* Wait forever. */ while (true) { System.Threading.Thread.Sleep(1000000); // ctx.processEvents(1000000); } }
protected int onReceive(object cbArg, LBMMessage msg) { long t; if (_ping) { t = System.Diagnostics.Stopwatch.GetTimestamp() * lbmpong.pspertick / 1000; } else { t = 0; } switch (msg.type()) { case LBM.MSG_DATA: if (rtt_ignore == 0) { _msg_count++; } byte[] message = null; IntPtr messagePtr = (IntPtr)0; long s = 0; if (use_smx) { messagePtr = msg.dataPointerSafe(); } else { message = msg.data(); } if (_ping) { s = (use_smx) ? lbmpong.parse_s(messagePtr, 0) : lbmpong.parse_s(message, 0); calc_latency(t, s); if (rtt_ignore == 0 && _msg_count == _msgs) { rtt_avg_usec = ((double)total_nsec / 1000.0) / (double)_msg_count; print_rtt_data(); print_latency(System.Console.Out); try { print_stats(); } catch (LBMException ex) { System.Console.Error.WriteLine("Error printing transport stats in ponglbmreceiver" + ex.Message); } end(); msg.dispose(); return(0); } if (_msecpause > 0) { // This would not normally be a good // thing in a callback on the context // thread. System.Threading.Thread.Sleep(_msecpause); } if (!use_smx) { lbmpong.format(message, 0, System.Diagnostics.Stopwatch.GetTimestamp() * lbmpong.pspertick / 1000); } } if (use_mim) { _ctx.send(_ping ? "lbmpong/ping" : "lbmpong/pong", message, message.Length, LBM.MSG_FLUSH | LBM.SRC_NONBLOCK); } else if (use_smx) { try { IntPtr writeBuff; if (_src.buffAcquire(out writeBuff, (uint)msg.length(), 0) == 0) { if (_ping) { lbmpong.format(writeBuff, 0, System.Diagnostics.Stopwatch.GetTimestamp() * lbmpong.pspertick / 1000); } else { memcpy(writeBuff, messagePtr, (int)msg.length()); } _src.buffsComplete(); } } catch (LBMException ex) { System.Console.Error.WriteLine("Error (while doing SMX acquire/complete): " + ex.Message); System.Environment.Exit(1); } } else { _src.send(message, message.Length, LBM.MSG_FLUSH | LBM.SRC_NONBLOCK); } if (_ping && _verbose) { System.Console.Out.WriteLine(_msg_count + " curr " + t + " sent " + s + " latency " + (t - s) + " ns"); } if (rtt_ignore > 0) { rtt_ignore--; } break; case LBM.MSG_BOS: System.Console.Error.WriteLine("[" + msg.topicName() + "][" + msg.source() + "], Beginning of Transport Session"); break; case LBM.MSG_EOS: System.Console.Error.WriteLine("[" + msg.topicName() + "][" + msg.source() + "], End of Transport Session"); if (_end_on_eos) { end(); } break; case LBM.MSG_UNRECOVERABLE_LOSS: if (_verbose) { System.Console.Error.WriteLine("[" + msg.topicName() + "][" + msg.source() + "][" + msg.sequenceNumber() + "], LOST"); } /* Any kind of loss makes this test invalid */ System.Console.WriteLine("Unrecoverable loss occurred. Quitting..."); System.Environment.Exit(1); break; case LBM.MSG_UNRECOVERABLE_LOSS_BURST: System.Console.Error.WriteLine("[" + msg.topicName() + "][" + msg.source() + "], LOST BURST"); /* Any kind of loss makes this test invalid */ System.Console.WriteLine("Unrecoverable loss occurred. Quitting..."); System.Environment.Exit(1); break; default: System.Console.Out.WriteLine("Unhandled receiver event [" + msg.type() + "] from source [" + msg.source() + "] with topic [" + msg.topicName() + "]. Refer to https://ultramessaging.github.io/currdoc/doc/dotnet_example/index.html#unhandledcsevents for a detailed description."); break; } msg.dispose(); System.Console.Out.Flush(); return(0); }
private lbmpong(string[] args) { if (System.Environment.GetEnvironmentVariable("LBM_LICENSE_FILENAME") == null && System.Environment.GetEnvironmentVariable("LBM_LICENSE_INFO") == null) { SetEnvironmentVariable("LBM_LICENSE_FILENAME", "lbm_license.txt"); } LBM lbm = new LBM(); lbm.setLogger(new LBMLogging(logger)); process_cmdline(args); if (use_mim && !eventq) { System.Console.Out.WriteLine("Using mim requires event queue to send from receive callback - forcing use"); eventq = true; } if (msecpause > 0 && !eventq) { System.Console.Out.WriteLine("Setting pause value requires event queue - forcing use"); eventq = true; } LBMSourceAttributes sattr = new LBMSourceAttributes(); LBMContextAttributes cattr = new LBMContextAttributes(); /* Check if protocol needs to be set to lbtrm | lbtru */ if (protocol == 'M') { try { sattr.setValue("transport", "LBTRM"); cattr.setValue("transport_lbtrm_data_rate_limit", send_rate.ToString()); cattr.setValue("transport_lbtrm_retransmit_rate_limit", retrans_rate.ToString()); } catch (LBMException ex) { System.Console.Error.WriteLine("Error setting LBTRM rate: " + ex.Message); System.Environment.Exit(1); } } if (protocol == 'U') { try { sattr.setValue("transport", "LBTRU"); cattr.setValue("transport_lbtru_data_rate_limit", send_rate.ToString()); cattr.setValue("transport_lbtru_retransmit_rate_limit", retrans_rate.ToString()); } catch (LBMException ex) { System.Console.Error.WriteLine("Error setting LBTRU rate: " + ex.Message); System.Environment.Exit(1); } } LBMContext ctx = new LBMContext(cattr); PongLBMEventQueue evq = null; if (eventq) { System.Console.Error.WriteLine("Event queue in use"); evq = new PongLBMEventQueue(); } else { System.Console.Error.WriteLine("No event queue"); } System.Console.Out.Flush(); LBMSource src = null; PongLBMReceiver rcv; LBMTopic src_topic = null; LBMTopic rcv_topic; if (ping) { System.Console.Error.WriteLine( "Sending " + msgs + " " + msglen + " byte messages to topic lbmpong/ping pausing " + msecpause + " msec between"); if (!use_mim) { src_topic = ctx.allocTopic("lbmpong/ping", sattr); } rcv_topic = ctx.lookupTopic("lbmpong/pong"); } else { rcv_topic = ctx.lookupTopic("lbmpong/ping"); if (!use_mim) { src_topic = ctx.allocTopic("lbmpong/pong", sattr); } } PongSrcCB srccb = new PongSrcCB(); if (!use_mim) { src = ctx.createSource(src_topic, new LBMSourceEventCallback(srccb.onSourceEvent), null); use_smx = src.getAttributeValue("transport").ToLower().Contains("smx"); if (use_smx) { /* Perform configuration validation */ const int smx_header_size = 16; int max_payload_size = Convert.ToInt32(src.getAttributeValue("transport_lbtsmx_datagram_max_size")) + smx_header_size; if (msglen > max_payload_size) { /* The SMX transport doesn't fragment, so payload must be within maximum size limits */ System.Console.WriteLine("Error: Message size requested is larger than configured SMX datagram size."); System.Environment.Exit(1); } } } rcv = new PongLBMReceiver(ctx, rcv_topic, evq, src, ping, msecpause, msgs, verbose, end_on_eos, rtt_collect, rtt_ignore, use_mim); System.Threading.Thread.Sleep(5000); if (ping) { byte [] message = new byte[msglen]; rcv.start(); format(message, 0, System.Diagnostics.Stopwatch.GetTimestamp() * lbmpong.pspertick / 1000); if (use_mim) { ctx.send("lbmpong/ping", message, msglen, LBM.MSG_FLUSH); } else if (use_smx) { try { IntPtr writeBuff; if (src.buffAcquire(out writeBuff, (uint)msglen, 0) == 0) { Marshal.Copy(message, 0, writeBuff, msglen); src.buffsComplete(); } } catch (LBMException ex) { System.Console.Error.WriteLine("Error (while doing SMX acquire/complete): " + ex.Message); System.Environment.Exit(1); } } else { src.send(message, msglen, LBM.MSG_FLUSH); } } if (eventq) { evq.run(run_secs * 1000); } else { System.Threading.Thread.Sleep(run_secs * 1000); } System.Console.Error.WriteLine("Quitting...."); if (!use_mim) { src.close(); } rcv.close(); ctx.close(); if (eventq) { evq.close(); } }