From 84a943c5f8a2d250fe23466c5c6308ce0bf4fc93 Mon Sep 17 00:00:00 2001 From: Lukas Kurz Date: Fri, 10 Feb 2023 20:51:34 +0100 Subject: [PATCH] Fix android to android Fixes #37 --- .../Internal/NearShareApp.cs | 10 +++++---- .../Internal/NearShareHandshakeApp.cs | 4 ++-- .../NearShareSender.cs | 2 +- .../CdpAppBase.cs | 4 ++-- .../CdpChannel.cs | 19 +++++++++------- .../Messages/CdpMessage.cs | 22 ++++++++++++++----- ...onFragmentHeader.cs => BinaryMsgHeader.cs} | 17 +++++++------- Test/Program.cs | 7 +++--- 8 files changed, 50 insertions(+), 35 deletions(-) rename ShortDev.Microsoft.ConnectedDevices/Messages/Session/{SessionFragmentHeader.cs => BinaryMsgHeader.cs} (50%) diff --git a/ShortDev.Microsoft.ConnectedDevices.NearShare/Internal/NearShareApp.cs b/ShortDev.Microsoft.ConnectedDevices.NearShare/Internal/NearShareApp.cs index b50f82a..4ddf1e6 100644 --- a/ShortDev.Microsoft.ConnectedDevices.NearShare/Internal/NearShareApp.cs +++ b/ShortDev.Microsoft.ConnectedDevices.NearShare/Internal/NearShareApp.cs @@ -16,9 +16,11 @@ internal sealed class NearShareApp : CdpAppBase const uint PartitionSize = 102400u; // 131072u + uint _messageId = 0; public override void HandleMessage(CdpMessage msg) { - var payload = ValueSet.Parse(msg.Read()); + var payload = ValueSet.Parse(msg.ReadBinary(out var binaryHeader)); + _messageId = binaryHeader.MessageId; if (!payload.ContainsKey("ControlMessage")) throw new InvalidDataException(); @@ -146,14 +148,14 @@ void RequestBlob(ulong requestedPosition, uint size = PartitionSize) request.Add("BlobPosition", requestedPosition); request.Add("BlobSize", size); request.Add("ContentId", 0u); - SendValueSet(request); + SendValueSet(request, _messageId); } void OnCancel() { ValueSet request = new(); request.Add("ControlMessage", (uint)NearShareControlMsgType.CancelTransfer); - SendValueSet(request); + SendValueSet(request, _messageId); CloseChannel(); } @@ -162,7 +164,7 @@ void OnCompleted() { ValueSet request = new(); request.Add("ControlMessage", (uint)NearShareControlMsgType.CompleteTransfer); - SendValueSet(request); + SendValueSet(request, _messageId); CloseChannel(); } diff --git a/ShortDev.Microsoft.ConnectedDevices.NearShare/Internal/NearShareHandshakeApp.cs b/ShortDev.Microsoft.ConnectedDevices.NearShare/Internal/NearShareHandshakeApp.cs index 67509d3..a98a9ac 100644 --- a/ShortDev.Microsoft.ConnectedDevices.NearShare/Internal/NearShareHandshakeApp.cs +++ b/ShortDev.Microsoft.ConnectedDevices.NearShare/Internal/NearShareHandshakeApp.cs @@ -13,7 +13,7 @@ public class NearShareHandshakeApp : CdpAppBase, ICdpAppId public override void HandleMessage(CdpMessage msg) { - var payload = ValueSet.Parse(msg.Read()); + var payload = ValueSet.Parse(msg.ReadBinary(out _)); string id = payload.Get("OperationId").ToString(); CdpAppRegistration.RegisterApp( @@ -29,7 +29,7 @@ public override void HandleMessage(CdpMessage msg) ValueSet response = new(); response.Add("SelectedPlatformVersion", 1u); response.Add("VersionHandShakeResult", 1u); - SendValueSet(response); + SendValueSet(response, msgId: 0); Channel.Dispose(); } diff --git a/ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareSender.cs b/ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareSender.cs index 305da7d..97cb7d0 100644 --- a/ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareSender.cs +++ b/ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareSender.cs @@ -53,7 +53,7 @@ public static void StartHandshake(CdpChannel channel, Guid operationId) msg.Add("MaxPlatformVersion", 1u); msg.Add("MinPlatformVersion", 1u); msg.Add("OperationId", operationId); - channel.SendMessage(msg.Write); + channel.SendBinaryMessage(msg.Write, msgId: 0); } public void HandleMessage(CdpMessage msg) diff --git a/ShortDev.Microsoft.ConnectedDevices/CdpAppBase.cs b/ShortDev.Microsoft.ConnectedDevices/CdpAppBase.cs index cf202d3..d181e20 100644 --- a/ShortDev.Microsoft.ConnectedDevices/CdpAppBase.cs +++ b/ShortDev.Microsoft.ConnectedDevices/CdpAppBase.cs @@ -22,8 +22,8 @@ public abstract class CdpAppBase : IChannelMessageHandler public abstract void HandleMessage(CdpMessage msg); - protected void SendValueSet(ValueSet request) - => Channel.SendMessage(request.Write); + protected void SendValueSet(ValueSet request, uint msgId) + => Channel.SendBinaryMessage(request.Write, msgId); protected virtual void CloseChannel() { diff --git a/ShortDev.Microsoft.ConnectedDevices/CdpChannel.cs b/ShortDev.Microsoft.ConnectedDevices/CdpChannel.cs index 9a99d0f..38a115a 100644 --- a/ShortDev.Microsoft.ConnectedDevices/CdpChannel.cs +++ b/ShortDev.Microsoft.ConnectedDevices/CdpChannel.cs @@ -49,6 +49,16 @@ internal CdpChannel(CdpSession session, ulong channelId, IChannelMessageHandler public void HandleMessageAsync(CdpMessage msg) => MessageHandler.HandleMessage(msg); + public void SendBinaryMessage(BodyCallback bodyCallback, uint msgId) + => SendMessage(writer => + { + new BinaryMsgHeader() + { + MessageId = msgId + }.Write(writer); + bodyCallback(writer); + }); + public void SendMessage(BodyCallback bodyCallback) { CommonHeader header = new() @@ -57,14 +67,7 @@ public void SendMessage(BodyCallback bodyCallback) ChannelId = ChannelId }; - Session.SendMessage(Socket, header, writer => - { - new SessionFragmentHeader() - { - MessageId = 0 - }.Write(writer); - bodyCallback(writer); - }); + Session.SendMessage(Socket, header, bodyCallback); } void IDisposable.Dispose() diff --git a/ShortDev.Microsoft.ConnectedDevices/Messages/CdpMessage.cs b/ShortDev.Microsoft.ConnectedDevices/Messages/CdpMessage.cs index 3d6a27a..f970eb8 100644 --- a/ShortDev.Microsoft.ConnectedDevices/Messages/CdpMessage.cs +++ b/ShortDev.Microsoft.ConnectedDevices/Messages/CdpMessage.cs @@ -41,18 +41,28 @@ public void AddFragment(ReadOnlySpan fragment) } #endregion - public SessionFragmentHeader? FragmentHeader { get; private set; } - public EndianReader Read() { - if (!IsComplete) - throw new InvalidOperationException("Wait for completion"); + ThrowIfNotCompleted(); + + return new(Endianness.BigEndian, _buffer.AsSpan()); + } + + public EndianReader ReadBinary(out BinaryMsgHeader header) + { + ThrowIfNotCompleted(); - EndianReader reader = new(Endianness.BigEndian, _buffer.AsSpan()); - FragmentHeader = SessionFragmentHeader.Parse(reader); + var reader = Read(); + header = BinaryMsgHeader.Parse(reader); return reader; } + void ThrowIfNotCompleted() + { + if (!IsComplete) + throw new InvalidOperationException("Wait for completion"); + } + public void Dispose() { } diff --git a/ShortDev.Microsoft.ConnectedDevices/Messages/Session/SessionFragmentHeader.cs b/ShortDev.Microsoft.ConnectedDevices/Messages/Session/BinaryMsgHeader.cs similarity index 50% rename from ShortDev.Microsoft.ConnectedDevices/Messages/Session/SessionFragmentHeader.cs rename to ShortDev.Microsoft.ConnectedDevices/Messages/Session/BinaryMsgHeader.cs index 20bd00f..71f2ec0 100644 --- a/ShortDev.Microsoft.ConnectedDevices/Messages/Session/SessionFragmentHeader.cs +++ b/ShortDev.Microsoft.ConnectedDevices/Messages/Session/BinaryMsgHeader.cs @@ -1,5 +1,4 @@ using ShortDev.Networking; -using System.IO; namespace ShortDev.Microsoft.ConnectedDevices.Messages.Session; @@ -7,18 +6,18 @@ namespace ShortDev.Microsoft.ConnectedDevices.Messages.Session; /// cdp.dll!cdp::BinaryFragmenter::GetMessageFragments
/// /// -public sealed class SessionFragmentHeader : ICdpHeader +public sealed class BinaryMsgHeader : ICdpHeader { - public int FragmentCount { get; set; } = 1; - public int FragmentIndex { get; set; } = 0; - public required int MessageId { get; set; } + public uint FragmentCount { get; set; } = 1; + public uint FragmentIndex { get; set; } = 0; + public required uint MessageId { get; set; } - public static SessionFragmentHeader Parse(EndianReader reader) + public static BinaryMsgHeader Parse(EndianReader reader) => new() { - FragmentCount = reader.ReadInt32(), - FragmentIndex = reader.ReadInt32(), - MessageId = reader.ReadInt32(), + FragmentCount = reader.ReadUInt32(), + FragmentIndex = reader.ReadUInt32(), + MessageId = reader.ReadUInt32(), }; public void Write(EndianWriter writer) diff --git a/Test/Program.cs b/Test/Program.cs index ffdd365..f84fab1 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -3,6 +3,8 @@ using ShortDev.Microsoft.ConnectedDevices.Messages.Connection; using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.Authentication; using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.DeviceInfo; +using ShortDev.Microsoft.ConnectedDevices.Messages.Session; +using ShortDev.Microsoft.ConnectedDevices.Serialization; using ShortDev.Networking; using Spectre.Console; @@ -59,9 +61,8 @@ void HandleMessage(CommonHeader header, EndianReader reader) } else if (header.Type == MessageType.Session) { - // reader.PrintPayload(); - //var prepend = reader.ReadBytes(0x0000000C); - //var valueSet = ValueSet.Parse(reader.BaseStream); + BinaryMsgHeader binaryHeader = BinaryMsgHeader.Parse(reader); + var valueSet = ValueSet.Parse(reader); } else {