Skip to content

Commit 5e16666

Browse files
Centralize send logic
1 parent d417acd commit 5e16666

33 files changed

+236
-305
lines changed

lib/ShortDev.Microsoft.ConnectedDevices/CdpSession.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using ShortDev.Microsoft.ConnectedDevices.Session.Connection;
99
using ShortDev.Microsoft.ConnectedDevices.Transports;
1010
using System.Collections.Concurrent;
11+
using System.ComponentModel;
1112

1213
namespace ShortDev.Microsoft.ConnectedDevices;
1314

@@ -96,6 +97,36 @@ internal static async Task<CdpSession> ConnectClientAsync(ConnectedDevicesPlatfo
9697
uint _sequenceNumber = 0;
9798
ulong _requestId = 0;
9899
internal CdpCryptor? Cryptor { get; set; }
100+
101+
public void SendMessage<TMessageHeader, TMessage>(
102+
CdpSocket socket,
103+
CommonHeader header, in TMessageHeader messageHeader, in TMessage message,
104+
bool supplyRequestId = false
105+
) where TMessageHeader : IBinaryWritable where TMessage : IBinaryWritable
106+
{
107+
SendMessage(socket, ref header, messageHeader, message, supplyRequestId: supplyRequestId);
108+
}
109+
110+
[EditorBrowsable(EditorBrowsableState.Never)]
111+
public void SendMessage<TMessageHeader, TMessage>(
112+
CdpSocket socket,
113+
ref CommonHeader header, in TMessageHeader messageHeader, in TMessage message,
114+
bool supplyRequestId = false
115+
) where TMessageHeader : IBinaryWritable where TMessage : IBinaryWritable
116+
{
117+
var writer = EndianWriter.Create(Endianness.BigEndian, ConnectedDevicesPlatform.MemoryPool);
118+
try
119+
{
120+
messageHeader.Write(ref writer);
121+
message.Write(ref writer);
122+
SendMessage(socket, header, writer.Stream.WrittenSpan, supplyRequestId);
123+
}
124+
finally
125+
{
126+
writer.Dispose();
127+
}
128+
}
129+
99130
public void SendMessage(CdpSocket socket, CommonHeader header, ReadOnlySpan<byte> payload, bool supplyRequestId = false)
100131
{
101132
if (header.Type == MessageType.Session && Cryptor == null)

lib/ShortDev.Microsoft.ConnectedDevices/Messages/AdditionalHeader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ShortDev.Microsoft.ConnectedDevices.Messages;
1010
/// </summary>
1111
/// <param name="Type"></param>
1212
/// <param name="Value"></param>
13-
public record AdditionalHeader(AdditionalHeaderType Type, ReadOnlyMemory<byte> Value)
13+
public readonly record struct AdditionalHeader(AdditionalHeaderType Type, ReadOnlyMemory<byte> Value)
1414
{
1515
public static AdditionalHeader CreateCorrelationHeader()
1616
=> FromCorrelationVector(new CorrelationVectorV1());

lib/ShortDev.Microsoft.ConnectedDevices/Messages/CommonHeaders.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,6 @@ public void SetReplyToId(ulong requestId)
247247
if (header == null)
248248
return null;
249249

250-
return BinaryPrimitives.ReadUInt64LittleEndian(header.Value.Span);
250+
return BinaryPrimitives.ReadUInt64LittleEndian(header.Value.Value.Span);
251251
}
252252
}

lib/ShortDev.Microsoft.ConnectedDevices/Messages/Connection/Authentication/AuthenticationPayload.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ namespace ShortDev.Microsoft.ConnectedDevices.Messages.Connection.Authentication
99
/// <summary>
1010
/// For all authentication, devices send their device / user certificate, which is self-signed.
1111
/// </summary>
12-
public sealed class AuthenticationPayload : IBinaryWritable, IBinaryParsable<AuthenticationPayload>
12+
public readonly record struct AuthenticationPayload : IBinaryWritable, IBinaryParsable<AuthenticationPayload>
1313
{
14-
private AuthenticationPayload() { }
15-
1614
public static AuthenticationPayload Parse<TReader>(ref TReader reader) where TReader : struct, IEndianReader, allows ref struct
1715
=> new()
1816
{

lib/ShortDev.Microsoft.ConnectedDevices/Messages/Connection/ConnectionHeader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
/// <summary>
44
/// The <see cref="ConnectionHeader"/> is common for all Connection Messages.
55
/// </summary>
6-
public sealed class ConnectionHeader : IBinaryWritable, IBinaryParsable<ConnectionHeader>
6+
public readonly record struct ConnectionHeader : IBinaryWritable, IBinaryParsable<ConnectionHeader>
77
{
88
/// <summary>
99
/// Indicates the current connection type.
1010
/// </summary>
11-
public required ConnectionType MessageType { get; set; }
11+
public required ConnectionType MessageType { get; init; }
1212

1313
/// <summary>
1414
/// Displays the types of available connections.
1515
/// </summary>
16-
public required ConnectionMode ConnectionMode { get; set; }
16+
public required ConnectionMode ConnectionMode { get; init; }
1717

1818
public static ConnectionHeader Parse<TReader>(ref TReader reader) where TReader : struct, IEndianReader, allows ref struct
1919
{

lib/ShortDev.Microsoft.ConnectedDevices/Messages/Connection/ConnectionRequest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,37 @@ namespace ShortDev.Microsoft.ConnectedDevices.Messages.Connection;
66
/// <summary>
77
/// Client initiates a connection request with a host device.
88
/// </summary>
9-
public sealed class ConnectionRequest : IBinaryWritable, IBinaryParsable<ConnectionRequest>
9+
public readonly record struct ConnectionRequest : IBinaryWritable, IBinaryParsable<ConnectionRequest>
1010
{
1111
/// <summary>
1212
/// The type of elliptical curve used.
1313
/// </summary>
14-
public required CurveType CurveType { get; set; }
14+
public required CurveType CurveType { get; init; }
1515
/// <summary>
1616
/// The expected size of HMAC.
1717
/// </summary>
18-
public required ushort HmacSize { get; set; }
18+
public required ushort HmacSize { get; init; }
1919
/// <summary>
2020
/// Random values
2121
/// </summary>
22-
public required CdpNonce Nonce { get; set; }
22+
public required CdpNonce Nonce { get; init; }
2323
/// <summary>
2424
/// The maximum size of a single message fragment. <br/>
2525
/// (Fixed Value of <see cref="MessageFragmenter.DefaultMessageFragmentSize"/>).
2626
/// </summary>
27-
public required uint MessageFragmentSize { get; set; }
27+
public required uint MessageFragmentSize { get; init; }
2828
/// <summary>
2929
/// A fixed-length key.
3030
/// This is the X component of the key. <br/>
3131
/// (See <see cref="System.Security.Cryptography.ECPoint.X"/>)
3232
/// </summary>
33-
public required ReadOnlyMemory<byte> PublicKeyX { get; set; }
33+
public required ReadOnlyMemory<byte> PublicKeyX { get; init; }
3434
/// <summary>
3535
/// A fixed-length key.
3636
/// This is the Y component of the key. <br/>
3737
/// (See <see cref="System.Security.Cryptography.ECPoint.Y"/>)
3838
/// </summary>
39-
public required ReadOnlyMemory<byte> PublicKeyY { get; set; }
39+
public required ReadOnlyMemory<byte> PublicKeyY { get; init; }
4040

4141
public static ConnectionRequest Parse<TReader>(ref TReader reader) where TReader : struct, IEndianReader, allows ref struct
4242
=> new()

lib/ShortDev.Microsoft.ConnectedDevices/Messages/Connection/ConnectionResponse.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,37 @@ namespace ShortDev.Microsoft.ConnectedDevices.Messages.Connection;
77
/// The host responds with a connection response message including device information. <br/>
88
/// Only the Result is sent if the Result is anything other than <see cref="ConnectionResult.Pending"/>.
99
/// </summary>
10-
public sealed class ConnectionResponse : IBinaryWritable, IBinaryParsable<ConnectionResponse>
10+
public readonly record struct ConnectionResponse : IBinaryWritable, IBinaryParsable<ConnectionResponse>
1111
{
1212
/// <summary>
1313
/// The result of the connection request.
1414
/// </summary>
15-
public required ConnectionResult Result { get; set; }
15+
public required ConnectionResult Result { get; init; }
1616
/// <summary>
1717
/// The expected size of HMAC.
1818
/// </summary>
19-
public required ushort HmacSize { get; set; }
19+
public required ushort HmacSize { get; init; }
2020
/// <summary>
2121
/// Random values.
2222
/// </summary>
23-
public required CdpNonce Nonce { get; set; }
23+
public required CdpNonce Nonce { get; init; }
2424
/// <summary>
2525
/// The maximum size of a single message fragment. <br/>
2626
/// (Fixed Value of <see cref="MessageFragmenter.DefaultMessageFragmentSize"/>).
2727
/// </summary>
28-
public required uint MessageFragmentSize { get; set; }
28+
public required uint MessageFragmentSize { get; init; }
2929
/// <summary>
3030
/// A fixed-length key that is based on the <see cref="CurveType"/> from <see cref="ConnectionRequest"/>, which is sent only if the connection is successful. <br/>
3131
/// This is the X component of the key. <br/>
3232
/// (See <see cref="System.Security.Cryptography.ECPoint.X"/>)
3333
/// </summary>
34-
public required ReadOnlyMemory<byte> PublicKeyX { get; set; }
34+
public required ReadOnlyMemory<byte> PublicKeyX { get; init; }
3535
/// <summary>
3636
/// A fixed-length key that is based on the <see cref="CurveType"/> from <see cref="ConnectionRequest"/>, which is sent only if the connection is successful. <br/>
3737
/// This is the Y component of the key.
3838
/// (See <see cref="System.Security.Cryptography.ECPoint.Y"/>)
3939
/// </summary>
40-
public required ReadOnlyMemory<byte> PublicKeyY { get; set; }
40+
public required ReadOnlyMemory<byte> PublicKeyY { get; init; }
4141

4242
public static ConnectionResponse Parse<TReader>(ref TReader reader) where TReader : struct, IEndianReader, allows ref struct
4343
{

lib/ShortDev.Microsoft.ConnectedDevices/Messages/Connection/DeviceInfo/DeviceInfoMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace ShortDev.Microsoft.ConnectedDevices.Messages.Connection.DeviceInfo;
66
/// <summary>
77
/// This message requests information from the device.
88
/// </summary>
9-
public sealed class DeviceInfoMessage : IBinaryWritable, IBinaryParsable<DeviceInfoMessage>
9+
public readonly record struct DeviceInfoMessage : IBinaryWritable, IBinaryParsable<DeviceInfoMessage>
1010
{
1111
public static DeviceInfoMessage Parse<TReader>(ref TReader reader) where TReader : struct, IEndianReader, allows ref struct
1212
=> new()

lib/ShortDev.Microsoft.ConnectedDevices/Messages/Connection/TransportUpgrade/EndpointMetadata.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ShortDev.Microsoft.ConnectedDevices.Messages.Connection.TransportUpgrade;
44

5-
public record class EndpointMetadata(CdpTransportType Type, byte[] Data) : IBinaryWritable, IBinaryParsable<EndpointMetadata>
5+
public readonly record struct EndpointMetadata(CdpTransportType Type, byte[] Data) : IBinaryWritable, IBinaryParsable<EndpointMetadata>
66
{
77
public static EndpointMetadata Tcp { get; } = new(CdpTransportType.Tcp, []);
88
public static EndpointMetadata WifiDirect { get; } = new(CdpTransportType.WifiDirect, []);
@@ -40,3 +40,11 @@ public static void WriteArray<TWriter>(ref TWriter writer, IReadOnlyList<Endpoin
4040
endpoint.Write(ref writer);
4141
}
4242
}
43+
44+
public readonly record struct EndpointMetadataArray(IReadOnlyList<EndpointMetadata> Endpoints) : IBinaryWritable, IBinaryParsable<EndpointMetadataArray>
45+
{
46+
public void Write<TWriter>(ref TWriter writer) where TWriter : struct, IEndianWriter, allows ref struct
47+
=> EndpointMetadata.WriteArray(ref writer, Endpoints);
48+
public static EndpointMetadataArray Parse<TReader>(ref TReader reader) where TReader : struct, IEndianReader, allows ref struct
49+
=> new(EndpointMetadata.ParseArray(ref reader));
50+
}

lib/ShortDev.Microsoft.ConnectedDevices/Messages/Connection/TransportUpgrade/UpgradeIdPayload.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/// This message transports the details of the upgrade. <br/>
55
/// (See <see cref="ConnectionType.TransportRequest"/> and <see cref="ConnectionType.TransportConfirmation"/>)
66
/// </summary>
7-
public sealed class UpgradeIdPayload : IBinaryWritable, IBinaryParsable<UpgradeIdPayload>
7+
public readonly record struct UpgradeIdPayload : IBinaryWritable, IBinaryParsable<UpgradeIdPayload>
88
{
99
public required Guid UpgradeId { get; init; }
1010

0 commit comments

Comments
 (0)