public int GetVehicleCode(Span <byte> dst) { const int length = 6; if (dst.Length < length) { ThrowHelper.ThrowWhenSpanLengthTooSmall(dst.Length); } _buffer.GetBytes(_offset + 28, dst); return(length); }
public int GetSessionID(Span <byte> dst) { const int length = 10; if (dst.Length < length) { ThrowHelper.ThrowWhenSpanLengthTooSmall(dst.Length); } _buffer.GetBytes(_offset + 0, dst); return(length); }
public int GetQuoteEntryID(byte[] dst, int dstOffset) { const int length = 10; if (dstOffset < 0 || dstOffset > (dst.Length - length)) { throw new IndexOutOfRangeException("dstOffset out of range for copy: offset=" + dstOffset); } _buffer.GetBytes(_offset + 0, dst, dstOffset, length); return(length); }
public int GetEnteringTrader(Span <byte> dst) { const int length = 5; if (dst.Length < length) { ThrowHelper.ThrowWhenSpanLengthTooSmall(dst.Length); } _buffer.GetBytes(_offset + 36, dst); return(length); }
public int GetVehicleCode(byte[] dst, int dstOffset) { const int length = 6; if (dstOffset < 0 || dstOffset > (dst.Length - length)) { throw new IndexOutOfRangeException("dstOffset out of range for copy: offset=" + dstOffset); } _buffer.GetBytes(_offset + 32, dst, dstOffset, length); return(length); }
public int GetPackageName(byte[] dst, int dstOffset, int length) { const int sizeOfLengthField = 1; int limit = Limit; _buffer.CheckLimit(limit + sizeOfLengthField); int dataLength = _buffer.Uint8Get(limit); int bytesCopied = Math.Min(length, dataLength); Limit = limit + sizeOfLengthField + dataLength; _buffer.GetBytes(limit + sizeOfLengthField, dst, dstOffset, bytesCopied); return(bytesCopied); }
public int GetUri(Span <byte> dst) { const int sizeOfLengthField = 4; int limit = _parentMessage.Limit; _buffer.CheckLimit(limit + sizeOfLengthField); int dataLength = (int)_buffer.Uint32GetLittleEndian(limit); int bytesCopied = Math.Min(dst.Length, dataLength); _parentMessage.Limit = limit + sizeOfLengthField + dataLength; _buffer.GetBytes(limit + sizeOfLengthField, dst.Slice(0, bytesCopied)); return(bytesCopied); }
public void ShouldPutStringWithNullTermination() { // the string length is less than the max and less than the capacity - should add null terminator const string value = "abc123"; const int index = 0; var written = _directBuffer.SetNullTerminatedBytesFromString(AsciiEncoding, value, index, 100, Terminator); var read = new byte[written]; var numBytesWritten = _directBuffer.GetBytes(index, read, 0, written); Assert.AreEqual(7, numBytesWritten); Assert.AreEqual(Terminator, read[read.Length - 1]); // should be terminated with the null value string result = AsciiEncoding.GetString(read); Assert.AreEqual(value, result.Substring(0, read.Length - 1)); }
static byte[] GenerateCar() { // This byte array is used for encoding and decoding, this is what you would send on the wire or save to disk var byteBuffer = new byte[4096]; // You need to "wrap" the array with a DirectBuffer, this class is used by the generated code to read and write efficiently to the underlying byte array var directBuffer = new DirectBuffer(byteBuffer); int bufferOffset = 0; var MessageHeader = new Sbe.MessageHeader(); var Car = new Car(); // Before encoding a message we need to create a SBE header which specify what we are going to encode (this will allow the decoder to detect that it's an encoded 'car' object) // We will probably simplify this part soon, so the header gets applied automatically, but for now it's manual MessageHeader.Wrap(directBuffer, bufferOffset, Car.SchemaVersion); // position the MessageHeader on the DirectBuffer, at the correct position MessageHeader.BlockLength = Car.BlockLength; // size that a car takes on the wire MessageHeader.SchemaId = Car.SchemaId; MessageHeader.TemplateId = Car.TemplateId; // identifier for the car object (SBE template ID) MessageHeader.Version = Car.SchemaVersion; // this can be overriden if we want to support different versions of the car object (advanced functionality) // Now that we have encoded the header in the byte array we can encode the car object itself bufferOffset += Sbe.MessageHeader.Size; int carLength = CarExample.Encode(Car, directBuffer, bufferOffset); var byteBuffer2 = new byte[carLength]; directBuffer.GetBytes(0, byteBuffer2, 0, carLength); return(byteBuffer2); }
public void ShouldPutStringNullTerminated() { //checks we can write a string and then retrieves it 'longhand' const string value = "abc123"; var encoding = System.Text.Encoding.ASCII; const int index = 0; var written = _directBuffer.SetNullTerminatedBytesFromString(encoding, value, index, value.Length, (byte)0); var read = new byte[written]; var numBytesWritten = _directBuffer.GetBytes(index, read, 0, written); Assert.AreEqual(1, numBytesWritten); Assert.AreEqual((byte)0, read[read.Length - 1]); string result = encoding.GetString(read); Assert.AreEqual(value, result); }
public static PrimitiveValue Get(DirectBuffer buffer, PrimitiveType type, int length) { if (length == 0) { return(null); } switch (type.Type) { case SbePrimitiveType.Char: if (length == 1) { return(new PrimitiveValue(buffer.CharGet(0), 1)); } else { var array = new byte[length]; buffer.GetBytes(0, array, 0, array.Length); return(new PrimitiveValue(array, "UTF-8", array.Length)); } case SbePrimitiveType.Int8: return(new PrimitiveValue(buffer.Int8Get(0), 1)); case SbePrimitiveType.Int16: return(new PrimitiveValue(buffer.Int16GetLittleEndian(0), 2)); case SbePrimitiveType.Int32: return(new PrimitiveValue(buffer.Int32GetLittleEndian(0), 4)); case SbePrimitiveType.Int64: return(new PrimitiveValue(buffer.Int64GetLittleEndian(0), 8)); case SbePrimitiveType.UInt8: return(new PrimitiveValue(buffer.Uint8Get(0), 1)); case SbePrimitiveType.UInt16: return(new PrimitiveValue(buffer.Uint16GetLittleEndian(0), 2)); case SbePrimitiveType.UInt32: return(new PrimitiveValue(buffer.Uint32GetLittleEndian(0), 4)); case SbePrimitiveType.UInt64: return(new PrimitiveValue(buffer.Uint64GetLittleEndian(0), 8)); case SbePrimitiveType.Float: return(new PrimitiveValue(buffer.FloatGetLittleEndian(0), 4)); case SbePrimitiveType.Double: return(new PrimitiveValue(buffer.DoubleGetLittleEndian(0), 8)); default: return(null); } }
public static PrimitiveValue Get(DirectBuffer buffer, PrimitiveType type, int length) { if (length == 0) { return null; } switch (type.Type) { case SbePrimitiveType.Char: if (length == 1) { return new PrimitiveValue(buffer.CharGet(0), 1); } else { var array = new byte[length]; buffer.GetBytes(0, array, 0, array.Length); return new PrimitiveValue(array, "UTF-8", array.Length); } case SbePrimitiveType.Int8: return new PrimitiveValue(buffer.Int8Get(0), 1); case SbePrimitiveType.Int16: return new PrimitiveValue(buffer.Int16GetLittleEndian(0), 2); case SbePrimitiveType.Int32: return new PrimitiveValue(buffer.Int32GetLittleEndian(0), 4); case SbePrimitiveType.Int64: return new PrimitiveValue(buffer.Int64GetLittleEndian(0), 8); case SbePrimitiveType.UInt8: return new PrimitiveValue(buffer.Uint8Get(0), 1); case SbePrimitiveType.UInt16: return new PrimitiveValue(buffer.Uint16GetLittleEndian(0), 2); case SbePrimitiveType.UInt32: return new PrimitiveValue(buffer.Uint32GetLittleEndian(0), 4); case SbePrimitiveType.UInt64: return new PrimitiveValue(buffer.Uint64GetLittleEndian(0), 8); case SbePrimitiveType.Float: return new PrimitiveValue(buffer.FloatGetLittleEndian(0), 4); case SbePrimitiveType.Double: return new PrimitiveValue(buffer.DoubleGetLittleEndian(0), 8); default: return null; } }
public int GetManufacturerCode(Span <byte> dst) { const int length = 3; if (dst.Length < length) { ThrowHelper.ThrowWhenSpanLengthTooSmall(dst.Length); } _buffer.GetBytes(_offset + 3, dst); return(length); }
public int GetUsageDescription(byte[] dst, int dstOffset, int length) { const int sizeOfLengthField = 4; int limit = _parentMessage.Limit; _buffer.CheckLimit(limit + sizeOfLengthField); int dataLength = (int)_buffer.Uint32GetLittleEndian(limit); int bytesCopied = Math.Min(length, dataLength); _parentMessage.Limit = limit + sizeOfLengthField + dataLength; _buffer.GetBytes(limit + sizeOfLengthField, dst, dstOffset, bytesCopied); return(bytesCopied); }
public virtual void OnVarData(Token fieldToken, DirectBuffer buffer, int bufferIndex, int length, Token typeToken) { string value; try { int varDataLength = buffer.GetBytes(bufferIndex, _tempBuffer, 0, length); System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(typeToken.Encoding.CharacterEncoding); value = encoding.GetString(_tempBuffer, 0, varDataLength); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.Write(ex.StackTrace); return; } PrintScope(); Console.WriteLine("{0}={1}", fieldToken.Name, value); }
static byte[] GenerateNewOrderSingle() { var byteBuffer = new byte[4096]; var directBuffer = new DirectBuffer(byteBuffer); int bufferOffset = 0; Type MessageHeaderType = GetType("MessageHeader"); var MessageHeaderObj = GetObjectByType("MessageHeader"); Type SimpleNewOrderType = GetType("SimpleNewOrder"); var SimpleNewOrderObject = GetObjectByType("SimpleNewOrder"); FieldInfo schemaVersion = GetFieldByName("SimpleNewOrder", "SchemaVersion"); MessageHeaderType.InvokeMember("Wrap", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, MessageHeaderObj, new object[] { directBuffer, bufferOffset, schemaVersion.GetValue(SimpleNewOrderObject) }); FieldInfo size = GetFieldByName("MessageHeader", "Size"); bufferOffset += int.Parse(size.GetValue(MessageHeaderObj).ToString()); SimpleNewOrderType.InvokeMember("WrapForEncode", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, SimpleNewOrderObject, new object[] { directBuffer, bufferOffset }); PropertyInfo clOrdId = GetPropertyByName("SimpleNewOrder", "ClOrdID"); clOrdId.SetValue(SimpleNewOrderObject, 1234567890); FieldInfo sizeSimpleNewOrder = GetFieldByName("SimpleNewOrder", "Size"); int SimpleNewOrderLength = int.Parse(sizeSimpleNewOrder.GetValue(SimpleNewOrderObject).ToString()); //NewOrderSingleExample.Encode(sno, directBuffer, bufferOffset); var byteBuffer2 = new byte[SimpleNewOrderLength]; directBuffer.GetBytes(0, byteBuffer2, 0, SimpleNewOrderLength); return(byteBuffer); }
private int WriteFieldsToTable(IWritableReactiveTable table, IReactiveColumn column, int rowId, DirectBuffer buffer, int bufferOffset, int read) { var columnId = column.ColumnId; var remaining = read - bufferOffset; if (column.Type == typeof(int)) { if (remaining < sizeof(int)) { return(-1); } table.SetValue(columnId, rowId, buffer.Int32GetLittleEndian(bufferOffset)); return(sizeof(int)); } else if (column.Type == typeof(short)) { if (remaining < sizeof(short)) { return(-1); } table.SetValue(columnId, rowId, buffer.Int16GetLittleEndian(bufferOffset)); return(sizeof(short)); } else if (column.Type == typeof(string)) { if (remaining < sizeof(ushort)) { return(-1); } var stringLength = buffer.Uint16GetLittleEndian(bufferOffset); bufferOffset += sizeof(ushort); remaining = read - bufferOffset; if (remaining < stringLength) { return(-1); } var bytesRead = buffer.GetBytes(bufferOffset, _stringTempBuffer, 0, stringLength); var value = Encoding.Default.GetString(_stringTempBuffer, 0, bytesRead); table.SetValue(columnId, rowId, value); return(sizeof(ushort) + bytesRead); } else if (column.Type == typeof(bool)) { if (remaining < sizeof(byte)) { return(-1); } var b = buffer.CharGet(bufferOffset); table.SetValue(columnId, rowId, b == 1); return(sizeof(byte)); } else if (column.Type == typeof(double)) { if (remaining < sizeof(double)) { return(-1); } var value = buffer.DoubleGetLittleEndian(bufferOffset); table.SetValue(columnId, rowId, value); return(sizeof(double)); } else if (column.Type == typeof(long)) { if (remaining < sizeof(long)) { return(-1); } table.SetValue(columnId, rowId, buffer.Int64GetLittleEndian(bufferOffset)); return(sizeof(long)); } else if (column.Type == typeof(decimal)) { if (remaining < sizeof(decimal)) { return(-1); } Buffer.BlockCopy(_byteArray, bufferOffset, _decimalBuffer, 0, _decimalBuffer.Length); table.SetValue(columnId, rowId, _bytesToDecimal(_decimalBuffer)); return(_decimalBuffer.Length); } else if (column.Type == typeof(DateTime)) { // table.SetValue(columnId, rowId, BclHelpers.ReadDateTime(reader)); } else if (column.Type == typeof(TimeSpan)) { // table.SetValue(columnId, rowId, BclHelpers.ReadTimeSpan(reader)); } else if (column.Type == typeof(Guid)) { // table.SetValue(columnId, rowId, BclHelpers.ReadGuid(reader)); } else if (column.Type == typeof(byte)) { if (remaining < sizeof(byte)) { return(-1); } table.SetValue(columnId, rowId, buffer.CharGet(bufferOffset)); return(sizeof(byte)); } else if (column.Type == typeof(char)) { if (remaining < sizeof(char)) { return(-1); } table.SetValue(columnId, rowId, (char)buffer.Uint16GetLittleEndian(bufferOffset)); return(sizeof(char)); } else if (column.Type == typeof(float)) { if (remaining < sizeof(float)) { return(-1); } table.SetValue(columnId, rowId, buffer.FloatGetLittleEndian(bufferOffset)); return(sizeof(float)); } return(0); }
public byte[] EncodeSBEMessage(SbeMessage Message) { if (_Assembly == null) { throw new Exception("To use SBE Engine, please init SbeReflectionWrapper passing the sbe file dll as parameter"); } var byteBuffer = new byte[4096]; var directBuffer = new DirectBuffer(byteBuffer); int bufferOffset = 0; Type MessageHeaderType = ReflectionHelper.GetType(_Assembly, _Header); var MessageHeaderObj = ReflectionHelper.GetObjectByType(_Assembly, _Header); Type MessageBodyType = ReflectionHelper.GetType(_Assembly, Message.Name); var MessageBodyObj = ReflectionHelper.GetObjectByType(_Assembly, Message.Name); FieldInfo schemaVersion = ReflectionHelper.GetFieldByName(_Assembly, Message.Name, "SchemaVersion"); //WRAP MessageHeaderType.InvokeMember("Wrap", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, MessageHeaderObj, new object[] { directBuffer, bufferOffset, schemaVersion.GetValue(MessageBodyObj) }); #region SET MESSAGE HEADER FIELDS //BLOCK LENGTH PropertyInfo blockLengthHeader = ReflectionHelper.GetPropertyByName(_Assembly, _Header, "BlockLength"); FieldInfo blockLengthBody = ReflectionHelper.GetFieldByName(_Assembly, Message.Name, "BlockLength"); blockLengthHeader.SetValue(MessageHeaderObj, blockLengthBody.GetValue(MessageBodyObj)); //SCHEMAID PropertyInfo schemaIDHeader = ReflectionHelper.GetPropertyByName(_Assembly, _Header, "SchemaId"); FieldInfo schemaIDBody = ReflectionHelper.GetFieldByName(_Assembly, Message.Name, "SchemaId"); schemaIDHeader.SetValue(MessageHeaderObj, schemaIDBody.GetValue(MessageBodyObj)); //TEMPLATEID PropertyInfo templateIdHeader = ReflectionHelper.GetPropertyByName(_Assembly, _Header, "TemplateId"); templateIdHeader.SetValue(MessageHeaderObj, Convert.ChangeType(Message.Id, templateIdHeader.PropertyType)); //VERSION PropertyInfo versionHeader = ReflectionHelper.GetPropertyByName(_Assembly, _Header, "Version"); FieldInfo schemaVersionBody = ReflectionHelper.GetFieldByName(_Assembly, Message.Name, "SchemaVersion"); versionHeader.SetValue(MessageHeaderObj, schemaVersionBody.GetValue(MessageBodyObj)); #endregion FieldInfo size = ReflectionHelper.GetFieldByName(_Assembly, _Header, "Size"); bufferOffset += int.Parse(size.GetValue(MessageHeaderObj).ToString()); MessageBodyType.InvokeMember("WrapForEncode", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, MessageBodyObj, new object[] { directBuffer, bufferOffset }); foreach (SbeField field in Message.Fields) { ReflectionHelper.SetField(_Assembly, field, Message.Name, MessageBodyObj); } PropertyInfo sizeBody = ReflectionHelper.GetPropertyByName(_Assembly, Message.Name, "Size"); int BodyLength = int.Parse(sizeBody.GetValue(MessageBodyObj).ToString()); var tempBuffer = new byte[BodyLength + 8]; directBuffer.GetBytes(0, tempBuffer, 0, BodyLength + 8); return(tempBuffer); }
public byte[] EncodeSBEMessageQATEngine(List <SbeMessage> Message) { if (Message.Count != 2) { return(null); } var byteBuffer = new byte[4096]; var directBuffer = new DirectBuffer(byteBuffer); int bufferOffset = 0; #region Header foreach (SbeField f in Message[0].Fields) { if (!string.IsNullOrEmpty(f.Value)) { directBuffer.Uint16PutLittleEndian(bufferOffset, ushort.Parse(f.Value)); } bufferOffset += f.Length.Value; } #endregion #region Body foreach (SbeField field in Message[1].Fields) { if (!field.Presence.ToLower().Equals("constant")) { if (field.SemanticType.ToLower().Equals("string")) { SetField(directBuffer, field.Fields[0], ref bufferOffset); field.Fields[1].PrimitiveType = "char"; SetField(directBuffer, field.Fields[1], ref bufferOffset); bufferOffset += int.Parse(field.Fields[0].Value); } else if (field.Type.ToLower().Equals("groupsizeencoding")) { directBuffer.Uint16PutLittleEndian(bufferOffset, (ushort)field.BlockLength); bufferOffset += 2; directBuffer.Uint8Put(bufferOffset, (byte)int.Parse(field.Value)); bufferOffset += 1; if (field.Fields != null && field.Fields.Count > 0) { foreach (SbeField child in field.Fields) { if (!child.Presence.ToLower().Equals("constant")) { SetField(directBuffer, child, ref bufferOffset); } } } } else { if (field.Fields != null && field.Fields.Count > 0) { foreach (SbeField child in field.Fields) { if (!child.Presence.ToLower().Equals("constant")) { SetField(directBuffer, child, ref bufferOffset); } } } else { SetField(directBuffer, field, ref bufferOffset); } } } } #endregion var tempBuffer = new byte[bufferOffset]; directBuffer.GetBytes(0, tempBuffer, 0, bufferOffset); return(tempBuffer); }