Exemplo n.º 1
0
 /// <summary>
 /// Map a position over a buffer and this indicator owns the counter for reclamation.
 /// </summary>
 /// <param name="buffer">          containing the counter. </param>
 /// <param name="counterId">       identifier of the counter. </param>
 /// <param name="countersManager"> to be used for freeing the counter when this is closed. </param>
 public UnsafeBufferPosition(UnsafeBuffer buffer, int counterId, CountersManager countersManager)
 {
     _buffer = buffer;
     _counterId = counterId;
     _countersManager = countersManager;
     _offset = CountersReader.CounterOffset(counterId);
 }
Exemplo n.º 2
0
 public void WhenAllocatedAndDisposed_ShouldFreeMemory()
 {
     using (var buffer = new UnsafeBuffer(short.MaxValue + 1))
     {
         GC.KeepAlive(buffer);
     }
 }
Exemplo n.º 3
0
        public static void Main()
        {
            const string channel = "aeron:ipc";
            const int streamId = 42;

            var buffer = new UnsafeBuffer(new byte[256]);

            try
            {
                using (var aeron = Aeron.Connect())
                using (var publisher = aeron.AddPublication(channel, streamId))
                using (var subscriber = aeron.AddSubscription(channel, streamId))
                {
                    var message = buffer.PutStringWithoutLengthUtf8(0, "Hello World!");

                    publisher.Offer(buffer, 0, message);
                    Console.WriteLine("Message sent...");

                    while (subscriber.Poll(PrintMessage, 1) == 0)
                    {
                        Thread.Sleep(10);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
        }
Exemplo n.º 4
0
        public SingleThreadSegmentPool(int segmentCount)
        {
            if (segmentCount < 1024)
            {
                throw new ArgumentException($"SegmentCount must be at least {MinimalSegmentCount}", nameof(segmentCount));
            }

            var segmentStructureOverhead = segmentCount*Segment.Size;
            var segmentData = segmentCount*SegmentSize;

            _buffer = new UnsafeBuffer(segmentData + segmentStructureOverhead);

            var segments = (Segment*) _buffer.RawBytes;
            var data = _buffer.RawBytes + segmentStructureOverhead;

            for (var i = 0; i < segmentCount; i++)
            {
                var s = segments + i;
                var buffer = data + i*SegmentSize;
                var segment = new Segment(buffer, SegmentSize);

                // copy to the memory pointed by s
                Native.MemcpyUnmanaged((byte*) s, (byte*) &segment, Segment.Size);

                Push(s);
            }
        }
Exemplo n.º 5
0
        internal Publication(ClientConductor clientConductor, string channel, int streamId, int sessionId, IReadablePosition positionLimit, LogBuffers logBuffers, long registrationId)
        {
            var buffers = logBuffers.AtomicBuffers();
            var logMetaDataBuffer = buffers[LogBufferDescriptor.LOG_META_DATA_SECTION_INDEX];

            for (var i = 0; i < LogBufferDescriptor.PARTITION_COUNT; i++)
            {
                _termAppenders[i] = new TermAppender(buffers[i], buffers[i + LogBufferDescriptor.PARTITION_COUNT]);
            }

            var termLength = logBuffers.TermLength();
            _maxPayloadLength = LogBufferDescriptor.MtuLength(logMetaDataBuffer) - DataHeaderFlyweight.HEADER_LENGTH;
            MaxMessageLength = FrameDescriptor.ComputeMaxMessageLength(termLength);
            _clientConductor = clientConductor;
            Channel = channel;
            StreamId = streamId;
            SessionId = sessionId;
            InitialTermId = LogBufferDescriptor.InitialTermId(logMetaDataBuffer);
            _logMetaDataBuffer = logMetaDataBuffer;
            RegistrationId = registrationId;
            _positionLimit = positionLimit;
            _logBuffers = logBuffers;
            _positionBitsToShift = IntUtil.NumberOfTrailingZeros(termLength);
            _headerWriter = new HeaderWriter(LogBufferDescriptor.DefaultFrameHeader(logMetaDataBuffer));
        }
Exemplo n.º 6
0
        private void ProcessSquelch(float* audio, int length)
        {
            if (_squelchThreshold > 0)
            {
                if (_hissBuffer == null || _hissBuffer.Length != length)
                {
                    _hissBuffer = UnsafeBuffer.Create(length, sizeof(float));
                    _hissPtr = (float*) _hissBuffer;
                }

                Utils.Memcpy(_hissPtr, audio, length * sizeof(float));

                _hissFilter.Process(_hissPtr, length);

                for (var i = 0; i < _hissBuffer.Length; i++)
                {
                    var n = (1 - _noiseAveragingRatio) * _noiseLevel + _noiseAveragingRatio * Math.Abs(_hissPtr[i]);
                    if (!float.IsNaN(n))
                    {
                        _noiseLevel = n;
                    }
                    if (_noiseLevel > _noiseThreshold)
                    {
                        audio[i] = 0.0f;
                    }
                }

                _isSquelchOpen = _noiseLevel < _noiseThreshold;
            }
            else
            {
                _isSquelchOpen = true;
            }
        }
Exemplo n.º 7
0
 public unsafe ComplexFilter(Complex[] kernel)
     : base(ComplexFilter.GetFFTSize(kernel.Length))
 {
     this._kernelBuffer = UnsafeBuffer.Create(this.FFTSize, sizeof (Complex));
       this._kernelPtr = (Complex*) (void*) this._kernelBuffer;
       this.SetKernel(kernel);
 }
Exemplo n.º 8
0
        static Trig()
        {
            _mask = ~(-1 << ResolutionInBits);
            var sampleCount = _mask + 1;

            _sinBuffer = UnsafeBuffer.Create(sampleCount, sizeof(float));
            _cosBuffer = UnsafeBuffer.Create(sampleCount, sizeof(float));
            _sinPtr = (float*) _sinBuffer;
            _cosPtr = (float*) _cosBuffer;

            const float twoPi = (float) (Math.PI * 2.0);
            const float pi2 = (float) (Math.PI / 2.0);
            _indexScale = sampleCount / twoPi;

            for (var i = 0; i < sampleCount; i++)
            {
                _sinPtr[i] = (float) Math.Sin((i + 0.5f) / sampleCount * twoPi);
                _cosPtr[i] = (float) Math.Cos((i + 0.5f) / sampleCount * twoPi);
            }

            for (var angle = 0.0f; angle < twoPi; angle += pi2)
            {
                _sinPtr[(int) (angle * _indexScale) & _mask] = (float) Math.Sin(angle);
                _cosPtr[(int) (angle * _indexScale) & _mask] = (float) Math.Cos(angle);
            }
        }
Exemplo n.º 9
0
 public void Dispose()
 {
     _coeffBuffer = null;
     _queueBuffer = null;
     _coeffPtr = null;
     _queuePtr = null;
     GC.SuppressFinalize(this);
 }
Exemplo n.º 10
0
 public unsafe void Dispose()
 {
     this._coeffBuffer = (UnsafeBuffer) null;
       this._queueBuffer = (UnsafeBuffer) null;
       this._coeffPtr = (float*) null;
       this._queuePtr = (float*) null;
       GC.SuppressFinalize((object) this);
 }
Exemplo n.º 11
0
 public unsafe NoiseFilter(int fftSize = 4096)
     : base(fftSize)
 {
     this._gainBuffer1 = UnsafeBuffer.Create(fftSize, 4);
       this._gainPtr1 = (float*) (void*) this._gainBuffer1;
       this._gainBuffer2 = UnsafeBuffer.Create(fftSize, 4);
       this._gainPtr2 = (float*) (void*) this._gainBuffer2;
 }
Exemplo n.º 12
0
        public void Setup()
        {
            _consumer = A.Fake<IntObjConsumer<string>>();
            _metaData = A.Fake<CountersReader.MetaData>();

            _labelsBuffer = new UnsafeBuffer(new byte[NumberOfCounters*CountersReader.METADATA_LENGTH]);
            _counterBuffer = new UnsafeBuffer(new byte[NumberOfCounters*CountersReader.COUNTER_LENGTH]);

            _manager = new CountersManager(_labelsBuffer, _counterBuffer);
            _otherManager = new CountersManager(_labelsBuffer, _counterBuffer);
        }
Exemplo n.º 13
0
        public RdsDecoder()
        {
            _pllBuffer = UnsafeBuffer.Create(sizeof(Pll));
            _pll = (Pll*) _pllBuffer;

            _oscBuffer = UnsafeBuffer.Create(sizeof(Oscillator));
            _osc = (Oscillator*) _oscBuffer;

            _syncFilterBuffer = UnsafeBuffer.Create(sizeof(IirFilter));
            _syncFilter = (IirFilter*) _syncFilterBuffer;
        }
Exemplo n.º 14
0
 public unsafe RdsDecoder()
 {
     this._pllBuffer = UnsafeBuffer.Create(sizeof (Pll));
       this._pll = (Pll*) (void*) this._pllBuffer;
       this._oscBuffer = UnsafeBuffer.Create(sizeof (Oscillator));
       this._osc = (Oscillator*) (void*) this._oscBuffer;
       this._syncFilterBuffer = UnsafeBuffer.Create(sizeof (IirFilter));
       this._syncFilter = (IirFilter*) (void*) this._syncFilterBuffer;
       this._bitDecoder = new RdsDetectorBank();
       this._bitDecoder.FrameAvailable += new RdsFrameAvailableDelegate(this.FrameAvailableHandler);
 }
Exemplo n.º 15
0
        public NoiseFilter(int fftSize)
            : base(fftSize)
        {
            _gainBuffer = UnsafeBuffer.Create(fftSize, sizeof(float));
            _gainPtr = (float*) _gainBuffer;

            _smoothedGainBuffer = UnsafeBuffer.Create(fftSize, sizeof(float));
            _smoothedGainPtr = (float*) _smoothedGainBuffer;

            _powerBuffer = UnsafeBuffer.Create(fftSize, sizeof(float));
            _powerPtr = (float*) _powerBuffer;
        }
Exemplo n.º 16
0
 public unsafe IQBalancer()
 {
     this._dcRemoverIBuffer = UnsafeBuffer.Create(sizeof (DcRemover));
       this._dcRemoverI = (DcRemover*) (void*) this._dcRemoverIBuffer;
       this._dcRemoverI->Init(1E-05f);
       this._dcRemoverQBuffer = UnsafeBuffer.Create(sizeof (DcRemover));
       this._dcRemoverQ = (DcRemover*) (void*) this._dcRemoverQBuffer;
       this._dcRemoverQ->Init(1E-05f);
       this._windowBuffer = UnsafeBuffer.Create((Array) FilterBuilder.MakeWindow(WindowType.Hamming, 1024));
       this._windowPtr = (float*) (void*) this._windowBuffer;
       this._isMultithreaded = Environment.ProcessorCount > 1;
 }
Exemplo n.º 17
0
 public unsafe OverlapSaveProcessor(int fftSize)
 {
     this._fftSize = fftSize;
       this._halfSize = this._fftSize / 2;
       this._inputPos = this._halfSize;
       this._queuepBuffer = UnsafeBuffer.Create(this._fftSize, sizeof (Complex));
       this._queuePtr = (Complex*) (void*) this._queuepBuffer;
       this._fftBuffer = UnsafeBuffer.Create(this._fftSize, sizeof (Complex));
       this._fftPtr = (Complex*) (void*) this._fftBuffer;
       this._outputBuffer = UnsafeBuffer.Create(this._halfSize, sizeof (Complex));
       this._outputPtr = (Complex*) (void*) this._outputBuffer;
 }
Exemplo n.º 18
0
        public virtual void ShouldPassThroughUnfragmentedMessage()
        {
            A.CallTo(() => header.Flags).Returns(FrameDescriptor.UNFRAGMENTED);

            var srcBuffer = new UnsafeBuffer(new byte[128]);
            const int offset = 8;
            const int length = 32;

            adapter.OnFragment(srcBuffer, offset, length, header);

            A.CallTo(() => delegateFragmentHandler(srcBuffer, offset, length, header))
                .MustHaveHappened(Repeated.Exactly.Once);
        }
Exemplo n.º 19
0
 public IQBalancer()
 {
     _dcRemoverIBuffer = UnsafeBuffer.Create(sizeof(DcRemover));
     _dcRemoverI = (DcRemover*) _dcRemoverIBuffer;
     _dcRemoverI->Init(DcTimeConst);
     _dcRemoverQBuffer = UnsafeBuffer.Create(sizeof(DcRemover));
     _dcRemoverQ = (DcRemover*) _dcRemoverQBuffer;
     _dcRemoverQ->Init(DcTimeConst);
     var window = FilterBuilder.MakeWindow(WindowType.Hamming, FFTBins);
     _windowBuffer = UnsafeBuffer.Create(window);
     _windowPtr = (float*) _windowBuffer;
     _isMultithreaded = Environment.ProcessorCount > 1;
 }
Exemplo n.º 20
0
        private void ProcessMono(float* baseBand, float* interleavedStereo, int length)
        {
            #region Prepare buffer

            if (_channelABuffer == null || _channelABuffer.Length != length)
            {
                _channelABuffer = UnsafeBuffer.Create(length, sizeof(float));
                _channelAPtr = (float*)_channelABuffer;
            }

            #endregion

            #region Decimate L+R

            Utils.Memcpy(_channelAPtr, baseBand, length * sizeof(float));
            _channelADecimator.Process(_channelAPtr, length);

            #endregion

            #region Filter L+R

            length /= _audioDecimationFactor;
            _channelAFilter.Process(_channelAPtr, length);

            #endregion

            #region Process deemphasis

            for (var i = 0; i < length; i++)
            {
                _deemphasisAvgL += _deemphasisAlpha * (_channelAPtr[i] - _deemphasisAvgL);
                _channelAPtr[i] = _deemphasisAvgL;
            }

            #endregion

            #region Fill output buffer

            for (var i = 0; i < length; i++)
            {
                var sample = _channelAPtr[i] * AudioGain;
                interleavedStereo[i * 2] = sample;
                interleavedStereo[i * 2 + 1] = sample;
            }

            #endregion
        }
Exemplo n.º 21
0
        public void ShouldRejectWriteWhenInsufficientSpace()
        {
            const int length = 200;
            const long head = 0L;
            var tail = head + (Capacity - BitUtil.Align(length - RecordDescriptor.Alignment, RecordDescriptor.Alignment));

            A.CallTo(() => _buffer.GetLongVolatile(HeadCounterIndex)).Returns(head);
            A.CallTo(() => _buffer.GetLongVolatile(TailCounterIndex)).Returns(tail);

            var srcBuffer = new UnsafeBuffer(new byte[1024]);

            const int srcIndex = 0;
            Assert.False(_ringBuffer.Write(MsgTypeID, srcBuffer, srcIndex, length));

            A.CallTo(() => _buffer.PutInt(A<int>._, A<int>._)).MustNotHaveHappened();
            A.CallTo(() => _buffer.CompareAndSetLong(A<int>._, A<long>._, A<long>._)).MustNotHaveHappened();
            A.CallTo(() => _buffer.PutBytes(A<int>._, srcBuffer, A<int>._, A<int>._)).MustNotHaveHappened();
            A.CallTo(() => _buffer.PutIntOrdered(A<int>._, A<int>._)).MustNotHaveHappened();
        }
Exemplo n.º 22
0
        public static void Main()
        {
            var ctx = new Aeron.Context()
                .AvailableImageHandler(AvailablePongImageHandler);

            var fragmentAssembler = new FragmentAssembler(PongHandler);

            Console.WriteLine("Publishing Ping at " + PingChannel + " on stream Id " + PingStreamID);
            Console.WriteLine("Subscribing Pong at " + PongChannel + " on stream Id " + PongStreamID);
            Console.WriteLine("Message length of " + MessageLength + " bytes");

            using (var aeron = Aeron.Connect(ctx))
            {
                Console.WriteLine("Warming up... " + WarmupNumberOfIterations + " iterations of " + WarmupNumberOfMessages + " messages");

                using (var publication = aeron.AddPublication(PingChannel, PingStreamID))
                using (var subscription = aeron.AddSubscription(PongChannel, PongStreamID))
                using (var byteBuffer = BufferUtil.AllocateDirectAligned(MessageLength, BitUtil.CACHE_LINE_LENGTH))
                using (var atomicBuffer = new UnsafeBuffer(byteBuffer))
                {
                    Latch.Wait();

                    for (var i = 0; i < WarmupNumberOfIterations; i++)
                    {
                        RoundTripMessages(atomicBuffer, fragmentAssembler.OnFragment, publication, subscription, WarmupNumberOfMessages);
                    }

                    Thread.Sleep(100);

                    do
                    {
                        Histogram.Reset();
                        Console.WriteLine("Pinging " + NumberOfMessages + " messages");

                        RoundTripMessages(atomicBuffer, fragmentAssembler.OnFragment, publication, subscription, NumberOfMessages);
                        Console.WriteLine("Histogram of RTT latencies in microseconds.");

                        Histogram.OutputPercentileDistribution(Console.Out, outputValueUnitScalingRatio: 1000);
                    } while (Console.Read() == 'y');
                }
            }
        }
Exemplo n.º 23
0
 public Resampler(double inputSampleRate, double outputSampleRate, int taps)
 {
     DoubleToFraction(outputSampleRate / inputSampleRate, out _interpolationFactor, out _decimationFactor);
     var filterLenght = (int) (500.0 / 32000 * inputSampleRate) / _interpolationFactor * _interpolationFactor;
     _tapsPerPhase = filterLenght / _interpolationFactor;
     _firKernelBuffer = UnsafeBuffer.Create(filterLenght, sizeof(float));
     _firKernel = (float*) _firKernelBuffer;
     var cutoff = Math.Min(inputSampleRate, outputSampleRate) * ProtectedPassband;
     var kernel = FilterBuilder.MakeLowPassKernel(inputSampleRate * _interpolationFactor, filterLenght - 1, cutoff, WindowType.BlackmanHarris4);
     fixed (float* ptr = kernel)
     {
         for (var i = 0; i < kernel.Length; i++)
         {
             ptr[i] *= _interpolationFactor;
         }
         Utils.Memcpy(_firKernel, ptr, filterLenght * sizeof(float));
     }
     _firQueueBuffer = UnsafeBuffer.Create(filterLenght, sizeof(float));
     _firQueue = (float*) _firQueueBuffer;
 }
Exemplo n.º 24
0
 static unsafe Trig()
 {
     int length = Trig._mask + 1;
       Trig._sinBuffer = UnsafeBuffer.Create(length, 4);
       Trig._cosBuffer = UnsafeBuffer.Create(length, 4);
       Trig._sinPtr = (float*) (void*) Trig._sinBuffer;
       Trig._cosPtr = (float*) (void*) Trig._cosBuffer;
       Trig._indexScale = (float) length / 6.283185f;
       for (int index = 0; index < length; ++index)
       {
     Trig._sinPtr[index] = (float) Math.Sin(((double) index + 0.5) / (double) length * 6.28318548202515);
     Trig._cosPtr[index] = (float) Math.Cos(((double) index + 0.5) / (double) length * 6.28318548202515);
       }
       float num = 0.0f;
       while ((double) num < 6.28318548202515)
       {
     Trig._sinPtr[(int) ((double) num * (double) Trig._indexScale) & Trig._mask] = (float) Math.Sin((double) num);
     Trig._cosPtr[(int) ((double) num * (double) Trig._indexScale) & Trig._mask] = (float) Math.Cos((double) num);
     num += 1.570796f;
       }
 }
Exemplo n.º 25
0
        public FftProcessor(int fftSize)
        {            
            _fftSize = fftSize;
            _halfSize = fftSize / 2;
            _overlapSize = (int) Math.Ceiling(_fftSize * OverlapRatio);
            _fftBufferPos = _halfSize;

            _blendFactor = 1.0f / _overlapSize;

            _fftBuffer = UnsafeBuffer.Create(fftSize, sizeof(Complex));
            _fftBufferPtr = (Complex*)_fftBuffer;

            _outOverlapBuffer = UnsafeBuffer.Create(_overlapSize, sizeof(float));
            _outOverlapPtr = (float*) _outOverlapBuffer;

            _overlapBuffer = UnsafeBuffer.Create(fftSize / 2, sizeof(float));
            _overlapBufferPtr = (float*)_overlapBuffer;

            _sampleBuffer = UnsafeBuffer.Create(fftSize, sizeof(float));
            _sampleBufferPtr = (float*)_sampleBuffer;
            _sampleBufferHead = _halfSize;
        }
Exemplo n.º 26
0
        public void ShouldWriteToEmptyBuffer()
        {
            const int length = 8;
            var recordLength = length + RecordDescriptor.HeaderLength;
            var alignedRecordLength = BitUtil.Align(recordLength, RecordDescriptor.Alignment);
            const long tail = 0L;
            const long head = 0L;

            A.CallTo(() => _buffer.GetLongVolatile(HeadCounterIndex)).Returns(head);
            A.CallTo(() => _buffer.GetLongVolatile(TailCounterIndex)).Returns(tail);
            A.CallTo(() => _buffer.CompareAndSetLong(TailCounterIndex, tail, tail + alignedRecordLength)).Returns(true);


            var srcBuffer = new UnsafeBuffer(new byte[1024]);
            const int srcIndex = 0;

            Assert.True(_ringBuffer.Write(MsgTypeID, srcBuffer, srcIndex, length));

            A.CallTo(() => _buffer.PutLongOrdered((int) tail, RecordDescriptor.MakeHeader(-recordLength, MsgTypeID))).MustHaveHappened()
                .Then(A.CallTo(() => _buffer.PutBytes(RecordDescriptor.EncodedMsgOffset((int) tail), srcBuffer, srcIndex, length)).MustHaveHappened())
                .Then(A.CallTo(() => _buffer.PutIntOrdered(RecordDescriptor.LengthOffset((int) tail), recordLength)).MustHaveHappened());
        }
Exemplo n.º 27
0
 public unsafe Resampler(double inputSampleRate, double outputSampleRate, int tapsPerPhase = 160, double protectedPassband = 0.45)
 {
     Resampler.DoubleToFraction(outputSampleRate / inputSampleRate, out this._interpolationFactor, out this._decimationFactor);
       this._tapsPerPhase = tapsPerPhase;
       int length = tapsPerPhase * this._interpolationFactor;
       this._firKernelBuffer = UnsafeBuffer.Create(length, 4);
       this._firKernel = (float*) (void*) this._firKernelBuffer;
       double cutoffFrequency = Math.Min(inputSampleRate, outputSampleRate) * protectedPassband;
       float[] numArray = FilterBuilder.MakeLowPassKernel(inputSampleRate * (double) this._interpolationFactor, length - 1, cutoffFrequency, WindowType.BlackmanHarris4);
       fixed (float* numPtr = numArray)
       {
     for (int index = 0; index < numArray.Length; ++index)
     {
       IntPtr num1 = (IntPtr) (numPtr + index);
       double num2 = (double) *(float*) num1 * (double) this._interpolationFactor;
       *(float*) num1 = (float) num2;
     }
     Utils.Memcpy((void*) this._firKernel, (void*) numPtr, length * 4);
       }
       this._firQueueBuffer = UnsafeBuffer.Create(length, 4);
       this._firQueue = (float*) (void*) this._firQueueBuffer;
 }
Exemplo n.º 28
0
 public unsafe OverlapAddProcessor(int fftSize)
 {
     this._fftSize = fftSize;
       this._halfSize = this._fftSize / 2;
       this._inputPos = this._halfSize;
       this._queuepBuffer = UnsafeBuffer.Create(this._fftSize, sizeof (Complex));
       this._queuePtr = (Complex*) (void*) this._queuepBuffer;
       this._windowBuffer = UnsafeBuffer.Create(this._fftSize, 4);
       this._windowPtr = (float*) (void*) this._windowBuffer;
       this._fftBuffer = UnsafeBuffer.Create(this._fftSize, sizeof (Complex));
       this._fftPtr = (Complex*) (void*) this._fftBuffer;
       this._outputBuffer = UnsafeBuffer.Create(this._halfSize, sizeof (Complex));
       this._outputPtr = (Complex*) (void*) this._outputBuffer;
       this._overlapBuffer = UnsafeBuffer.Create(this._halfSize, sizeof (Complex));
       this._overlapPtr = (Complex*) (void*) this._overlapBuffer;
       double num = Math.PI / 2.0 / (double) (this._halfSize - 1);
       for (int index = 0; index < this._halfSize; ++index)
       {
     double a = (double) index * num;
     this._windowPtr[index] = (float) Math.Sin(a);
     this._windowPtr[this._fftSize - 1 - index] = this._windowPtr[index];
       }
 }
Exemplo n.º 29
0
        /**
         * Extracts image pixels into byte array "pixels"
         */
        protected unsafe void GetImagePixels()
        {
            int w = image.Width;
            int h = image.Height;

            //        int type = image.GetType().;
            if (w != width ||
                h != height
                )
            {
                // create new image with right size/format
                Image temp =
                    new Bitmap(width, height);
                Graphics g = Graphics.FromImage(temp);
                g.DrawImage(image, 0, 0);
                image = temp;
                g.Dispose();
            }

            pixels = new UnsafeBuffer(3 * image.Width * image.Height);
            byte * addr       = (byte *)pixels.Address;
            Bitmap tempBitmap = new Bitmap(image);

            for (int th = 0; th < image.Height; th++)
            {
                for (int tw = 0; tw < image.Width; tw++)
                {
                    Color color  = tempBitmap.GetPixel(tw, th);
                    *     addr++ = color.R;
                    *     addr++ = color.G;
                    *     addr++ = color.B;
                }
            }

            //pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        }
Exemplo n.º 30
0
        public void SetUp()
        {
            _sendBuffer          = new byte[SendBufferCapacity];
            _atomicSendBuffer    = new UnsafeBuffer(_sendBuffer);
            _logMetaDataBuffer   = new UnsafeBuffer(new byte[LogBufferDescriptor.LOG_META_DATA_LENGTH]);
            _termBuffers         = new UnsafeBuffer[LogBufferDescriptor.PARTITION_COUNT];
            _termMetaDataBuffers = new UnsafeBuffer[LogBufferDescriptor.PARTITION_COUNT];
            _buffers             = new UnsafeBuffer[LogBufferDescriptor.PARTITION_COUNT * 2 + 1];

            _conductor        = A.Fake <ClientConductor>();
            _logBuffers       = A.Fake <LogBuffers>();
            _publicationLimit = A.Fake <IReadablePosition>();

            A.CallTo(() => _publicationLimit.Volatile).Returns(2 * SendBufferCapacity);
            A.CallTo(() => _logBuffers.AtomicBuffers()).Returns(_buffers);
            A.CallTo(() => _logBuffers.TermLength()).Returns(LogBufferDescriptor.TERM_MIN_LENGTH);

            LogBufferDescriptor.InitialTermId(_logMetaDataBuffer, TermID1);
            LogBufferDescriptor.TimeOfLastStatusMessage(_logMetaDataBuffer, 0);

            for (var i = 0; i < LogBufferDescriptor.PARTITION_COUNT; i++)
            {
                _termBuffers[i]         = new UnsafeBuffer(new byte[LogBufferDescriptor.TERM_MIN_LENGTH]);
                _termMetaDataBuffers[i] = new UnsafeBuffer(new byte[LogBufferDescriptor.TERM_META_DATA_LENGTH]);

                _buffers[i] = _termBuffers[i];
                _buffers[i + LogBufferDescriptor.PARTITION_COUNT] = _termMetaDataBuffers[i];
            }
            _buffers[LogBufferDescriptor.LOG_META_DATA_SECTION_INDEX] = _logMetaDataBuffer;

            _publication = new Publication(_conductor, Channel, StreamID1, SessionID1, _publicationLimit, _logBuffers, CorrelationID);

            _publication.IncRef();

            LogBufferDescriptor.InitialiseTailWithTermId(_termMetaDataBuffers[0], TermID1);
        }
Exemplo n.º 31
0
        public static void RemoveAt(UnsafeList *list, int index)
        {
            UDebug.Assert(list != null);
            UDebug.Assert(list->_items.Ptr != null);

            var count = list->_count;

            // cast to uint trick, which eliminates < 0 check
            if ((uint)index >= (uint)count)
            {
                throw new IndexOutOfRangeException(ThrowHelper.ArgumentOutOfRange_Index);
            }

            // reduce count
            list->_count = --count;

            // if index is still less than count, it means we removed an item
            // not at the end of the list, and that we have to shift the items
            // down from (index+1, count-index) to (index, count-index)
            if (index < count)
            {
                UnsafeBuffer.Move(list->_items, index + 1, index, count - index);
            }
        }
Exemplo n.º 32
0
        public static bool IsActive(MappedByteBuffer cncByteBuffer, IEpochClock epochClock, long timeoutMs,
                                    int versionFieldOffset, int timestampFieldOffset, Action <int> versionCheck, Action <string> logger)
        {
            if (null == cncByteBuffer)
            {
                return(false);
            }

            UnsafeBuffer cncBuffer = new UnsafeBuffer(cncByteBuffer);

            long startTimeMs = epochClock.Time();
            int  cncVersion;

            while (0 == (cncVersion = cncBuffer.GetIntVolatile(versionFieldOffset)))
            {
                if (epochClock.Time() > (startTimeMs + timeoutMs))
                {
                    throw new System.InvalidOperationException("CnC file is created but not initialised.");
                }

                Sleep(1);
            }

            versionCheck(cncVersion);

            long timestamp    = cncBuffer.GetLongVolatile(timestampFieldOffset);
            long now          = epochClock.Time();
            long timestampAge = now - timestamp;

            if (null != logger)
            {
                logger("INFO: heartbeat is (ms): " + timestampAge);
            }

            return(timestampAge <= timeoutMs);
        }
Exemplo n.º 33
0
        public ClusterMarkFile(DirectoryInfo directory, string filename, IEpochClock epochClock, long timeoutMs,
                               Action <string> logger)
        {
            markFile = new MarkFile(
                directory,
                filename,
                MarkFileHeaderDecoder.VersionEncodingOffset(),
                MarkFileHeaderDecoder.ActivityTimestampEncodingOffset(),
                timeoutMs,
                epochClock,
                (version) =>
            {
                if (SemanticVersion.Major(version) != MAJOR_VERSION)
                {
                    throw new ClusterException("mark file major version " + SemanticVersion.Major(version) +
                                               " does not match software:" + AeronCluster.Configuration.PROTOCOL_MAJOR_VERSION);
                }
            },
                logger);

            buffer = markFile.Buffer();
            headerDecoder.Wrap(buffer, 0, MarkFileHeaderDecoder.BLOCK_LENGTH, MarkFileHeaderDecoder.SCHEMA_VERSION);
            errorBuffer = new UnsafeBuffer(buffer, headerDecoder.HeaderLength(), headerDecoder.ErrorBufferLength());
        }
Exemplo n.º 34
0
 public STDTNode(VoidPtr address, int numEntries)
 {
     version = 1;
     unk1    = 0;
     unk2    = 0;
     entries = new UnsafeBuffer((numEntries * 4));
     if (address == null)
     {
         byte *pOut = (byte *)entries.Address;
         for (int i = 0; i < (numEntries * 4); i++)
         {
             *pOut++ = 0;
         }
     }
     else
     {
         byte *pIn  = (byte *)address;
         byte *pOut = (byte *)entries.Address;
         for (int i = 0; i < (numEntries * 4); i++)
         {
             *pOut++ = *pIn++;
         }
     }
 }
        int          _slotOffset; // Readonly

        /// <summary>
        /// Allocates a new SPSCRingbuffer. Capacity will be set to a power of 2.
        /// </summary>
        public static UnsafeMPSCQueue *Allocate <T>(int capacity) where T : unmanaged
        {
            if (capacity < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(capacity), string.Format(ThrowHelper.ArgumentOutOfRange_MustBePositive, nameof(capacity)));
            }

            capacity = Memory.RoundUpToPowerOf2(capacity);

            // Required to get the memory size of the Slot + Value
            int slotStride = Marshal.SizeOf(new QueueSlot <T>());
            int slotAlign  = Memory.GetMaxAlignment(sizeof(T), sizeof(int));
            int slotOffset = Memory.RoundToAlignment(sizeof(T), slotAlign);

            int alignment = Memory.GetAlignment(slotStride);

            var sizeOfQueue = Memory.RoundToAlignment(sizeof(UnsafeMPSCQueue), alignment);
            var sizeOfArray = slotStride * capacity;

            var ptr = Memory.MallocAndZero(sizeOfQueue + sizeOfArray, alignment);

            UnsafeMPSCQueue *queue = (UnsafeMPSCQueue *)ptr;

            // initialize fixed buffer from same block of memory as the stack
            UnsafeBuffer.InitFixed(&queue->_items, (byte *)ptr + sizeOfQueue, capacity, slotStride);

            // Read-only values
            queue->_mask       = capacity - 1;
            queue->_slotOffset = slotOffset;
            queue->_typeHandle = typeof(T).TypeHandle.Value;

            // Reset the queue for use.
            Clear(queue);

            return(queue);
        }
Exemplo n.º 36
0
        /**
         * Flushes any pending data and closes output file.
         * If writing to an OutputStream, the stream is not
         * closed.
         */
        public bool Finish()
        {
            if (!started)
            {
                return(false);
            }

            bool ok = true;

            started = false;
            try
            {
                fs.WriteByte(0x3b); // gif trailer
                fs.Flush();
                if (closeStream)
                {
                    fs.Close();
                }
            }
            catch (IOException)
            {
                ok = false;
            }

            // reset for subsequent use
            transIndex    = 0;
            fs            = null;
            image         = null;
            pixels        = null;
            indexedPixels = null;
            colorTab      = null;
            closeStream   = false;
            firstFrame    = true;

            return(ok);
        }
Exemplo n.º 37
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SerializedObjectReader"/> class based on the specified input stream, output stream and configuration.
        /// </summary>
        /// <param name="input">The input stream.</param>
        /// <param name="output">The output stream.</param>
        /// <param name="configuration">The configuration parameters to use for the reader.</param>
        /// <param name="label">The memory allocator label to use.</param>
        /// <param name="leaveInputOpen">True to leave the input stream open after the reader object is disposed; otherwise, false.</param>
        /// <param name="leaveOutputOpen">True to leave the output stream open after the reader object is disposed; otherwise, false.</param>
        /// <exception cref="ArgumentException">The configuration is invalid.</exception>
        public SerializedObjectReader(Stream input, PackedBinaryStream output, SerializedObjectReaderConfiguration configuration, Allocator label = SerializationConfiguration.DefaultAllocatorLabel, bool leaveInputOpen = true, bool leaveOutputOpen = true)
        {
            if (configuration.BlockBufferSize < 16)
            {
                throw new ArgumentException("BlockBufferSize < 16");
            }

            if (configuration.TokenBufferSize < 16)
            {
                throw new ArgumentException("TokenBufferSize < 16");
            }

            m_LeaveOutputOpen = leaveOutputOpen;

            m_StreamBlockReader = configuration.UseReadAsync
                ? (IUnsafeStreamBlockReader) new AsyncBlockReader(input, configuration.BlockBufferSize, leaveInputOpen)
                : (IUnsafeStreamBlockReader) new SyncBlockReader(input, configuration.BlockBufferSize, leaveInputOpen);

            m_Tokenizer    = new JsonTokenizer(configuration.TokenBufferSize, configuration.ValidationType, label);
            m_Parser       = new NodeParser(m_Tokenizer, configuration.NodeBufferSize, label);
            m_BinaryStream = output;
            m_BinaryWriter = new PackedBinaryWriter(m_BinaryStream, m_Tokenizer, label);
            m_Block        = default;
        }
Exemplo n.º 38
0
        public int AppendUnfragmentedMessage(HeaderWriter header, IDirectBuffer srcBuffer, int srcOffset, int length, ReservedValueSupplier reservedValueSupplier, int activeTermId)
#endif
        {
            int          frameLength   = length + DataHeaderFlyweight.HEADER_LENGTH;
            int          alignedLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            long         rawTail       = GetAndAddRawTail(alignedLength);
            int          termId        = LogBufferDescriptor.TermId(rawTail);
            long         termOffset    = rawTail & 0xFFFFFFFFL;
            UnsafeBuffer termBuffer    = _termBuffer;
            int          termLength    = termBuffer.Capacity;

            CheckTerm(activeTermId, termId);

            long resultingOffset = termOffset + alignedLength;

            if (resultingOffset > termLength)
            {
                resultingOffset = HandleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
            }
            else
            {
                int frameOffset = (int)termOffset;
                header.Write(termBuffer, frameOffset, frameLength, LogBufferDescriptor.TermId(rawTail));
                termBuffer.PutBytes(frameOffset + DataHeaderFlyweight.HEADER_LENGTH, srcBuffer, srcOffset, length);

                if (null != reservedValueSupplier)
                {
                    long reservedValue = reservedValueSupplier(termBuffer, frameOffset, frameLength);
                    termBuffer.PutLong(frameOffset + DataHeaderFlyweight.RESERVED_VALUE_OFFSET, reservedValue, ByteOrder.LittleEndian);
                }

                FrameDescriptor.FrameLengthOrdered(termBuffer, frameOffset, frameLength);
            }

            return((int)resultingOffset);
        }
Exemplo n.º 39
0
        public static UnsafeBuffer Decode(MDL0NormalData *header)
        {
            int            count   = header->_numVertices;
            float          scale   = VQuant.DeQuantTable[header->_divisor]; //Should always be zero?
            int            type    = (int)ElementCodec.CodecType.XYZ + header->_type;
            ElementDecoder decoder = ElementCodec.Decoders[type];
            UnsafeBuffer   buffer;

            if (header->_isNBT != 0)
            {
                count *= 3; //Format is the same, just with three Vectors each
            }

            buffer = new UnsafeBuffer(count * 12);

            byte *pIn = (byte *)header->Data, pOut = (byte *)buffer.Address;

            for (int i = 0; i < count; i++)
            {
                decoder(ref pIn, ref pOut, scale);
            }

            return(buffer);
        }
Exemplo n.º 40
0
        public virtual void ShouldAssembleTwoPartMessage()
        {
            A.CallTo(() => header.Flags).ReturnsNextFromSequence(FrameDescriptor.BEGIN_FRAG_FLAG, FrameDescriptor.END_FRAG_FLAG, FrameDescriptor.END_FRAG_FLAG);
            // Need to add this twice because FakeItEasy doesn't fall back to the implementation

            var srcBuffer = new UnsafeBuffer(new byte[1024]);
            const int offset = 0;
            var length = srcBuffer.Capacity/2;

            srcBuffer.SetMemory(0, length, 65);
            srcBuffer.SetMemory(length, length, 66);

            adapter.OnFragment(srcBuffer, offset, length, header);
            adapter.OnFragment(srcBuffer, length, length, header);

            Func<UnsafeBuffer, bool> bufferAssertion = capturedBuffer =>
            {
                for (var i = 0; i < srcBuffer.Capacity; i++)
                {
                    if (capturedBuffer.GetByte(i) != srcBuffer.GetByte(i))
                    {
                        return false;
                    }
                }
                return true;
            };

            Func<Header, bool> headerAssertion = capturedHeader => capturedHeader.SessionId == SESSION_ID &&
                                                                   capturedHeader.Flags == FrameDescriptor.END_FRAG_FLAG;
            A.CallTo(() => delegateFragmentHandler(
                A<UnsafeBuffer>.That.Matches(bufferAssertion, "buffer"),
                offset,
                length*2,
                A<Header>.That.Matches(headerAssertion, "header")))
                .MustHaveHappened(Repeated.Exactly.Once);
        }
Exemplo n.º 41
0
 public TBGDNode(VoidPtr address, int numEntries)
 {
     unk0    = 39;           // Default set in Smashville
     unk1    = 0;
     unk2    = 0;
     entries = new UnsafeBuffer((numEntries * 4));
     if (address == null)
     {
         byte *pOut = (byte *)entries.Address;
         for (int i = 0; i < (numEntries * 4); i++)
         {
             *pOut++ = 0;
         }
     }
     else
     {
         byte *pIn  = (byte *)address;
         byte *pOut = (byte *)entries.Address;
         for (int i = 0; i < (numEntries * 4); i++)
         {
             *pOut++ = *pIn++;
         }
     }
 }
Exemplo n.º 42
0
 public unsafe OverlapCrossfadeProcessor(int fftSize, float crossFadingRatio = 0.0f)
 {
     this._fftSize = fftSize;
       this._halfSize = this._fftSize / 2;
       this._crossFadingSize = (int) ((double) this._halfSize * (double) crossFadingRatio);
       this._outputSize = this._halfSize - this._crossFadingSize;
       this._inputPos = this._halfSize + this._crossFadingSize;
       this._queuepBuffer = UnsafeBuffer.Create(this._fftSize, sizeof (Complex));
       this._queuePtr = (Complex*) (void*) this._queuepBuffer;
       this._windowBuffer = UnsafeBuffer.Create(this._crossFadingSize, 4);
       this._windowPtr = (float*) (void*) this._windowBuffer;
       this._fftBuffer = UnsafeBuffer.Create(this._fftSize, sizeof (Complex));
       this._fftPtr = (Complex*) (void*) this._fftBuffer;
       this._outputBuffer = UnsafeBuffer.Create(this._outputSize, sizeof (Complex));
       this._outputPtr = (Complex*) (void*) this._outputBuffer;
       this._crossFadingBuffer = UnsafeBuffer.Create(this._crossFadingSize, sizeof (Complex));
       this._crossFadingPtr = (Complex*) (void*) this._crossFadingBuffer;
       double num = Math.PI / 2.0 / (double) (this._crossFadingSize - 1);
       for (int index = 0; index < this._crossFadingSize; ++index)
       {
     double a = (double) index * num;
     this._windowPtr[index] = (float) Math.Pow(Math.Sin(a), 2.0);
       }
 }
Exemplo n.º 43
0
        public ElementDescriptor(MDL0Polygon *polygon)
        {
            MDL0Header *model = (MDL0Header *)((byte *)polygon + polygon->_mdl0Offset);
            byte *      pData = (byte *)polygon->DefList;
            byte *      pCom;
            ElementDef *pDef;
            int         fmtLo, fmtHi;
            int         grp0, grp1, grp2;
            int         format;

            //Create remap table for vertex weights
            RemapTable = new UnsafeBuffer(polygon->_numVertices * 4);
            RemapSize  = 0;
            Stride     = 0;

            //Read element descriptor from polygon display list
            //Use direct access instead!
            //May change depending on file version

            fmtLo = *(bint *)(pData + 12);
            fmtHi = *(bint *)(pData + 18);
            grp0  = *(bint *)(pData + 34);
            grp1  = *(bint *)(pData + 40);
            grp2  = *(bint *)(pData + 46);

            //grp1 = *(buint*)(pData + 40);
            //grp1 |= (ulong)(*(buint*)(pData + 46)) << 32;


            //Build extract script.
            //What we're doing is assigning extract commands for elements in the polygon, in true order.
            //This allows us to process the polygon blindly, assuming that the definition is accurate.
            //Theoretically, this should offer a significant speed bonus.
            fixed(int *pDefData = Defs)
            fixed(byte *pComData = Commands)
            {
                pCom = pComData;
                pDef = (ElementDef *)pDefData;

                //Pos/Norm weight
                if (Weighted = (fmtLo & 1) != 0)
                {
                    *pCom++ = (byte)DecodeOp.PosWeight;
                    Stride++;
                }

                //Tex matrix
                for (int i = 0; i < 8; i++)
                {
                    if (((fmtLo >> (i + 1)) & 1) != 0)
                    {
                        *pCom++ = (byte)(DecodeOp.TexMtx0 + i);
                        Stride++;
                    }
                }

                //Positions
                format = ((fmtLo >> 9) & 3) - 1;
                if (format >= 0)
                {
                    pDef->Input = (byte)format;
                    pDef->Type  = 0;
                    if (format == 0)
                    {
                        throw new NotSupportedException("Direct mode is not suported for polygons!");
                        //pDef->Scale = (byte)((grp0 >> 4) & 0x1F);
                        //pDef->Output = (byte)(((grp0 >> 1) & 0x7) + ((grp0 & 1) == 0 ? ElementCodec.CodecType.XY : ElementCodec.CodecType.XYZ));
                        //pCom[NumCommands++] = (byte)DecodeOp.ElementDirect;
                    }
                    else
                    {
                        Stride      += format;
                        pDef->Output = 12;
                        *pCom++ = (byte)DecodeOp.ElementIndexed;
                    }
                    pDef++;
                }

                //Normals
                format = ((fmtLo >> 11) & 3) - 1;
                if (format >= 0)
                {
                    pDef->Input = (byte)format;
                    pDef->Type  = 1;
                    if (format == 0)
                    {
                        throw new NotSupportedException("Direct mode is not suported for polygons!");
                        //pDef->Scale = 0; //Implied?
                        //pDef->Output = (byte)(((grp0 >> 10) & 0x7) + ((grp0 & (1 << 10)) == 0 ? ElementCodec.CodecType.XYZ : ElementCodec.CodecType.XYZ));
                        //pCom[NumCommands++] = (byte)DecodeOp.ElementDirect;
                    }
                    else
                    {
                        Stride      += format;
                        pDef->Output = 12;
                        *pCom++ = (byte)DecodeOp.ElementIndexed;
                    }
                    pDef++;
                }

                //Colors
                for (int i = 0; i < 2; i++)
                {
                    format = ((fmtLo >> (i * 2 + 13)) & 3) - 1;
                    if (format >= 0)
                    {
                        pDef->Input = (byte)format;
                        pDef->Type  = (byte)(i + 2);
                        if (format == 0)
                        {
                            throw new NotSupportedException("Direct mode is not suported for polygons!");
                            //pDef->Output = (byte)((grp0 >> (i * 4 + 14)) & 7);
                            //pCom[NumCommands++] = (byte)DecodeOp.ElementDirect;
                        }
                        else
                        {
                            Stride      += format;
                            pDef->Output = 4;
                            *pCom++ = (byte)DecodeOp.ElementIndexed;
                        }
                        pDef++;
                    }
                }

                //UVs
                for (int i = 0; i < 8; i++)
                {
                    format = ((fmtHi >> (i * 2)) & 3) - 1;
                    if (format >= 0)
                    {
                        pDef->Input = (byte)format;
                        pDef->Type  = (byte)(i + 4);
                        if (format == 0)
                        {
                            throw new NotSupportedException("Direct mode is not suported for polygons!");
                            //Needs work!
                            //if (i == 0)
                            //{
                            //    pDef->Output = (byte)(((grp0 >> 22) & 7) + ((grp0 & 22) == 0 ? ElementCodec.CodecType.S : ElementCodec.CodecType.ST));
                            //    pDef->Scale = (byte)((grp0 >> 25) & 0x1F);
                            //}
                            //else
                            //{
                            //    pDef->Output = (byte)((int)((grp1 >> (i * 9 + 1)) & 7) + ((grp1 & ((ulong)1 << (i * 9 + 1))) == 0 ? ElementCodec.CodecType.S : ElementCodec.CodecType.ST));
                            //    pDef->Scale = (byte)((grp1 >> (i * 9 + 4)) & 0x1F);
                            //}
                            //pCom[NumCommands++] = (byte)DecodeOp.ElementDirect;
                        }
                        else
                        {
                            Stride      += format;
                            pDef->Output = 8;
                            *pCom++ = (byte)DecodeOp.ElementIndexed;
                        }
                        pDef++;
                    }
                }

                *pCom = 0;
            }
        }
Exemplo n.º 44
0
        private void PatchPointers()
        {
            _buffer?.Dispose();

            //Make a copy of the file's data that we can patch with offsets
            _buffer = new UnsafeBuffer(WorkingUncompressed.Length);
            Memory.Move(_buffer.Address, WorkingUncompressed.Address, (uint)WorkingUncompressed.Length);

            HKXHeader *           header  = (HKXHeader *)_buffer.Address;
            PhysicsOffsetSection *section = header->OffsetSections;

            for (int i = 0; i < header->_sectionCount; i++, section++)
            {
                int     dataOffset = section->_dataOffset;
                VoidPtr data = _buffer.Address + dataOffset;
                int     local = section->LocalPatchesLength, global = section->GlobalPatchesLength;

                if (section->ExportsLength > 0)
                {
                    Console.WriteLine("Has exports");
                }

                if (section->ImportsLength > 0)
                {
                    Console.WriteLine("Has imports");
                }

                //Global patches have to be made before local ones
                if (global > 0)
                {
                    //Global patches set offsets from this section to data in another section (or this one)
                    VoidPtr      start = data + section->_globalPatchesOffset;
                    GlobalPatch *patch = (GlobalPatch *)start;
                    while ((int)patch - (int)start < global && patch->_dataOffset >= 0 && patch->_pointerOffset >= 0)
                    {
                        //Make the pointer offset relative to itself so it's self-contained
                        int   ptrOffset = patch->_pointerOffset;
                        bint *ptr       = (bint *)(data + ptrOffset);
                        PhysicsOffsetSection *otherSection = &header->OffsetSections[patch->_sectionIndex];
                        int dOffset = patch->_dataOffset + otherSection->_dataOffset - dataOffset;
                        int offset  = dOffset - ptrOffset;
                        *   ptr     = offset;
                        patch++;
                    }
                }

                if (local > 0)
                {
                    //Local patches set offsets to data located elsewhere in this section
                    VoidPtr     start = data + section->_localPatchesOffset;
                    LocalPatch *patch = (LocalPatch *)start;
                    while ((int)patch - (int)start < local && patch->_dataOffset >= 0)
                    {
                        //Make the pointer offset relative to itself so it's self-contained
                        int   ptrOffset = patch->_pointerOffset;
                        bint *ptr       = (bint *)(data + ptrOffset);
                        *     ptr       = patch->_dataOffset - ptrOffset;
                        patch++;
                    }
                }
            }
        }
Exemplo n.º 45
0
        public int AppendFragmentedMessage(
            int termId,
            int termOffset,
            HeaderWriter header,
            DirectBufferVector[] vectors,
            int length,
            int maxPayloadLength,
            ReservedValueSupplier reservedValueSupplier)
        {
            int numMaxPayloads   = length / maxPayloadLength;
            int remainingPayload = length % maxPayloadLength;
            int lastFrameLength  = remainingPayload > 0
                ? BitUtil.Align(remainingPayload + DataHeaderFlyweight.HEADER_LENGTH, FrameDescriptor.FRAME_ALIGNMENT)
                : 0;
            int requiredLength = (numMaxPayloads * (maxPayloadLength + DataHeaderFlyweight.HEADER_LENGTH)) +
                                 lastFrameLength;
            UnsafeBuffer termBuffer = _termBuffer;
            int          termLength = termBuffer.Capacity;

            int resultingOffset = termOffset + requiredLength;

            PutRawTailOrdered(termId, resultingOffset);

            if (resultingOffset > termLength)
            {
                resultingOffset = HandleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
            }
            else
            {
                int  frameOffset  = termOffset;
                byte flags        = FrameDescriptor.BEGIN_FRAG_FLAG;
                int  remaining    = length;
                int  vectorIndex  = 0;
                int  vectorOffset = 0;

                do
                {
                    int bytesToWrite  = Math.Min(remaining, maxPayloadLength);
                    int frameLength   = bytesToWrite + DataHeaderFlyweight.HEADER_LENGTH;
                    int alignedLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);

                    header.Write(termBuffer, frameOffset, frameLength, termId);

                    int bytesWritten  = 0;
                    int payloadOffset = frameOffset + DataHeaderFlyweight.HEADER_LENGTH;

                    do
                    {
                        var vector          = vectors[vectorIndex];
                        int vectorRemaining = vector.length - vectorOffset;
                        int numBytes        = Math.Min(bytesToWrite - bytesWritten, vectorRemaining);

                        termBuffer.PutBytes(payloadOffset, vector.buffer, vector.offset + vectorOffset, numBytes);

                        bytesWritten  += numBytes;
                        payloadOffset += numBytes;
                        vectorOffset  += numBytes;

                        if (vectorRemaining <= numBytes)
                        {
                            vectorIndex++;
                            vectorOffset = 0;
                        }
                    } while (bytesWritten < bytesToWrite);

                    if (remaining <= maxPayloadLength)
                    {
                        flags |= FrameDescriptor.END_FRAG_FLAG;
                    }

                    FrameDescriptor.FrameFlags(termBuffer, frameOffset, flags);

                    if (null != reservedValueSupplier)
                    {
                        long reservedValue = reservedValueSupplier(termBuffer, frameOffset, frameLength);
                        termBuffer.PutLong(frameOffset + DataHeaderFlyweight.RESERVED_VALUE_OFFSET, reservedValue, ByteOrder.LittleEndian);
                    }

                    FrameDescriptor.FrameLengthOrdered(termBuffer, frameOffset, frameLength);

                    flags        = 0;
                    frameOffset += alignedLength;
                    remaining   -= bytesToWrite;
                } while (remaining > 0);
            }

            return(resultingOffset);
        }
Exemplo n.º 46
0
        /// <summary>
        ///     Compare and set the raw value of the tail for the given partition.
        /// </summary>
        /// <param name="metaDataBuffer"> containing the tail counters. </param>
        /// <param name="partitionIndex">    for the tail counter. </param>
        /// <param name="expectedRawTail">   expected current value. </param>
        /// <param name="updateRawTail">     to be applied. </param>
        /// <returns> true if the update was successful otherwise false. </returns>
        public static bool CasRawTail(UnsafeBuffer metaDataBuffer, int partitionIndex, long expectedRawTail, long updateRawTail)
        {
            var index = TERM_TAIL_COUNTERS_OFFSET + BitUtil.SIZE_OF_LONG * partitionIndex;

            return(metaDataBuffer.CompareAndSetLong(index, expectedRawTail, updateRawTail));
        }
Exemplo n.º 47
0
        public void SetUp()
        {
            MockClientErrorHandler = A.Fake <ErrorHandler>(options => options.Wrapping(throwable =>
            {
                if (!SuppressPrintError)
                {
                    Console.WriteLine(throwable.ToString());
                    Console.Write(throwable.StackTrace);
                }
            }));

            PublicationReady  = new PublicationBuffersReadyFlyweight();
            CorrelatedMessage = new CorrelatedMessageFlyweight();
            ErrorResponse     = new ErrorResponseFlyweight();

            PublicationReadyBuffer  = new UnsafeBuffer(new byte[SEND_BUFFER_CAPACITY]);
            CorrelatedMessageBuffer = new UnsafeBuffer(new byte[SEND_BUFFER_CAPACITY]);
            ErrorMessageBuffer      = new UnsafeBuffer(new byte[SEND_BUFFER_CAPACITY]);
            CounterValuesBuffer     = new UnsafeBuffer(new byte[COUNTER_BUFFER_LENGTH]);
            MockToClientReceiver    = A.Fake <CopyBroadcastReceiver>();

            MockAvailableImageHandler   = A.Fake <AvailableImageHandler>();
            MockUnavailableImageHandler = A.Fake <UnavailableImageHandler>();

            LogBuffersFactory = A.Fake <ILogBuffersFactory>();

            SubscriberPositionMap = new Dictionary <long, long>(); // should return -1 when element does not exist

            DriverProxy = A.Fake <DriverProxy>();

            A.CallTo(() => DriverProxy.AddPublication(CHANNEL, STREAM_ID_1)).Returns(CORRELATION_ID);
            A.CallTo(() => DriverProxy.AddPublication(CHANNEL, STREAM_ID_2)).Returns(CORRELATION_ID_2);
            A.CallTo(() => DriverProxy.RemovePublication(CORRELATION_ID)).Returns(CLOSE_CORRELATION_ID);
            A.CallTo(() => DriverProxy.AddSubscription(A <string> ._, A <int> ._)).Returns(CORRELATION_ID);
            A.CallTo(() => DriverProxy.RemoveSubscription(CORRELATION_ID)).Returns(CLOSE_CORRELATION_ID);

            Conductor = new ClientConductor(EpochClock, NanoClock, MockToClientReceiver, LogBuffersFactory, CounterValuesBuffer, DriverProxy, MockClientErrorHandler, MockAvailableImageHandler, MockUnavailableImageHandler, MapMode.ReadOnly, KEEP_ALIVE_INTERVAL, AWAIT_TIMEOUT, NanoUtil.FromMilliseconds(INTER_SERVICE_TIMEOUT_MS), PUBLICATION_CONNECTION_TIMEOUT_MS);

            PublicationReady.Wrap(PublicationReadyBuffer, 0);
            CorrelatedMessage.Wrap(CorrelatedMessageBuffer, 0);
            ErrorResponse.Wrap(ErrorMessageBuffer, 0);

            PublicationReady.CorrelationId(CORRELATION_ID);
            PublicationReady.SessionId(SESSION_ID_1);
            PublicationReady.StreamId(STREAM_ID_1);
            PublicationReady.LogFileName(SESSION_ID_1 + "-log");

            SubscriberPositionMap.Add(CORRELATION_ID, 0);

            CorrelatedMessage.CorrelationId(CLOSE_CORRELATION_ID);

            var termBuffersSession1 = new UnsafeBuffer[LogBufferDescriptor.PARTITION_COUNT];
            var termBuffersSession2 = new UnsafeBuffer[LogBufferDescriptor.PARTITION_COUNT];

            for (var i = 0; i < LogBufferDescriptor.PARTITION_COUNT; i++)
            {
                termBuffersSession1[i] = new UnsafeBuffer(new byte[TERM_BUFFER_LENGTH]);
                termBuffersSession2[i] = new UnsafeBuffer(new byte[TERM_BUFFER_LENGTH]);
            }

            UnsafeBuffer logMetaDataSession1 = new UnsafeBuffer(new byte[TERM_BUFFER_LENGTH]);
            UnsafeBuffer logMetaDataSession2 = new UnsafeBuffer(new byte[TERM_BUFFER_LENGTH]);

            IMutableDirectBuffer header1 = DataHeaderFlyweight.CreateDefaultHeader(SESSION_ID_1, STREAM_ID_1, 0);
            IMutableDirectBuffer header2 = DataHeaderFlyweight.CreateDefaultHeader(SESSION_ID_2, STREAM_ID_2, 0);

            LogBufferDescriptor.StoreDefaultFrameHeader(logMetaDataSession1, header1);
            LogBufferDescriptor.StoreDefaultFrameHeader(logMetaDataSession2, header2);

            var logBuffersSession1 = A.Fake <LogBuffers>();
            var logBuffersSession2 = A.Fake <LogBuffers>();

            A.CallTo(() => LogBuffersFactory.Map(SESSION_ID_1 + "-log", A <MapMode> ._)).Returns(logBuffersSession1);
            A.CallTo(() => LogBuffersFactory.Map(SESSION_ID_2 + "-log", A <MapMode> ._)).Returns(logBuffersSession2);
            A.CallTo(() => logBuffersSession1.TermBuffers()).Returns(termBuffersSession1);
            A.CallTo(() => logBuffersSession2.TermBuffers()).Returns(termBuffersSession2);

            A.CallTo(() => logBuffersSession1.MetaDataBuffer()).Returns(logMetaDataSession1);
            A.CallTo(() => logBuffersSession2.MetaDataBuffer()).Returns(logMetaDataSession2);
        }
Exemplo n.º 48
0
 /// <summary>
 ///     Set the value of the end of stream position.
 /// </summary>
 /// <param name="metaDataBuffer"> containing the metadata. </param>
 /// <param name="position">          value of the end of stream position </param>
 public static void EndOfStreamPosition(UnsafeBuffer metaDataBuffer, long position)
 {
     metaDataBuffer.PutLongOrdered(LOG_END_OF_STREAM_POSITION_OFFSET, position);
 }
Exemplo n.º 49
0
        public static long RawTailVolatile(UnsafeBuffer metaDataBuffer)
        {
            var partitionIndex = IndexByTermCount(ActiveTermCount(metaDataBuffer));

            return(metaDataBuffer.GetLongVolatile(TERM_TAIL_COUNTERS_OFFSET + BitUtil.SIZE_OF_LONG * partitionIndex));
        }
Exemplo n.º 50
0
 public static void InitialiseTailWithTermId(UnsafeBuffer logMetaData, int partitionIndex, int termId)
 {
     logMetaData.PutLong(TERM_TAIL_COUNTERS_OFFSET + partitionIndex * BitUtil.SIZE_OF_LONG, PackTail(termId, 0));
 }
Exemplo n.º 51
0
 /// <summary>
 ///     Set the raw value of the tail for the given partition.
 /// </summary>
 /// <param name="metaDataBuffer"> containing the tail counters. </param>
 /// <param name="partitionIndex">    for the tail counter. </param>
 /// <param name="rawTail">           to be stored </param>
 public static void RawTailVolatile(UnsafeBuffer metaDataBuffer, int partitionIndex, long rawTail)
 {
     metaDataBuffer.PutLongVolatile(TERM_TAIL_COUNTERS_OFFSET + BitUtil.SIZE_OF_LONG * partitionIndex,
                                    rawTail);
 }
Exemplo n.º 52
0
 public static UnsafeBuffer DefaultFrameHeader(UnsafeBuffer metaDataBuffer)
 {
     return(new UnsafeBuffer(metaDataBuffer, LOG_DEFAULT_FRAME_HEADER_OFFSET,
                             DataHeaderFlyweight.HEADER_LENGTH));
 }
Exemplo n.º 53
0
 public static void ApplyDefaultHeader(UnsafeBuffer metaDataBuffer, UnsafeBuffer termBuffer, int termOffset)
 {
     termBuffer.PutBytes(termOffset, metaDataBuffer, LOG_DEFAULT_FRAME_HEADER_OFFSET,
                         DataHeaderFlyweight.HEADER_LENGTH);
 }
Exemplo n.º 54
0
 /// <summary>
 ///     Set the value of the current active partition index for the producer.
 /// </summary>
 /// <param name="metaDataBuffer"> containing the metadata. </param>
 /// <param name="termCount">         value of the active term count used by the producer of this log. </param>
 public static void ActiveTermCount(UnsafeBuffer metaDataBuffer, int termCount)
 {
     metaDataBuffer.PutInt(LOG_ACTIVE_TERM_COUNT_OFFSET, termCount);
 }
Exemplo n.º 55
0
 /// <summary>
 ///     Compare and set the value of the current active term count.
 /// </summary>
 /// <param name="metaDataBuffer"> containing the metadata. </param>
 /// <param name="expectedTermCount"> value of the active term count expected in the log </param>
 /// <param name="updateTermCount">   value of the active term count to be updated in the log </param>
 /// <returns> true if successful otherwise false. </returns>
 public static bool CasActiveTermCount(UnsafeBuffer metaDataBuffer, int expectedTermCount, int updateTermCount)
 {
     return(metaDataBuffer.CompareAndSetInt(LOG_ACTIVE_TERM_COUNT_OFFSET, expectedTermCount, updateTermCount));
 }
Exemplo n.º 56
0
 /// <summary>
 ///     Get the value of the active term count used by the producer of this log. Consumers may have a different
 ///     active term count if they are running behind. The read is done with volatile semantics.
 /// </summary>
 /// <param name="metaDataBuffer"> containing the metadata. </param>
 /// <returns> the value of the active term count used by the producer of this log. </returns>
 public static int ActiveTermCount(UnsafeBuffer metaDataBuffer)
 {
     return(metaDataBuffer.GetIntVolatile(LOG_ACTIVE_TERM_COUNT_OFFSET));
 }
Exemplo n.º 57
0
        /// <summary>
        /// Peek for new messages in a stream by scanning forward from an initial position. If new messages are found then
        /// they will be delivered to the <seealso cref="IControlledFragmentHandler"/> up to a limited position.
        ///
        /// Use a <seealso cref="ControlledFragmentAssembler"/> to assemble messages which span multiple fragments. Scans must also
        /// start at the beginning of a message so that the assembler is reset.
        ///
        /// </summary>
        /// <param name="initialPosition"> from which to peek forward. </param>
        /// <param name="handler"> to which message fragments are delivered. </param>
        /// <param name="limitPosition">   up to which can be scanned. </param>
        /// <returns> the resulting position after the scan terminates which is a complete message. </returns>
        /// <seealso cref="ControlledFragmentAssembler"/>
        /// <seealso cref="ImageControlledFragmentAssembler"/>
        public long ControlledPeek(long initialPosition, IControlledFragmentHandler handler, long limitPosition)
        {
            if (_isClosed)
            {
                return(initialPosition);
            }

            ValidatePosition(initialPosition);
            if (initialPosition >= limitPosition)
            {
                return(initialPosition);
            }

            int          initialOffset = (int)initialPosition & _termLengthMask;
            int          offset        = initialOffset;
            long         position      = initialPosition;
            UnsafeBuffer termBuffer    = ActiveTermBuffer(initialPosition);
            var          header        = _header;
            int          limitOffset   = (int)Math.Min(termBuffer.Capacity, (limitPosition - initialPosition) + offset);

            _header.Buffer = termBuffer;
            long resultingPosition = initialPosition;

            try
            {
                while (offset < limitOffset)
                {
                    int length = FrameDescriptor.FrameLengthVolatile(termBuffer, offset);
                    if (length <= 0)
                    {
                        break;
                    }

                    int frameOffset = offset;
                    offset += BitUtil.Align(length, FrameDescriptor.FRAME_ALIGNMENT);

                    if (FrameDescriptor.IsPaddingFrame(termBuffer, frameOffset))
                    {
                        position         += (offset - initialOffset);
                        initialOffset     = offset;
                        resultingPosition = position;

                        continue;
                    }

                    _header.Offset = frameOffset;


                    var action = handler.OnFragment(
                        termBuffer,
                        frameOffset + DataHeaderFlyweight.HEADER_LENGTH,
                        length - DataHeaderFlyweight.HEADER_LENGTH,
                        _header);

                    if (action == ControlledFragmentHandlerAction.ABORT)
                    {
                        break;
                    }

                    position     += (offset - initialOffset);
                    initialOffset = offset;

                    if ((_header.Flags & FrameDescriptor.END_FRAG_FLAG) == FrameDescriptor.END_FRAG_FLAG)
                    {
                        resultingPosition = position;
                    }

                    if (action == ControlledFragmentHandlerAction.BREAK)
                    {
                        break;
                    }
                }
            }
            catch (Exception t)
            {
                _errorHandler(t);
            }

            return(resultingPosition);
        }
Exemplo n.º 58
0
 public static long RawTailVolatile(UnsafeBuffer metaDataBuffer, int partitionIndex)
 {
     return(metaDataBuffer.GetLongVolatile(TERM_TAIL_COUNTERS_OFFSET + BitUtil.SIZE_OF_LONG * partitionIndex));
 }
Exemplo n.º 59
0
 public JsonValidationResult Validate(UnsafeBuffer <char> buffer, int start, int count)
 {
     ScheduleValidation(buffer, start, count).Complete();
     return(GetResult());
 }
Exemplo n.º 60
0
 /// <summary>
 ///     Get the value of the end of stream position.
 /// </summary>
 /// <param name="metaDataBuffer"> containing the metadata. </param>
 /// <returns> the value of end of stream position </returns>
 public static long EndOfStreamPosition(UnsafeBuffer metaDataBuffer)
 {
     return(metaDataBuffer.GetLongVolatile(LOG_END_OF_STREAM_POSITION_OFFSET));
 }