public static void Main() { if (MessageLength < BitUtil.SIZE_OF_LONG) { throw new ArgumentException($"Message length must be at least {BitUtil.SIZE_OF_LONG:D} bytes"); } ComputerSpecifications.Dump(); var context = new Aeron.Context(); var reporter = new RateReporter(1000, PrintRate); _reporterThread = new Thread(_ => reporter.Run()); _reporterThread.Start(); // Connect to media driver and add publication to send messages on the configured channel and stream ID. // The Aeron and Publication classes implement AutoCloseable, and will automatically // clean up resources when this try block is finished. using (var aeron = Aeron.Connect(context)) using (var publication = aeron.AddPublication(Channel, StreamID)) using (var byteBuffer = BufferUtil.AllocateDirectAligned(MessageLength, BitUtil.CACHE_LINE_LENGTH)) using (var buffer = new UnsafeBuffer(byteBuffer)) { do { _printingActive = true; Console.WriteLine($"Streaming {NumberOfMessages} messages of {(RandomMessageLength ? " random" : "")} size {MessageLength} bytes to {Channel} on stream Id {StreamID}"); long backPressureCount = 0; for (long i = 0; i < NumberOfMessages; i++) { var length = LengthGenerator.AsInt; buffer.PutLong(0, i); OfferIdleStrategy.Reset(); while (publication.Offer(buffer, 0, length) < 0L) { // The offer failed, which is usually due to the publication // being temporarily blocked. Retry the offer after a short // spin/yield/sleep, depending on the chosen IdleStrategy. backPressureCount++; OfferIdleStrategy.Idle(); } reporter.OnMessage(1, length); } Console.WriteLine("Done streaming. Back pressure ratio " + (double) backPressureCount/NumberOfMessages); if (0 < LingerTimeoutMs) { Console.WriteLine("Lingering for " + LingerTimeoutMs + " milliseconds..."); Thread.Sleep((int) LingerTimeoutMs); } _printingActive = false; Console.WriteLine("Execute again?"); } while (Console.ReadLine() == "y"); } reporter.Halt(); _reporterThread.Join(); }
public static void FillMetaData(UnsafeBuffer cncMetaDataBuffer, int toDriverBufferLength, int toClientsBufferLength, int counterMetaDataBufferLength, int counterValuesBufferLength, long clientLivenessTimeout, int errorLogBufferLength) { cncMetaDataBuffer.PutInt(CncVersionOffset(0), CNC_VERSION); cncMetaDataBuffer.PutInt(ToDriverBufferLengthOffset(0), toDriverBufferLength); cncMetaDataBuffer.PutInt(ToClientsBufferLengthOffset(0), toClientsBufferLength); cncMetaDataBuffer.PutInt(CountersMetaDataBufferLengthOffset(0), counterMetaDataBufferLength); cncMetaDataBuffer.PutInt(CountersValuesBufferLengthOffset(0), counterValuesBufferLength); cncMetaDataBuffer.PutLong(ClientLivenessTimeoutOffset(0), clientLivenessTimeout); cncMetaDataBuffer.PutInt(ErrorLogBufferLengthOffset(0), errorLogBufferLength); }
/// <summary> /// Set the initial value for the termId in the upper bits of the tail counter. /// </summary> /// <param name="termMetaData"> contain the tail counter. </param> /// <param name="initialTermId"> to be set. </param> public static void InitialiseTailWithTermId(UnsafeBuffer termMetaData, int initialTermId) { termMetaData.PutLong(TERM_TAIL_COUNTER_OFFSET, ((long) initialTermId) << 32); }
/// <summary> /// Return an initialised default Data Frame Header. /// </summary> /// <param name="sessionId"> for the header </param> /// <param name="streamId"> for the header </param> /// <param name="termId"> for the header </param> /// <returns> byte array containing the header </returns> public static UnsafeBuffer CreateDefaultHeader(int sessionId, int streamId, int termId) { var buffer = new UnsafeBuffer(new byte[HEADER_LENGTH]); buffer.PutByte(VERSION_FIELD_OFFSET, CURRENT_VERSION); buffer.PutByte(FLAGS_FIELD_OFFSET, (byte)BEGIN_AND_END_FLAGS); buffer.PutShort(TYPE_FIELD_OFFSET, HDR_TYPE_DATA); buffer.PutInt(SESSION_ID_FIELD_OFFSET, sessionId); buffer.PutInt(STREAM_ID_FIELD_OFFSET, streamId); buffer.PutInt(TERM_ID_FIELD_OFFSET, termId); buffer.PutLong(RESERVED_VALUE_OFFSET, DEFAULT_RESERVE_VALUE); return buffer; }
/// <summary> /// Set the correlation ID used for this log. /// </summary> /// <param name="logMetaDataBuffer"> containing the meta data. </param> /// <param name="id"> value to be set. </param> public static void CorrelationId(UnsafeBuffer logMetaDataBuffer, long id) { logMetaDataBuffer.PutLong(LOG_CORRELATION_ID_OFFSET, id); }