public static ColumnDefinitionPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); var catalog = reader.ReadLengthEncodedByteString(); var schema = reader.ReadLengthEncodedByteString(); var table = reader.ReadLengthEncodedByteString(); var physicalTable = reader.ReadLengthEncodedByteString(); var name = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString()); var physicalName = reader.ReadLengthEncodedByteString(); reader.ReadByte(0x0C); // length of fixed-length fields, always 0x0C var characterSet = (CharacterSet) reader.ReadUInt16(); var columnLength = (int) reader.ReadUInt32(); var columnType = (ColumnType) reader.ReadByte(); var columnFlags = (ColumnFlags) reader.ReadUInt16(); var decimals = reader.ReadByte(); // 0x00 for integers and static strings, 0x1f for dynamic strings, double, float, 0x00 to 0x51 for decimals reader.ReadByte(0); if (reader.BytesRemaining > 0) { int defaultValuesCount = checked((int) reader.ReadLengthEncodedInteger()); for (int i = 0; i < defaultValuesCount; i++) reader.ReadLengthEncodedByteString(); } if (reader.BytesRemaining != 0) throw new FormatException("Extra bytes at end of payload."); return new ColumnDefinitionPayload(name, characterSet, columnLength, columnType, columnFlags); }
internal InitialHandshakePacket(ByteArrayReader reader) { reader.ReadByte(c_protocolVersion); ServerVersion = reader.ReadNullTerminatedByteString(); ConnectionId = reader.ReadInt32(); AuthPluginData = reader.ReadByteString(8); reader.ReadByte(0); var capabilityFlagsLow = reader.ReadUInt16(); if (reader.BytesRemaining > 0) { var charSet = (CharacterSet)reader.ReadByte(); ServerStatus status = (ServerStatus)reader.ReadInt16(); var capabilityFlagsHigh = reader.ReadUInt16(); ProtocolCapabilities = (ProtocolCapabilities)(capabilityFlagsHigh << 16 | capabilityFlagsLow); var authPluginDataLength = reader.ReadByte(); var unused = reader.ReadByteString(10); if ((ProtocolCapabilities & ProtocolCapabilities.SecureConnection) != 0) { var authPluginData2 = reader.ReadByteString(Math.Max(13, authPluginDataLength - 8)); var concatenated = new byte[AuthPluginData.Length + authPluginData2.Length]; Buffer.BlockCopy(AuthPluginData, 0, concatenated, 0, AuthPluginData.Length); Buffer.BlockCopy(authPluginData2, 0, concatenated, AuthPluginData.Length, authPluginData2.Length); AuthPluginData = concatenated; } if ((ProtocolCapabilities & ProtocolCapabilities.PluginAuth) != 0) { AuthPluginName = Encoding.UTF8.GetString(reader.ReadNullTerminatedByteString()); } } }
CommResponse IProtocolCodec.ClientDecode(CommDataBase data) { ModbusClient ownerProtocol = (ModbusClient)data.OwnerProtocol; ModbusCommand userData = (ModbusCommand)data.UserData; ByteArrayReader incomingData = data.IncomingData; int count = incomingData.Length - 4; if (count >= 0 && (int)incomingData.ReadByte() == (int)ownerProtocol.Address) { byte num1 = incomingData.ReadByte(); ByteArrayReader body = new ByteArrayReader(incomingData.ReadBytes(count)); ushort num2 = ByteArrayHelpers.CalcCRC16(incomingData.ToArray(), 0, incomingData.Length - 2); if ((int)incomingData.ReadInt16LE() == (int)(short)num2 && ((int)num1 & (int)sbyte.MaxValue) == (int)userData.FunctionCode) { if (num1 <= (byte)127) { ModbusCodecBase.CommandCodecs[(int)num1]?.ClientDecode(userData, body); return(new CommResponse(data, 3)); } if (incomingData.CanRead(1)) { userData.ExceptionCode = incomingData.ReadByte(); } return(new CommResponse(data, 2)); } } return(new CommResponse(data, 0)); }
CommResponse IProtocolCodec.ServerDecode(CommDataBase data) { ModbusServer ownerProtocol = (ModbusServer)data.OwnerProtocol; ByteArrayReader incomingData = data.IncomingData; int length = incomingData.Length; if (length < 4) { return(new CommResponse(data, 0)); } if ((int)incomingData.ReadByte() == (int)ownerProtocol.Address) { byte fc = incomingData.ReadByte(); if ((int)fc < ModbusCodecBase.CommandCodecs.Length) { ModbusCommand command = new ModbusCommand(fc); data.UserData = (object)command; command.QueryTotalLength = 6; ModbusCommandCodec commandCodec = ModbusCodecBase.CommandCodecs[(int)fc]; ByteArrayReader body = new ByteArrayReader(incomingData.ReadBytes(length - 4)); commandCodec.ServerDecode(command, body); ushort num = ByteArrayHelpers.CalcCRC16(incomingData.ToArray(), 0, command.QueryTotalLength - 2); if ((int)ByteArrayHelpers.ReadInt16LE(((IByteArray)incomingData).Data, command.QueryTotalLength - 2) == (int)(short)num) { return(new CommResponse(data, 3)); } } } return(new CommResponse(data, 1)); }
/// <summary> /// Helper for unpacking discrete data incoming as a bit-array /// </summary> /// <param name="command"></param> /// <param name="body"></param> internal static void PopDiscretes( ModbusCommand command, ByteArrayReader body) { var byteCount = body.ReadByte(); var count = command.Count; command.Data = new ushort[count]; command.QueryTotalLength += (byteCount + 1); int k = 0; while (body.EndOfBuffer == false) { if (command.Count <= k) { break; } byte hb = body.CanRead(1) ? body.ReadByte() : (byte)0; byte lb = body.CanRead(1) ? body.ReadByte() : (byte)0; command.Data[k++] = (ushort)((hb << 8) | lb); //int n = count <= 8 ? count : 8; //count -= n; //for (int i = 0; i < n; i++) // command.Data[k++] = (ushort)(cell & (1 << i)); } }
CommResponse IProtocolCodec.ClientDecode(CommDataBase data) { ModbusClient ownerProtocol = (ModbusClient)data.OwnerProtocol; ModbusCommand userData = (ModbusCommand)data.UserData; ByteArrayReader incomingData = data.IncomingData; if (incomingData.Length >= 6 && (int)incomingData.ReadUInt16BE() == (int)(ushort)userData.TransId && incomingData.ReadInt16BE() == (short)0) { short num1 = incomingData.ReadInt16BE(); if (incomingData.Length >= (int)num1 + 6 && (int)incomingData.ReadByte() == (int)ownerProtocol.Address) { byte num2 = incomingData.ReadByte(); if (((int)num2 & (int)sbyte.MaxValue) == (int)userData.FunctionCode) { if (num2 <= (byte)127) { ByteArrayReader body = new ByteArrayReader(incomingData.ReadToEnd()); ModbusCodecBase.CommandCodecs[(int)num2]?.ClientDecode(userData, body); return(new CommResponse(data, 3)); } userData.ExceptionCode = incomingData.ReadByte(); return(new CommResponse(data, 2)); } } } return(new CommResponse(data, 0)); }
public static ColumnDefinitionPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); var catalog = reader.ReadLengthEncodedByteString(); var schema = reader.ReadLengthEncodedByteString(); var table = reader.ReadLengthEncodedByteString(); var physicalTable = reader.ReadLengthEncodedByteString(); var name = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString()); var physicalName = reader.ReadLengthEncodedByteString(); reader.ReadByte(0x0C); // length of fixed-length fields, always 0x0C var characterSet = (CharacterSet)reader.ReadUInt16(); var columnLength = (int)reader.ReadUInt32(); var columnType = (ColumnType)reader.ReadByte(); var columnFlags = (ColumnFlags)reader.ReadUInt16(); var decimals = reader.ReadByte(); // 0x00 for integers and static strings, 0x1f for dynamic strings, double, float, 0x00 to 0x51 for decimals reader.ReadByte(0); if (reader.BytesRemaining > 0) { int defaultValuesCount = checked ((int)reader.ReadLengthEncodedInteger()); for (int i = 0; i < defaultValuesCount; i++) { reader.ReadLengthEncodedByteString(); } } if (reader.BytesRemaining != 0) { throw new FormatException("Extra bytes at end of payload."); } return(new ColumnDefinitionPayload(name, characterSet, columnLength, columnType, columnFlags)); }
public static ColumnDefinitionPayload Create(ArraySegment <byte> arraySegment) { var reader = new ByteArrayReader(arraySegment); SkipLengthEncodedByteString(ref reader); // catalog SkipLengthEncodedByteString(ref reader); // schema SkipLengthEncodedByteString(ref reader); // table SkipLengthEncodedByteString(ref reader); // physical table SkipLengthEncodedByteString(ref reader); // name SkipLengthEncodedByteString(ref reader); // physical name reader.ReadByte(0x0C); // length of fixed-length fields, always 0x0C var characterSet = (CharacterSet)reader.ReadUInt16(); var columnLength = reader.ReadUInt32(); var columnType = (ColumnType)reader.ReadByte(); var columnFlags = (ColumnFlags)reader.ReadUInt16(); var decimals = reader.ReadByte(); // 0x00 for integers and static strings, 0x1f for dynamic strings, double, float, 0x00 to 0x51 for decimals reader.ReadByte(0); // reserved byte 1 reader.ReadByte(0); // reserved byte 2 return(new ColumnDefinitionPayload { OriginalData = arraySegment, CharacterSet = characterSet, ColumnLength = columnLength, ColumnType = columnType, ColumnFlags = columnFlags, Decimals = decimals }); }
internal InitialHandshakePacket(ByteArrayReader reader) { reader.ReadByte(c_protocolVersion); ServerVersion = reader.ReadNullTerminatedByteString(); var connectionId = reader.ReadInt32(); AuthPluginData = reader.ReadByteString(8); reader.ReadByte(0); var capabilityFlagsLow = reader.ReadUInt16(); if (reader.BytesRemaining > 0) { var charSet = (CharacterSet) reader.ReadByte(); ServerStatus status = (ServerStatus) reader.ReadInt16(); var capabilityFlagsHigh = reader.ReadUInt16(); ProtocolCapabilities = (ProtocolCapabilities) (capabilityFlagsHigh << 16 | capabilityFlagsLow); var authPluginDataLength = reader.ReadByte(); var unused = reader.ReadByteString(10); if (ProtocolCapabilities.HasFlag(ProtocolCapabilities.SecureConnection) && authPluginDataLength > 0) { var authPluginData2 = reader.ReadByteString(Math.Max(13, authPluginDataLength - 8)); var concatenated = new byte[AuthPluginData.Length + authPluginData2.Length]; Buffer.BlockCopy(AuthPluginData, 0, concatenated, 0, AuthPluginData.Length); Buffer.BlockCopy(authPluginData2, 0, concatenated, AuthPluginData.Length, authPluginData2.Length); AuthPluginData = concatenated; } if (ProtocolCapabilities.HasFlag(ProtocolCapabilities.PluginAuth)) AuthPluginName = Encoding.UTF8.GetString(reader.ReadNullTerminatedByteString()); } }
/// <summary> /// Helper for unpacking discrete data incoming as a bit-array /// </summary> /// <param name="command"></param> /// <param name="body"></param> internal static void PopDiscretes( ModbusCommand command, ByteArrayReader body) { var byteCount = body.ReadByte(); var count = command.Count; command.Data = new ushort[count]; command.QueryTotalLength += (byteCount + 1); int k = 0; while (count > 0) { byteCount--; int cell = body.ReadByte(); int n = count <= 8 ? count : 8; count -= n; for (int i = 0; i < n; i++) { command.Data[k++] = (ushort)(cell & (1 << i)); } } }
public static InitialHandshakePayload Create(ReadOnlySpan <byte> span) { var reader = new ByteArrayReader(span); reader.ReadByte(c_protocolVersion); var serverVersion = reader.ReadNullTerminatedByteString(); var connectionId = reader.ReadInt32(); byte[] authPluginData = null; var authPluginData1 = reader.ReadByteString(8); string authPluginName = null; reader.ReadByte(0); var protocolCapabilities = (ProtocolCapabilities)reader.ReadUInt16(); if (reader.BytesRemaining > 0) { var charSet = (CharacterSet)reader.ReadByte(); var status = (ServerStatus)reader.ReadInt16(); var capabilityFlagsHigh = reader.ReadUInt16(); protocolCapabilities |= (ProtocolCapabilities)((ulong)capabilityFlagsHigh << 16); var authPluginDataLength = reader.ReadByte(); var unused = reader.ReadByteString(6); long extendedCapabilites = reader.ReadInt32(); if ((protocolCapabilities & ProtocolCapabilities.LongPassword) == 0) { // MariaDB clears the CLIENT_LONG_PASSWORD flag to indicate it's not a MySQL Server protocolCapabilities |= (ProtocolCapabilities)(extendedCapabilites << 32); } if ((protocolCapabilities & ProtocolCapabilities.SecureConnection) != 0) { var authPluginData2 = reader.ReadByteString(Math.Max(13, authPluginDataLength - 8)); authPluginData = new byte[authPluginData1.Length + authPluginData2.Length]; authPluginData1.CopyTo(authPluginData); authPluginData2.CopyTo(new Span <byte>(authPluginData).Slice(authPluginData1.Length)); } if ((protocolCapabilities & ProtocolCapabilities.PluginAuth) != 0) { authPluginName = Encoding.UTF8.GetString(reader.ReadNullOrEofTerminatedByteString()); } } if (authPluginData is null) { authPluginData = authPluginData1.ToArray(); } if (reader.BytesRemaining != 0) { throw new FormatException("Extra bytes at end of payload."); } return(new InitialHandshakePayload(protocolCapabilities, serverVersion.ToArray(), connectionId, authPluginData, authPluginName)); }
public static CachingSha2ServerResponsePayload Create(ReadOnlySpan <byte> span) { var reader = new ByteArrayReader(span); reader.ReadByte(Signature); var secondByte = reader.ReadByte(); return(new CachingSha2ServerResponsePayload( secondByte == SuccessSignature, secondByte == FullAuthRequiredSignature)); }
public static ErrorPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); var errorCode = reader.ReadUInt16(); reader.ReadByte(0x23); var state = Encoding.ASCII.GetString(reader.ReadByteString(5)); var message = Encoding.UTF8.GetString(reader.ReadByteString(payload.ArraySegment.Count - 9)); return new ErrorPayload(errorCode, state, message); }
public static CachingSha2ServerResponsePayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); var secondByte = reader.ReadByte(); return(new CachingSha2ServerResponsePayload( secondByte == SuccessSignature, secondByte == FullAuthRequiredSignature)); }
public static EventHeader Create(ref ByteArrayReader reader) { reader.ReadByte(); // skip 0x00 OK byte var timestamp = reader.ReadInt32(); var eventType = (EventType)reader.ReadByte(); var serverId = reader.ReadInt32(); var eventSize = reader.ReadInt32(); var position = reader.ReadInt32(); var flags = (EventFlags)reader.ReadInt16(); return(new EventHeader(timestamp, eventType, serverId, eventSize, position, flags)); }
public static StatementPrepareResponsePayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(0); var statementId = reader.ReadInt32(); var columnCount = (int)reader.ReadInt16(); var parameterCount = (int)reader.ReadInt16(); reader.ReadByte(0); var warningCount = (int)reader.ReadInt16(); return(new StatementPrepareResponsePayload(statementId, columnCount, parameterCount)); }
public static ErrorPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); var errorCode = reader.ReadUInt16(); reader.ReadByte(0x23); var state = Encoding.ASCII.GetString(reader.ReadByteString(5)); var message = Encoding.UTF8.GetString(reader.ReadByteString(payload.ArraySegment.Count - 9)); return(new ErrorPayload(errorCode, state, message)); }
public void ReadByte() { var reader = new ByteArrayReader(new byte[] { 0x80, 0x80 }); Assert.Equal((byte) 0x80, reader.ReadByte()); Assert.Equal(1u, reader.Offset); Assert.Equal((sbyte) -128, reader.ReadSByte()); Assert.Throws<EndOfStreamException>(() => reader.ReadByte()); }
public static PrepareStatementOkPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(0); var statementId = reader.ReadUInt32(); int columnCount = reader.ReadUInt16(); int parameterCount = reader.ReadUInt16(); reader.ReadByte(0); int warningCount = reader.ReadUInt16(); if (reader.BytesRemaining != 0) throw new FormatException("Extra bytes at end of payload."); return new PrepareStatementOkPayload(statementId, columnCount, parameterCount, warningCount); }
public static StatementPrepareResponsePayload Create(ReadOnlySpan <byte> span) { var reader = new ByteArrayReader(span); reader.ReadByte(0); var statementId = reader.ReadInt32(); var columnCount = (int)reader.ReadInt16(); var parameterCount = (int)reader.ReadInt16(); reader.ReadByte(0); var warningCount = (int)reader.ReadInt16(); return(new StatementPrepareResponsePayload(statementId, columnCount, parameterCount)); }
public static InitialHandshakePayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(c_protocolVersion); var serverVersion = reader.ReadNullTerminatedByteString(); var connectionId = reader.ReadInt32(); byte[] authPluginData = null; var authPluginData1 = reader.ReadByteArraySegment(8); string authPluginName = null; reader.ReadByte(0); var protocolCapabilities = (ProtocolCapabilities)reader.ReadUInt16(); if (reader.BytesRemaining > 0) { var charSet = (CharacterSet)reader.ReadByte(); var status = (ServerStatus)reader.ReadInt16(); var capabilityFlagsHigh = reader.ReadUInt16(); protocolCapabilities |= (ProtocolCapabilities)(capabilityFlagsHigh << 16); var authPluginDataLength = reader.ReadByte(); var unused = reader.ReadByteArraySegment(10); if ((protocolCapabilities & ProtocolCapabilities.SecureConnection) != 0) { var authPluginData2 = reader.ReadByteArraySegment(Math.Max(13, authPluginDataLength - 8)); var concatenated = new byte[authPluginData1.Count + authPluginData2.Count]; Buffer.BlockCopy(authPluginData1.Array, authPluginData1.Offset, concatenated, 0, authPluginData1.Count); Buffer.BlockCopy(authPluginData2.Array, authPluginData2.Offset, concatenated, authPluginData1.Count, authPluginData2.Count); authPluginData = concatenated; } if ((protocolCapabilities & ProtocolCapabilities.PluginAuth) != 0) { authPluginName = Encoding.UTF8.GetString(reader.ReadNullOrEofTerminatedByteString()); } } if (authPluginData == null) { authPluginData = new byte[authPluginData1.Count]; Buffer.BlockCopy(authPluginData1.Array, authPluginData1.Offset, authPluginData, 0, authPluginData1.Count); } if (reader.BytesRemaining != 0) { throw new FormatException("Extra bytes at end of payload."); } return(new InitialHandshakePayload(protocolCapabilities, serverVersion, connectionId, authPluginData, authPluginName)); }
public override void ServerDecode( ModbusCommand command, ByteArrayReader body) { _device = body.ReadByte(); var data_len = body.ReadByte(); body.ReadByte(); body.ReadByte(); _address = body.ReadInt32BE(); _dayIndex = _address & 0x03FF; _category = (_address & 0xFC00) >> 10; command.Count = data_len; command.Data = new ushort[command.Count]; }
public override bool ServerDecode( ModbusCommand command, ByteArrayReader body) { if (ModbusCodecBase.PopRequestHeader(command, body) == false) { return(false); } var success = false; if (body.CanRead(1)) { var count = body.ReadByte(); if (body.CanRead(count)) { command.QueryTotalLength += (count + 1); count /= 2; command.Data = new ushort[count]; for (int i = 0; i < count; i++) { command.Data[i] = body.ReadUInt16BE(); } success = true; } } return(success); }
public static AuthenticationMoreDataPayload Create(ReadOnlySpan <byte> span) { var reader = new ByteArrayReader(span); reader.ReadByte(Signature); return(new AuthenticationMoreDataPayload(reader.ReadByteString(reader.BytesRemaining).ToArray())); }
public void SetFullState(ByteArrayReader reader) { reader.Offset = 0; var objectId = reader.ReadUShort(); var reflectionName = reader.ReadString(); var type = (__SerializationGameObjectType)reader.ReadByte(); var playerUid = reader.ReadUShort(); SetStateHeader(reader); if (GlobalSettings.Debug) { SetFullStateInternal(reader); } else { try { SetFullStateInternal(reader); } catch (Exception ex) { Game.Logger.Error($"GameObject full state parsing failed! {ReflectionName}[ID {ObjectId}]", ex); SetFullStateInternal(reader); } } }
internal static void PopDiscretes(ModbusCommand command, ByteArrayReader body) { byte num1 = body.ReadByte(); int count = command.Count; command.Data = new ushort[count]; command.QueryTotalLength += (int)num1 + 1; byte num2; byte num3; for (int index = 0; !body.EndOfBuffer && command.Count > index; command.Data[index++] = (ushort)((uint)num2 << 8 | (uint)num3)) { num2 = body.CanRead(1) ? body.ReadByte() : (byte)0; num3 = body.CanRead(1) ? body.ReadByte() : (byte)0; } }
public void SetFullState(ByteArrayReader reader) { var isNull = reader.ReadBool(); if (isNull) { _isNullWeapon = true; return; } else { _isNullWeapon = false; } TargetingType = (WeaponTargetingType)reader.ReadByte(); MaxDistance = reader.ReadFloat(); ProjectileType = ResolveTypeFromString(reader.ReadString()); ProjectileVelocity = reader.ReadHalfVector(); ProjectileOffset = reader.ReadHalfVector(); ProjectileRotation = reader.ReadHalf(); ProjectileRotationVelocity = reader.ReadHalf(); MaxActiveProjectileCount = reader.ReadUShort(); FireRotationIsRelativeToTankRotation = reader.ReadBool(); FireRotationIsRelativeToTankLookDirection = reader.ReadBool(); AddedRotation = reader.ReadHalf(); TransformPositionAndVelocityByRotation = reader.ReadBool(); WeaponRechargeTime = reader.ReadTimeSpan(); WeaponName = reader.ReadString(); TimeRecharged = reader.ReadTimeSpan(); _projectileArray = reader.ReadBytes(); }
public override bool ClientDecode( ModbusCommand command, ByteArrayReader body) { var success = false; if (body.CanRead(1)) { var count = body.ReadByte(); if (body.CanRead(count)) { count /= 2; command.Data = new ushort[count]; for (int i = 0; i < count; i++) { command.Data[i] = body.ReadUInt16BE(); } success = true; } } return(success); }
public static AuthenticationMoreDataPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); return(new AuthenticationMoreDataPayload(reader.ReadByteArray(reader.BytesRemaining))); }
public static AuthenticationMethodSwitchRequestPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); var name = Encoding.UTF8.GetString(reader.ReadNullTerminatedByteString()); var data = reader.ReadByteString(reader.BytesRemaining); return new AuthenticationMethodSwitchRequestPayload(name, data); }
public void EmptyArray() { var reader = new ByteArrayReader(new byte[0]); Assert.Equal(0u, reader.Length); Assert.Throws<EndOfStreamException>(() => reader.ReadByte()); Assert.Equal(0, reader.ReadBytes(new byte[10], 0, 10)); }
public static LocalInfilePayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); var fileName = Encoding.UTF8.GetString(reader.ReadByteString(reader.BytesRemaining)); return(new LocalInfilePayload(fileName)); }
public override void ClientDecode( ModbusCommand command, ByteArrayReader body) { var count = body.ReadByte() / 2; command.Data = new ushort[count]; for (int i = 0; i < count; i++) command.Data[i] = body.ReadUInt16BE(); }
public static PrepareStatementOkPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(0); var statementId = reader.ReadUInt32(); int columnCount = reader.ReadUInt16(); int parameterCount = reader.ReadUInt16(); reader.ReadByte(0); int warningCount = reader.ReadUInt16(); if (reader.BytesRemaining != 0) { throw new FormatException("Extra bytes at end of payload."); } return(new PrepareStatementOkPayload(statementId, columnCount, parameterCount, warningCount)); }
public static LocalInfilePayload Create(ReadOnlySpan <byte> span) { var reader = new ByteArrayReader(span); reader.ReadByte(Signature); var fileName = Encoding.UTF8.GetString(reader.ReadByteString(reader.BytesRemaining)); return(new LocalInfilePayload(fileName)); }
public override void ClientDecode(ModbusCommand command, ByteArrayReader body) { int length = (int)body.ReadByte() / 2; command.Data = new ushort[length]; for (int index = 0; index < length; ++index) { command.Data[index] = body.ReadUInt16BE(); } }
public static OkPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); var affectedRowCount = checked((int) reader.ReadLengthEncodedInteger()); var lastInsertId = checked((long) reader.ReadLengthEncodedInteger()); var serverStatus = (ServerStatus) reader.ReadUInt16(); var warningCount = (int) reader.ReadUInt16(); return new OkPayload(affectedRowCount, lastInsertId, serverStatus, warningCount); }
public static EofPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); if (payload.ArraySegment.Count > 5) throw new FormatException("Not an EOF packet"); int warningCount = reader.ReadUInt16(); ServerStatus serverStatus = (ServerStatus) reader.ReadUInt16(); if (reader.BytesRemaining != 0) throw new FormatException("Extra bytes at end of payload."); return new EofPayload(warningCount, serverStatus); }
public override void ServerDecode( ModbusCommand command, ByteArrayReader body) { ModbusCodecBase.PopRequestHeader( command, body); var count = body.ReadByte() / 2; command.Data = new ushort[count]; command.QueryTotalLength += (count + 3); for (int i = 0; i < count; i++) command.Data[i] = body.ReadUInt16BE(); }
private static long ReadFieldLength(ByteArrayReader reader) { var leadByte = reader.ReadByte(); switch (leadByte) { case 0xFB: return -1; case 0xFC: return reader.ReadFixedLengthUInt32(2); case 0xFD: return reader.ReadFixedLengthUInt32(3); case 0xFE: return checked((long) reader.ReadFixedLengthUInt64(8)); default: return leadByte; } }
/// <summary> /// Helper for unpacking discrete data incoming as a bit-array /// </summary> /// <param name="command"></param> /// <param name="body"></param> internal static void PopDiscretes( ModbusCommand command, ByteArrayReader body) { var byteCount = body.ReadByte(); var count = command.Count; command.Data = new ushort[count]; command.QueryTotalLength += (byteCount + 1); int k = 0; while (body.EndOfBuffer == false) { if (command.Count <= k) break; byte hb = body.CanRead(1) ? body.ReadByte() : (byte)0; byte lb = body.CanRead(1) ? body.ReadByte() : (byte)0; command.Data[k++] = (ushort)((hb << 8) | lb); //int n = count <= 8 ? count : 8; //count -= n; //for (int i = 0; i < n; i++) // command.Data[k++] = (ushort)(cell & (1 << i)); } }