public void Recycle()
        {
            var directBuffer = new DirectBuffer();
            var firstBuffer = new Byte[16];
            directBuffer.Wrap(firstBuffer);

            directBuffer.Int64PutLittleEndian(0, 1);
            Assert.AreEqual(1, BitConverter.ToInt64(firstBuffer, 0));

            var secondBuffer = new byte[16];
            var secondBufferHandle = GCHandle.Alloc(secondBuffer, GCHandleType.Pinned);
            var secondUnmanagedBuffer = (byte*)secondBufferHandle.AddrOfPinnedObject().ToPointer();
            directBuffer.Wrap(secondUnmanagedBuffer, 16);
            directBuffer.Int64PutLittleEndian(0, 2);
            Assert.AreEqual(2, BitConverter.ToInt64(secondBuffer, 0));

            directBuffer.Dispose();
        }
        public void ConstructFromNativeBuffer()
        {
            var managedBuffer   = new Byte[16];
            var handle          = GCHandle.Alloc(managedBuffer, GCHandleType.Pinned);
            var unmanagedBuffer = (byte *)handle.AddrOfPinnedObject().ToPointer();

            const int value = 5;
            const int index = 0;

            using (var directBufferFromUnmanagedBuffer = new DirectBuffer(unmanagedBuffer, managedBuffer.Length))
            {
                directBufferFromUnmanagedBuffer.Int64PutLittleEndian(index, value);
                Assert.AreEqual(value, *(long *)(unmanagedBuffer + index));
            }
        }
        public void ConstructFromNativeBuffer()
        {
            var managedBuffer = new Byte[16];
            var handle = GCHandle.Alloc(managedBuffer, GCHandleType.Pinned);
            var unmanagedBuffer = (byte*) handle.AddrOfPinnedObject().ToPointer();

            const int value = 5;
            const int index = 0;
            
            using (var directBufferFromUnmanagedbuffer = new DirectBuffer(unmanagedBuffer, managedBuffer.Length))
            {
                directBufferFromUnmanagedbuffer.Int64PutLittleEndian(index, value);
                Assert.AreEqual(value, *(long*) (unmanagedBuffer + index));
            }
        }
        public void ReallocateFailure()
        {
            const int initialBufferSize = 8;
            var       initialBuffer     = new Byte[initialBufferSize];
            var       reallocableBuffer = new DirectBuffer(initialBuffer, (existingSize, requestedSize) =>
            {
                Assert.AreEqual(initialBufferSize, existingSize);
                Assert.AreEqual(16, requestedSize);
                return(null);
            });

            reallocableBuffer.CheckLimit(8);
            reallocableBuffer.Int64PutLittleEndian(0, 1);
            Assert.AreEqual(initialBufferSize, reallocableBuffer.Capacity);
            Assert.ThrowsException <IndexOutOfRangeException>(() => reallocableBuffer.CheckLimit(16));
        }
        public void ConstructFromArraySegment()
        {
            const int value  = 5;
            const int index  = 0;
            const int offset = 512;
            const int size   = 1024;

            var bigBuffer = new byte[size];
            var buffer    = new ArraySegment <byte>(bigBuffer, offset, size - offset);

            using (var directBuffer = new DirectBuffer(buffer))
            {
                directBuffer.Int64PutLittleEndian(index, value);
                Assert.AreEqual(value, bigBuffer[offset + index]);
                Assert.AreEqual(value, buffer.AsSpan <byte>()[index]);
            }
        }
        public void ShouldPutInt64LittleEndian(int value, int index)
        {
            _directBuffer.Int64PutLittleEndian(index, value);

            Assert.AreEqual(value, *(long *)(_pBuffer + index));
        }
Example #7
0
        public static int Put(DirectBuffer buffer, PrimitiveValue value, PrimitiveType type)
        {
            if (value == null)
            {
                return(0);
            }

            switch (type.Type)
            {
            case SbePrimitiveType.Char:
                if (value.Size == 1)
                {
                    buffer.CharPut(0, (byte)value.LongValue());
                    return(1);
                }
                else
                {
                    var byteArrayValue = value.ByteArrayValue();
                    buffer.SetBytes(0, byteArrayValue, 0, byteArrayValue.Length);
                    return(byteArrayValue.Length);
                }

            case SbePrimitiveType.Int8:
                buffer.Int8Put(0, (sbyte)value.LongValue());
                return(1);

            case SbePrimitiveType.Int16:
                buffer.Int16PutLittleEndian(0, (short)value.LongValue());
                return(2);

            case SbePrimitiveType.Int32:
                buffer.Int32PutLittleEndian(0, (int)value.LongValue());
                return(4);

            case SbePrimitiveType.Int64:
                buffer.Int64PutLittleEndian(0, value.LongValue());
                return(8);

            case SbePrimitiveType.UInt8:
                buffer.Uint8Put(0, (byte)value.LongValue());
                return(1);

            case SbePrimitiveType.UInt16:
                buffer.Uint16PutLittleEndian(0, (ushort)value.LongValue());
                return(2);

            case SbePrimitiveType.UInt32:
                buffer.Uint32PutLittleEndian(0, (uint)value.LongValue());
                return(4);

            case SbePrimitiveType.UInt64:
                buffer.Uint64PutLittleEndian(0, value.ULongValue());
                return(8);

            case SbePrimitiveType.Float:
                buffer.FloatPutLittleEndian(0, (float)value.DoubleValue());
                return(4);

            case SbePrimitiveType.Double:
                buffer.DoublePutLittleEndian(0, value.DoubleValue());
                return(8);

            default:
                return(0);
            }
        }
Example #8
0
        public static int Put(DirectBuffer buffer, PrimitiveValue value, PrimitiveType type)
        {
            if (value == null)
            {
                return 0;
            }

            switch (type.Type)
            {
                case SbePrimitiveType.Char:
                    if (value.Size == 1)
                    {
                        buffer.CharPut(0, (byte)value.LongValue());
                        return 1;
                    }
                    else
                    {
                        var byteArrayValue = value.ByteArrayValue();
                        buffer.SetBytes(0, byteArrayValue, 0, byteArrayValue.Length);
                        return byteArrayValue.Length;
                    }

                case SbePrimitiveType.Int8:
                    buffer.Int8Put(0, (sbyte)value.LongValue());
                    return 1;

                case SbePrimitiveType.Int16:
                    buffer.Int16PutLittleEndian(0, (short)value.LongValue());
                    return 2;

                case SbePrimitiveType.Int32:
                    buffer.Int32PutLittleEndian(0, (int)value.LongValue());
                    return 4;

                case SbePrimitiveType.Int64:
                    buffer.Int64PutLittleEndian(0, value.LongValue());
                    return 8;

                case SbePrimitiveType.UInt8:
                    buffer.Uint8Put(0, (byte)value.LongValue());
                    return 1;

                case SbePrimitiveType.UInt16:
                    buffer.Uint16PutLittleEndian(0, (ushort)value.LongValue());
                    return 2;

                case SbePrimitiveType.UInt32:
                    buffer.Uint32PutLittleEndian(0, (uint) value.LongValue());
                    return 4;

                case SbePrimitiveType.UInt64:
                    buffer.Uint64PutLittleEndian(0, value.ULongValue());
                    return 8;

                case SbePrimitiveType.Float:
                    buffer.FloatPutLittleEndian(0, (float) value.DoubleValue());
                    return 4;

                case SbePrimitiveType.Double:
                    buffer.DoublePutLittleEndian(0, value.DoubleValue());
                    return 8;

                default:
                    return 0;
            }
        }
Example #9
0
        private int WriteUpdateValue(IReactiveTable table, TableUpdate tableUpdate, DirectBuffer buffer, int offset)
        {
            var column = tableUpdate.Column;

            if (column.Type == typeof(int))
            {
                buffer.Int32PutLittleEndian(offset, table.GetValue <int>(column.ColumnId, tableUpdate.RowIndex));
                return(sizeof(int));
            }
            if (column.Type == typeof(short))
            {
                buffer.Int16PutLittleEndian(offset, table.GetValue <short>(column.ColumnId, tableUpdate.RowIndex));
                return(sizeof(short));
            }
            if (column.Type == typeof(string))
            {
                var stringValue = table.GetValue <string>(column.ColumnId, tableUpdate.RowIndex) ?? string.Empty;
                // Set the length
                var length = (ushort)stringValue.Length;
                buffer.Uint16PutLittleEndian(offset, length);
                var written = sizeof(ushort);
                offset += written;
                // And then the value
                var bytesWritten = Encoding.Default.GetBytes(stringValue, 0, stringValue.Length, _stringBuffer, 0);
                _buffer.SetBytes(offset, _stringBuffer, 0, bytesWritten);
                written += bytesWritten;
                //written += WriteStringToByteArray(stringValue, _byteArray, offset);
                return(written);
            }
            if (column.Type == typeof(bool))
            {
                buffer.CharPut(offset, (byte)(table.GetValue <bool>(column.ColumnId, tableUpdate.RowIndex) ? 1 : 0));
                return(sizeof(byte));
            }
            if (column.Type == typeof(double))
            {
                var value = table.GetValue <double>(column.ColumnId, tableUpdate.RowIndex);
                buffer.DoublePutLittleEndian(offset, value);
                return(sizeof(double));
            }
            if (column.Type == typeof(long))
            {
                buffer.Int64PutLittleEndian(offset, table.GetValue <long>(column.ColumnId, tableUpdate.RowIndex));
                return(sizeof(long));
            }
            if (column.Type == typeof(decimal))
            {
                var decimalVal = table.GetValue <decimal>(column.ColumnId, tableUpdate.RowIndex);
                _decimalGetBytes(decimalVal, _decimalBuffer);
                buffer.SetBytes(offset, _decimalBuffer, 0, _decimalBuffer.Length);
                return(_decimalBuffer.Length);
            }
            if (column.Type == typeof(DateTime))
            {
                throw new NotImplementedException();
            }
            if (column.Type == typeof(TimeSpan))
            {
                throw new NotImplementedException();
            }
            if (column.Type == typeof(Guid))
            {
                throw new NotImplementedException();
            }
            if (column.Type == typeof(float))
            {
                buffer.FloatPutLittleEndian(offset, table.GetValue <float>(column.ColumnId, tableUpdate.RowIndex));
                return(sizeof(float));
            }
            if (column.Type == typeof(byte))
            {
                buffer.CharPut(offset, table.GetValue <byte>(column.ColumnId, tableUpdate.RowIndex));
                return(sizeof(byte));
            }
            if (column.Type == typeof(char))
            {
                buffer.Uint16PutLittleEndian(offset, table.GetValue <char>(column.ColumnId, tableUpdate.RowIndex));
                return(sizeof(char));
            }
            return(0);
        }
        private void SetField(DirectBuffer buffer, SbeField field2, ref int offset)
        {
            SbeField field = field2.Clone();

            if (field.PrimitiveType == null)
            {
                field.PrimitiveType = field.Type;
            }

            if (field.PrimitiveType.ToLower().Equals("uint8"))
            {
                if (!string.IsNullOrEmpty(field.Value))
                {
                    buffer.Uint8Put(offset, (byte)int.Parse(field.Value));
                }
                offset += 1;
            }
            else if (field.PrimitiveType.ToLower().Equals("uint16"))
            {
                if (!string.IsNullOrEmpty(field.Value))
                {
                    buffer.Uint16PutLittleEndian(offset, ushort.Parse(field.Value));
                }
                offset += 2;
            }
            else if (field.PrimitiveType.ToLower().Equals("uint32"))
            {
                if (!string.IsNullOrEmpty(field.Value))
                {
                    buffer.Uint32PutLittleEndian(offset, uint.Parse(field.Value));
                }
                offset += 4;
            }
            else if (field.PrimitiveType.ToLower().Equals("uint64"))
            {
                if (!string.IsNullOrEmpty(field.Value))
                {
                    buffer.Uint64PutLittleEndian(offset, ulong.Parse(field.Value));
                }
                offset += 8;
            }
            else if (field.PrimitiveType.ToLower().Equals("int8"))
            {
                if (!string.IsNullOrEmpty(field.Value))
                {
                    buffer.Int8Put(offset, (sbyte)int.Parse(field.Value));
                }
                offset += 1;
            }
            else if (field.PrimitiveType.ToLower().Equals("int16"))
            {
                if (!string.IsNullOrEmpty(field.Value))
                {
                    buffer.Int16PutLittleEndian(offset, short.Parse(field.Value));
                }
                offset += 2;
            }
            else if (field.PrimitiveType.ToLower().Equals("int32"))
            {
                if (!string.IsNullOrEmpty(field.Value))
                {
                    buffer.Int32PutLittleEndian(offset, int.Parse(field.Value));
                }
                offset += 4;
            }
            else if (field.PrimitiveType.ToLower().Equals("int64"))
            {
                if (!string.IsNullOrEmpty(field.Value))
                {
                    buffer.Int64PutLittleEndian(offset, long.Parse(field.Value));
                }
                offset += 8;
            }
            else if (field.PrimitiveType.ToLower().Equals("char"))
            {
                if (!string.IsNullOrEmpty(field.Value))
                {
                    if (!String.IsNullOrEmpty(field.CharacterEncoding))
                    {
                        byte[] charString = Encoding.GetEncoding(field.CharacterEncoding).GetBytes(field.Value);
                        int    temp       = offset;
                        foreach (byte m in charString)
                        {
                            buffer.CharPut(temp, m);
                            temp++;
                        }
                    }
                    else
                    {
                        byte[] charString = Encoding.GetEncoding("UTF-8").GetBytes(field.Value);
                        buffer.CharPut(offset, charString[0]);
                    }
                }
                offset += field.Length.Value;
            }
        }
        public void Reallocate()
        {
            const int initialBufferSize = 8;
            var initialBuffer = new Byte[initialBufferSize];
            
            const int biggerBufferSize = 100;
            var biggerBuffer = new byte[biggerBufferSize];
            
            var reallocableBuffer = new DirectBuffer(initialBuffer, 
                (existingSize, requestedSize) =>
                {
                    Assert.AreEqual(initialBufferSize, existingSize);
                    Assert.AreEqual(16, requestedSize);
                    return biggerBuffer;
                });
            
            reallocableBuffer.CheckLimit(8);
            reallocableBuffer.Int64PutLittleEndian(0, 1);
            Assert.AreEqual(initialBufferSize, reallocableBuffer.Capacity);

            reallocableBuffer.CheckLimit(16);
            reallocableBuffer.Int64PutLittleEndian(8, 2);
            Assert.AreEqual(biggerBufferSize, reallocableBuffer.Capacity);

            Assert.AreEqual(1, BitConverter.ToInt64(biggerBuffer, 0));
            Assert.AreEqual(2, BitConverter.ToInt64(biggerBuffer, 8));
        }
        public void ReallocateFailure()
        {
            const int initialBufferSize = 8;
            var initialBuffer = new Byte[initialBufferSize];
            var reallocableBuffer = new DirectBuffer(initialBuffer, (existingSize, requestedSize) =>
            {
                Assert.AreEqual(initialBufferSize, existingSize);
                Assert.AreEqual(16, requestedSize);
                return null;
            });

            reallocableBuffer.CheckLimit(8);
            reallocableBuffer.Int64PutLittleEndian(0, 1);
            Assert.AreEqual(initialBufferSize, reallocableBuffer.Capacity);
            Assert.Throws<IndexOutOfRangeException>(() => reallocableBuffer.CheckLimit(16));
        }