Innovenergy_trunk/csharp/Lib/Protocols/DBus/WireFormat/DBusSocketReader.cs

40 lines
1.2 KiB
C#
Raw Permalink Normal View History

2023-02-16 12:57:06 +00:00
using InnovEnergy.Lib.Protocols.DBus.Protocol.Header;
using InnovEnergy.Lib.Protocols.DBus.Transport;
namespace InnovEnergy.Lib.Protocols.DBus.WireFormat;
internal class DBusSocketReader : DBusReader
{
private BufferedSocketReader BufferedSocketReader { get; }
public override Int32 BytesRead => BufferedSocketReader.BytesRead;
public DBusSocketReader(BufferedSocketReader bufferedSocketReader, Boolean swapEndian) : base(swapEndian)
{
BufferedSocketReader = bufferedSocketReader;
}
public DBusSocketReader(BufferedSocketReader bufferedSocketReader) :
this(bufferedSocketReader, MessageStart(bufferedSocketReader))
{
}
private static Boolean MessageStart(BufferedSocketReader socketReader)
{
Endian PeekEndian() => (Endian) socketReader.PeekByte();
while (PeekEndian() is not (Endian.Little or Endian.Big))
{
socketReader.ReadByte();
}
socketReader.Reset();
return PeekEndian() != Env.Endianness;
}
public override Byte ReadByte() => BufferedSocketReader.ReadByte();
public override ArraySegment<Byte> ReadSegment(Int32 size) => BufferedSocketReader.ReadSegment(size);
}