예제 #1
0
        public void Run()
        {
            const int OneKb = 1024;
            const int OneMb = OneKb * 1024;

            var testStreamId = 99;
            var channel      = "aeron:ipc";
            var maxMsgLength = OneMb;

            var context = new Aeron.Context();

            IIdleStrategy offerIdleStrategy = new SpinWaitIdleStrategy();

            // 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 = Adaptive.Aeron.Aeron.Connect(context))
                using (var publication = aeron.AddPublication(channel, testStreamId))
                    using (var byteBuffer = BufferUtil.AllocateDirectAligned(maxMsgLength, BitUtil.CACHE_LINE_LENGTH))
                        using (var buffer = new UnsafeBuffer(byteBuffer))
                        {
                            do
                            {
                                TestMessage tm = new TestMessage()
                                {
                                    EventId = 123, Bleb = string.Join(",", Enumerable.Repeat(Guid.NewGuid().ToString(), 100))
                                };
                                //Console.WriteLine($"Streaming {NumberOfMessages} messages of {(RandomMessageLength ? " random" : "")} size {MessageLength} bytes to {Channel} on stream Id {StreamID}");



                                Publish(tm, publication, offerIdleStrategy, buffer);

                                //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");
                        }
        }
예제 #2
0
        private void Run()
        {
            const int OneKb = 1024;
            const int OneMb = OneKb * 1024;

            var testStreamId = 99;
            var channel      = "aeron:ipc";
            var maxMsgLength = OneMb;

            // Maximum number of message fragments to receive during a single 'poll' operation
            const int fragmentLimitCount = 10;

            Console.WriteLine("Subscribing to " + channel + " on stream Id " + testStreamId);



            // Create a context, needed for client connection to media driver
            // A separate media driver process need to run prior to running this application

            // Create an Aeron instance with client-provided context configuration, connect to the
            // media driver, and add a subscription for the given channel and stream using the supplied
            // dataHandler method, which will be called with new messages as they are received.
            // The Aeron and Subscription classes implement AutoCloseable, and will automatically
            // clean up resources when this try block is finished.
            using (var aeron = Aeron.Connect(new Aeron.Context()))
                using (var subscription = aeron.AddSubscription(channel, testStreamId))
                {
                    IIdleStrategy idleStrategy = new SpinWaitIdleStrategy();

                    // Try to read the data from subscriber
                    while (_stopping.Get())
                    {
                        // poll delivers messages to the dataHandler as they arrive
                        // and returns number of fragments read, or 0
                        // if no data is available.
                        var fragmentsRead = subscription.Poll(new FragmentAssembler((buffer, offset, length, header) => {}), fragmentLimitCount);
                        // Give the IdleStrategy a chance to spin/yield/sleep to reduce CPU
                        // use if no messages were received.
                        idleStrategy.Idle(fragmentsRead);
                    }
                }
        }