Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reader: add ReadSignature overloads. #315

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Tmds.DBus.Protocol/Reader.Basic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public unsafe double ReadDouble()
return value;
}

public Signature ReadSignature()
=> new Signature(ReadSignatureAsSpan());

public ReadOnlySpan<byte> ReadSignatureAsSpan()
{
int length = ReadByte();
Expand All @@ -87,6 +90,15 @@ public void ReadSignature(string expected)
}
}

public void ReadSignature(ReadOnlySpan<byte> expected)
{
ReadOnlySpan<byte> signature = ReadSignatureAsSpan();
if (!signature.SequenceEqual(expected))
{
ThrowHelper.ThrowUnexpectedSignature(signature, Encoding.UTF8.GetString(expected));
}
}

public ReadOnlySpan<byte> ReadObjectPathAsSpan() => ReadSpan();

public ObjectPath ReadObjectPath() => new ObjectPath(ReadString());
Expand All @@ -97,8 +109,6 @@ public void ReadSignature(string expected)

public string ReadString() => Encoding.UTF8.GetString(ReadSpan());

public Signature ReadSignatureAsSignature() => new Signature(ReadSignatureAsSpan());

public string ReadSignatureAsString() => Encoding.UTF8.GetString(ReadSignatureAsSpan());

private ReadOnlySpan<byte> ReadSpan()
Expand Down
2 changes: 1 addition & 1 deletion src/Tmds.DBus.Protocol/Reader.ReadT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ref partial struct Reader
}
else if (typeof(T) == typeof(Signature))
{
return (T)(object)ReadSignatureAsSignature();
return (T)(object)ReadSignature();
}
else if (typeof(T).IsAssignableTo(typeof(SafeHandle)))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Tmds.DBus.Protocol/Reader.Variant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private VariantValue ReadTypeAsVariantValue(DBusType type, ReadOnlySpan<byte> in
case DBusType.ObjectPath:
return new VariantValue(ReadObjectPath(), nesting);
case DBusType.Signature:
return new VariantValue(ReadSignatureAsSignature(), nesting);
return new VariantValue(ReadSignature(), nesting);
case DBusType.UnixFd:
int idx = (int)ReadUInt32();
return new VariantValue(_handles, idx, nesting);
Expand Down
4 changes: 2 additions & 2 deletions src/Tmds.DBus.Tool/ProtocolGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ private void AppendInterface(string name, XElement interfaceXml)
{
AppendLine($"case \"{property.Name}\":");
_indentation++;
AppendLine($"reader.ReadSignature(\"{property.Signature}\");");
AppendLine($"reader.ReadSignature(\"{property.Signature}\"u8);");
AppendLine($"props.{property.NameUpper} = {CallReadArgumentType(property.Signature)};");
AppendLine($"changedList?.Add(\"{property.NameUpper}\");");
AppendLine("break;");
Expand Down Expand Up @@ -757,7 +757,7 @@ private void AppendReadMessageMethod(string name, bool variant, Argument[] args)
AppendLine("var reader = message.GetBodyReader();");
if (variant)
{
AppendLine($"reader.ReadSignature(\"{signature}\");");
AppendLine($"reader.ReadSignature(\"{signature}\"u8);");
}
if (args.Length == 1)
{
Expand Down
Loading