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

Improve transfer speed (sending) #178

Merged
merged 11 commits into from
Jul 30, 2024
Prev Previous commit
Next Next commit
Manual bond serialization
- Removes multiple useless copies of payload
  • Loading branch information
ShortDevelopment committed Jul 29, 2024
commit bc97ed90fe37c5abb771584c98c6a8e3971d8508
2 changes: 1 addition & 1 deletion Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<ItemGroup Label="Global Dependencies">
<PackageReference Include="ShortDev.IO" Version="0.1.3" />
<PackageReference Include="ShortDev.IO" Version="0.1.4" />
</ItemGroup>

<ItemGroup Label="Global Usings">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Text;
using System.Text;

namespace ShortDev.Microsoft.ConnectedDevices.NearShare;

Expand Down Expand Up @@ -43,9 +42,9 @@ public static CdpFileProvider FromStream(string fileName, Stream stream)
public ulong FileSize
=> (ulong)_buffer.Length;

public ReadOnlySpan<byte> ReadBlob(ulong start, uint length)
public ReadOnlyMemory<byte> ReadBlob(ulong start, uint length)
{
Span<byte> buffer = new byte[length];
var buffer = new byte[length];
_buffer.Position = (long)start;
_buffer.Read(buffer);
return buffer;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using ShortDev.Microsoft.ConnectedDevices.Serialization;

namespace ShortDev.Microsoft.ConnectedDevices.NearShare.Messages;
internal static class FetchDataResponse
{
public static void Write(EndianWriter writer, uint contentId, ulong start, ReadOnlySpan<byte> blob)
{
CompactBinaryBondWriter bondWriter = new(writer.Buffer);

bondWriter.WriteFieldBegin(Bond.BondDataType.BT_MAP, 1);
bondWriter.WriteContainerBegin(count: 4, Bond.BondDataType.BT_WSTRING, Bond.BondDataType.BT_STRUCT);

WritePropertyBegin(ref bondWriter, "ControlMessage", PropertyType.PropertyType_UInt32);
bondWriter.WriteFieldBegin(Bond.BondDataType.BT_UINT32, 104);
bondWriter.WriteUInt32((uint)NearShareControlMsgType.FetchDataResponse);
bondWriter.WriteStructEnd();

WritePropertyBegin(ref bondWriter, "ContentId", PropertyType.PropertyType_UInt32);
bondWriter.WriteFieldBegin(Bond.BondDataType.BT_UINT32, 104);
bondWriter.WriteUInt32(contentId);
bondWriter.WriteStructEnd();

WritePropertyBegin(ref bondWriter, "BlobPosition", PropertyType.PropertyType_UInt64);
bondWriter.WriteFieldBegin(Bond.BondDataType.BT_UINT64, 106);
bondWriter.WriteUInt64(start);
bondWriter.WriteStructEnd();

WritePropertyBegin(ref bondWriter, "DataBlob", PropertyType.PropertyType_UInt8Array);
bondWriter.WriteFieldBegin(Bond.BondDataType.BT_LIST, 200);
bondWriter.WriteContainerBegin(blob.Length, Bond.BondDataType.BT_UINT8);
bondWriter.WriteBytes(blob);
bondWriter.WriteStructEnd();

bondWriter.WriteStructEnd();
}

static void WritePropertyBegin(ref CompactBinaryBondWriter writer, string name, PropertyType type)
{
writer.WriteWString(name);

writer.WriteFieldBegin(Bond.BondDataType.BT_INT32, 0);
writer.WriteInt32((int)type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,10 @@ void HandleDataRequest(BinaryMsgHeader header, ValueSet payload)
TotalFiles = (uint)_files.Count
});

ValueSet response = new();
response.Add("ControlMessage", (uint)NearShareControlMsgType.FetchDataResponse);
response.Add("ContentId", contentId);
response.Add("BlobPosition", start);
response.Add("DataBlob", blob.ToArray().ToList()); // ToDo: Remove allocation
SendValueSet(response, header.MessageId);
Channel.SendBinaryMessage(writer =>
{
FetchDataResponse.Write(writer, contentId, start, blob.Span);
}, header.MessageId);
}
}
}
Loading