diff --git a/lib/ShortDev.Microsoft.ConnectedDevices.NearShare/Apps/NearShareApp.cs b/lib/ShortDev.Microsoft.ConnectedDevices.NearShare/Apps/NearShareApp.cs index b3da343..3bfe497 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices.NearShare/Apps/NearShareApp.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices.NearShare/Apps/NearShareApp.cs @@ -67,7 +67,7 @@ void HandleStartTransfer(CdpMessage msg, ValueSet payload) } _fileTransferToken = new() { - DeviceName = Channel.Session.Device.Name, + DeviceName = Channel.Session.DeviceName, TotalBytes = bytesToSend, Files = files }; @@ -88,7 +88,7 @@ void HandleStartTransfer(CdpMessage msg, ValueSet payload) NearShareReceiver.OnReceivedUri(new() { - DeviceName = Channel.Session.Device.Name, + DeviceName = Channel.Session.DeviceName, Uri = uri }); diff --git a/lib/ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareSender.cs b/lib/ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareSender.cs index c5cd0d7..5d39172 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareSender.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices.NearShare/NearShareSender.cs @@ -3,7 +3,6 @@ using ShortDev.Microsoft.ConnectedDevices.Messages.Session; using ShortDev.Microsoft.ConnectedDevices.NearShare.Apps; using ShortDev.Microsoft.ConnectedDevices.NearShare.Messages; -using ShortDev.Microsoft.ConnectedDevices.Platforms; using ShortDev.Microsoft.ConnectedDevices.Serialization; namespace ShortDev.Microsoft.ConnectedDevices.NearShare; @@ -12,9 +11,9 @@ public sealed class NearShareSender(ConnectedDevicesPlatform platform) { public ConnectedDevicesPlatform Platform { get; } = platform; - async Task PrepareTransferInternalAsync(CdpDevice device, CancellationToken cancellationToken) + async Task PrepareTransferInternalAsync(EndpointInfo endpoint, CancellationToken cancellationToken) { - var session = await Platform.ConnectAsync(device); + var session = await Platform.ConnectAsync(endpoint); Guid operationId = Guid.NewGuid(); @@ -32,7 +31,7 @@ async Task PrepareTransferInternalAsync(CdpDevice device, Ca public async Task SendUriAsync(CdpDevice device, Uri uri, CancellationToken cancellationToken = default) { - using var senderStateMachine = await PrepareTransferInternalAsync(device, cancellationToken); + using var senderStateMachine = await PrepareTransferInternalAsync(device.Endpoint, cancellationToken); await senderStateMachine.SendUriAsync(uri); } @@ -41,7 +40,7 @@ public async Task SendFileAsync(CdpDevice device, CdpFileProvider file, IProgres public async Task SendFilesAsync(CdpDevice device, IReadOnlyList files, IProgress progress, CancellationToken cancellationToken = default) { - using var senderStateMachine = await PrepareTransferInternalAsync(device, cancellationToken); + using var senderStateMachine = await PrepareTransferInternalAsync(device.Endpoint, cancellationToken); await senderStateMachine.SendFilesAsync(files, progress, cancellationToken); } diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/CdpChannel.cs b/lib/ShortDev.Microsoft.ConnectedDevices/CdpChannel.cs index 174e5ea..5b30378 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/CdpChannel.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/CdpChannel.cs @@ -1,7 +1,7 @@ using ShortDev.Microsoft.ConnectedDevices.Messages; using ShortDev.Microsoft.ConnectedDevices.Messages.Control; using ShortDev.Microsoft.ConnectedDevices.Messages.Session; -using ShortDev.Microsoft.ConnectedDevices.Platforms; +using ShortDev.Microsoft.ConnectedDevices.Transports; using System; using System.Collections.Generic; diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/CdpDevice.cs b/lib/ShortDev.Microsoft.ConnectedDevices/CdpDevice.cs similarity index 74% rename from lib/ShortDev.Microsoft.ConnectedDevices/Platforms/CdpDevice.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/CdpDevice.cs index 770581f..2dbec9f 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/CdpDevice.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/CdpDevice.cs @@ -1,6 +1,4 @@ -using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.TransportUpgrade; - -namespace ShortDev.Microsoft.ConnectedDevices.Platforms; +namespace ShortDev.Microsoft.ConnectedDevices; public record CdpDevice(string Name, DeviceType Type, EndpointInfo Endpoint) { diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Messages/Connection/DeviceInfo/CdpDeviceInfo.cs b/lib/ShortDev.Microsoft.ConnectedDevices/CdpDeviceInfo.cs similarity index 75% rename from lib/ShortDev.Microsoft.ConnectedDevices/Messages/Connection/DeviceInfo/CdpDeviceInfo.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/CdpDeviceInfo.cs index 20d5dea..1a2bbdc 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Messages/Connection/DeviceInfo/CdpDeviceInfo.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/CdpDeviceInfo.cs @@ -1,8 +1,7 @@ -using System.Collections.Generic; +using ShortDev.Microsoft.ConnectedDevices.Messages.Connection; using System.Text.Json.Serialization; -using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.TransportUpgrade; -namespace ShortDev.Microsoft.ConnectedDevices.Messages.Connection.DeviceInfo; +namespace ShortDev.Microsoft.ConnectedDevices; public sealed record CdpDeviceInfo { diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/CdpLog.cs b/lib/ShortDev.Microsoft.ConnectedDevices/CdpLog.cs index 8c52e46..5bf09b0 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/CdpLog.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/CdpLog.cs @@ -1,7 +1,5 @@ using Microsoft.Extensions.Logging; using ShortDev.Microsoft.ConnectedDevices.Messages.Connection; -using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.DeviceInfo; -using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.TransportUpgrade; using ShortDev.Microsoft.ConnectedDevices.Messages.Control; using ShortDev.Microsoft.ConnectedDevices.Transports; using System; @@ -25,8 +23,8 @@ internal static partial class CdpLog public static partial void ListeningStopped(this ILogger logger); - [LoggerMessage(EventId = 105, Level = LogLevel.Information, Message = "Device {DeviceName} connected with endpoint {Endpoint}")] - public static partial void DeviceConnected(this ILogger logger, string deviceName, EndpointInfo endpoint); + [LoggerMessage(EventId = 105, Level = LogLevel.Information, Message = "New socket from endpoint {Endpoint}")] + public static partial void NewSocket(this ILogger logger, EndpointInfo endpoint); diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/CdpSession.cs b/lib/ShortDev.Microsoft.ConnectedDevices/CdpSession.cs index be81b73..30a38d5 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/CdpSession.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/CdpSession.cs @@ -7,7 +7,7 @@ using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.Authentication; using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.DeviceInfo; using ShortDev.Microsoft.ConnectedDevices.Messages.Control; -using ShortDev.Microsoft.ConnectedDevices.Platforms; +using ShortDev.Microsoft.ConnectedDevices.Transports; using System.Collections.Concurrent; namespace ShortDev.Microsoft.ConnectedDevices; @@ -26,27 +26,29 @@ public sealed class CdpSession : IDisposable public PeerCapabilities ClientCapabilities { get; private set; } = 0; public ConnectedDevicesPlatform Platform { get; } - public CdpDevice Device { get; private set; } + public CdpDeviceInfo? DeviceInfo { get; private set; } + public string DeviceName => DeviceInfo?.Name ?? "UNKNOWN"; + public EndpointInfo Endpoint { get; private set; } readonly ILogger _logger; readonly UpgradeHandler _upgradeHandler; readonly ConnectHandler _connectHandler; - private CdpSession(ConnectedDevicesPlatform platform, CdpDevice device, SessionId sessionId) + private CdpSession(ConnectedDevicesPlatform platform, EndpointInfo initialEndpoint, SessionId sessionId) { Platform = platform; - Device = device; + Endpoint = initialEndpoint; SessionId = sessionId; _logger = platform.CreateLogger(); - _upgradeHandler = new(this, device); + _upgradeHandler = new(this, initialEndpoint); _connectHandler = new(this, _upgradeHandler); } #region Registration static readonly AutoKeyRegistry _sessionRegistry = []; - internal static CdpSession GetOrCreate(ConnectedDevicesPlatform platform, CdpDevice device, CommonHeader header) + internal static CdpSession GetOrCreate(ConnectedDevicesPlatform platform, EndpointInfo initialEndpoint, CommonHeader header) { - ArgumentNullException.ThrowIfNull(device); + ArgumentNullException.ThrowIfNull(initialEndpoint); ArgumentNullException.ThrowIfNull(header); var (_, localSessionId, remoteSessionId) = SessionId.Parse(header.SessionId); @@ -74,7 +76,7 @@ internal static CdpSession GetOrCreate(ConnectedDevicesPlatform platform, CdpDev // Create return _sessionRegistry.Create(localSessionId => new( platform, - device, + initialEndpoint, sessionId: new(IsHost: true, localSessionId, remoteSessionId) ), out _); } @@ -83,7 +85,7 @@ internal static async Task CreateClientAndConnectAsync(ConnectedDevi { var session = _sessionRegistry.Create(localSessionId => new( platform, - socket.RemoteDevice, + socket.Endpoint, sessionId: new(IsHost: false, localSessionId) ), out _); @@ -175,7 +177,7 @@ public void HandleMessage(CdpSocket socket, CommonHeader header, ref EndianReade } if (!_upgradeHandler.IsSocketAllowed(socket)) - throw UnexpectedMessage(socket.RemoteDevice.Endpoint.Address); + throw UnexpectedMessage(socket.Endpoint.Address); if (header.Type == MessageType.Control) { @@ -255,7 +257,7 @@ public void HandleConnect(CdpSocket socket, CommonHeader header, ref EndianReade return; if (!_upgradeHandler.IsSocketAllowed(socket)) - throw UnexpectedMessage(socket.RemoteDevice.Endpoint.Address); + throw UnexpectedMessage(socket.Endpoint.Address); if (connectionHeader.MessageType == ConnectionType.ConnectRequest) { @@ -428,7 +430,7 @@ async void PrepareSession(CdpSocket socket) socket = await _upgradeHandler.RequestUpgradeAsync(oldSocket); oldSocket.Dispose(); - _session.Device = socket.RemoteDevice; + _session.Endpoint = socket.Endpoint; } catch (Exception ex) { @@ -495,6 +497,8 @@ void HandleDeviceInfoMessage(CommonHeader header, ref EndianReader reader, CdpSo var msg = DeviceInfoMessage.Parse(ref reader); _logger.ReceivedDeviceInfo(msg.DeviceInfo); + _session.DeviceInfo = msg.DeviceInfo; + header.Flags = 0; _session.SendMessage(socket, header, (writer) => { @@ -637,7 +641,7 @@ public async Task StartClientChannelAsync(string appId, string appNa if (IsHost) throw new InvalidOperationException("Session is not a client"); - var socket = await Platform.CreateSocketAsync(Device); + var socket = await Platform.CreateSocketAsync(Endpoint); return await StartClientChannelAsync(appId, appName, handler, socket, cancellationToken); } diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/ConnectedDevicesPlatform.cs b/lib/ShortDev.Microsoft.ConnectedDevices/ConnectedDevicesPlatform.cs index 77b8197..050dd94 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/ConnectedDevicesPlatform.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/ConnectedDevicesPlatform.cs @@ -1,9 +1,6 @@ using Microsoft.Extensions.Logging; using ShortDev.Microsoft.ConnectedDevices.Encryption; using ShortDev.Microsoft.ConnectedDevices.Messages; -using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.DeviceInfo; -using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.TransportUpgrade; -using ShortDev.Microsoft.ConnectedDevices.Platforms; using ShortDev.Microsoft.ConnectedDevices.Transports; using System.Buffers; using System.Collections.Concurrent; @@ -87,7 +84,7 @@ public async void Listen(CancellationToken cancellationToken) private void OnDeviceConnected(ICdpTransport sender, CdpSocket socket) { - _logger.DeviceConnected(socket.RemoteDevice.Name, socket.RemoteDevice.Endpoint); + _logger.NewSocket(socket.Endpoint); ReceiveLoop(socket); } #endregion @@ -121,33 +118,33 @@ public async void Discover(CancellationToken cancellationToken) } } - public async Task ConnectAsync(CdpDevice device) + public async Task ConnectAsync(EndpointInfo endpoint) { - var socket = await CreateSocketAsync(device); + var socket = await CreateSocketAsync(endpoint); return await CdpSession.CreateClientAndConnectAsync(this, socket); } - internal async Task CreateSocketAsync(CdpDevice device) + internal async Task CreateSocketAsync(EndpointInfo endpoint) { - if (TryGetKnownSocket(device.Endpoint, out var knownSocket)) + if (TryGetKnownSocket(endpoint, out var knownSocket)) return knownSocket; - var transport = TryGetTransport(device.Endpoint.TransportType) ?? throw new InvalidOperationException($"No single transport found for type {device.Endpoint.TransportType}"); - var socket = await transport.ConnectAsync(device); + var transport = TryGetTransport(endpoint.TransportType) ?? throw new InvalidOperationException($"No single transport found for type {endpoint.TransportType}"); + var socket = await transport.ConnectAsync(endpoint); ReceiveLoop(socket); return socket; } - internal async Task TryCreateSocketAsync(CdpDevice device, TimeSpan connectTimeout) + internal async Task TryCreateSocketAsync(EndpointInfo endpoint, TimeSpan connectTimeout) { - if (TryGetKnownSocket(device.Endpoint, out var knownSocket)) + if (TryGetKnownSocket(endpoint, out var knownSocket)) return knownSocket; - var transport = TryGetTransport(device.Endpoint.TransportType); + var transport = TryGetTransport(endpoint.TransportType); if (transport == null) return null; - var socket = await transport.TryConnectAsync(device, connectTimeout); + var socket = await transport.TryConnectAsync(endpoint, connectTimeout); if (socket == null) return null; @@ -177,7 +174,7 @@ private void ReceiveLoop(CdpSocket socket) session = CdpSession.GetOrCreate( this, - socket.RemoteDevice ?? throw new InvalidDataException(), + socket.Endpoint, header ); @@ -215,11 +212,11 @@ void RegisterKnownSocket(CdpSocket socket) socket.Disposed += OnSocketClosed; void OnSocketClosed() { - _knownSockets.TryRemove(socket.RemoteDevice.Endpoint, out _); // ToDo: We might remove a newer socket here!! + _knownSockets.TryRemove(socket.Endpoint, out _); // ToDo: We might remove a newer socket here!! socket.Disposed -= OnSocketClosed; } - _knownSockets.AddOrUpdate(socket.RemoteDevice.Endpoint, socket, (key, current) => + _knownSockets.AddOrUpdate(socket.Endpoint, socket, (key, current) => { // ToDo: Alive check return socket; diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Messages/Connection/TransportUpgrade/EndpointInfo.cs b/lib/ShortDev.Microsoft.ConnectedDevices/EndpointInfo.cs similarity index 92% rename from lib/ShortDev.Microsoft.ConnectedDevices/Messages/Connection/TransportUpgrade/EndpointInfo.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/EndpointInfo.cs index 3fd17aa..fecedaf 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Messages/Connection/TransportUpgrade/EndpointInfo.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/EndpointInfo.cs @@ -1,10 +1,9 @@ using ShortDev.Microsoft.ConnectedDevices.Transports; -using System; using System.Net; using System.Net.NetworkInformation; using System.Text.Json.Serialization; -namespace ShortDev.Microsoft.ConnectedDevices.Messages.Connection.TransportUpgrade; +namespace ShortDev.Microsoft.ConnectedDevices; public record class EndpointInfo( [property: JsonPropertyName("endpointType")] CdpTransportType TransportType, diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Internal/ConcurrentList.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Internal/SynchronizedList.cs similarity index 61% rename from lib/ShortDev.Microsoft.ConnectedDevices/Internal/ConcurrentList.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/Internal/SynchronizedList.cs index 2f97cc6..0842b41 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Internal/ConcurrentList.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Internal/SynchronizedList.cs @@ -1,8 +1,8 @@ -using System.Collections.Generic; +using System.Collections; namespace ShortDev.Microsoft.ConnectedDevices.Internal; -internal sealed class ConcurrentList +internal sealed class SynchronizedList : IEnumerable { readonly List _data = []; @@ -23,4 +23,10 @@ public bool Contains(T item) lock (_data) return _data.Contains(item); } + + public IEnumerator GetEnumerator() + => _data.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() + => _data.GetEnumerator(); } diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Internal/UpgradeHandler.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Internal/UpgradeHandler.cs index 47df1af..b1ea3da 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Internal/UpgradeHandler.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Internal/UpgradeHandler.cs @@ -2,33 +2,26 @@ using ShortDev.Microsoft.ConnectedDevices.Messages; using ShortDev.Microsoft.ConnectedDevices.Messages.Connection; using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.TransportUpgrade; -using ShortDev.Microsoft.ConnectedDevices.Platforms; using ShortDev.Microsoft.ConnectedDevices.Transports; +using ShortDev.Microsoft.ConnectedDevices.Transports.Network; using System.Diagnostics; using System.Runtime.CompilerServices; namespace ShortDev.Microsoft.ConnectedDevices.Internal; -internal sealed class UpgradeHandler +internal sealed class UpgradeHandler(CdpSession session, EndpointInfo initialEndpoint) { - readonly ILogger _logger; - readonly CdpSession _session; - public UpgradeHandler(CdpSession session, CdpDevice initalDevice) - { - _session = session; - _logger = session.Platform.CreateLogger(); + readonly CdpSession _session = session; + readonly ILogger _logger = session.Platform.CreateLogger(); - // Initial address is always allowed - _allowedAddresses.Add(initalDevice.Endpoint.Address); - } + // Initial address is always allowed + readonly SynchronizedList _allowedAddresses = [initialEndpoint.Address]; + public bool IsSocketAllowed(CdpSocket socket) + => _allowedAddresses.Contains(socket.Endpoint.Address); #region Policies public bool IsUpgradeSupported => (/* ToDo: header131Value & */ _session.ClientCapabilities & _session.HostCapabilities & PeerCapabilities.UpgradeSupport) != 0; - - readonly ConcurrentList _allowedAddresses = new(); - public bool IsSocketAllowed(CdpSocket socket) - => _allowedAddresses.Contains(socket.RemoteDevice.Endpoint.Address); #endregion public bool TryHandleConnect(CdpSocket socket, ConnectionHeader connectionHeader, ref EndianReader reader) @@ -80,7 +73,7 @@ public bool TryHandleConnect(CdpSocket socket, ConnectionHeader connectionHeader } #region Host - readonly ConcurrentList _upgradeIds = new(); + readonly SynchronizedList _upgradeIds = []; void HandleTransportRequest(CdpSocket socket, ref EndianReader reader) { var msg = UpgradeIdPayload.Parse(ref reader); @@ -91,7 +84,7 @@ void HandleTransportRequest(CdpSocket socket, ref EndianReader reader) if (!allowed && _upgradeIds.Contains(msg.UpgradeId)) { // No we have confirmed that this address belongs to the same device (different transport) - _allowedAddresses.Add(socket.RemoteDevice.Endpoint.Address); + _allowedAddresses.Add(socket.Endpoint.Address); _upgradeIds.Remove(msg.UpgradeId); allowed = true; @@ -268,7 +261,7 @@ async void FindNewEndpoint() if (!int.TryParse(endpoint.Service, out var port)) return null; - return await _session.Platform.TryCreateSocketAsync(_session.Device.WithEndpoint(endpoint), UpgradeInstance.Timeout); + return await _session.Platform.TryCreateSocketAsync(endpoint, UpgradeInstance.Timeout); })); if (_currentUpgrade == null) @@ -314,7 +307,7 @@ void HandleUpgradeFinalizationResponse() Debug.Assert(_currentUpgrade.NewSocket != null); // Allow the new address - _allowedAddresses.Add(_currentUpgrade.NewSocket.RemoteDevice.Endpoint.Address); + _allowedAddresses.Add(_currentUpgrade.NewSocket.Endpoint.Address); // Request transport permission for new socket _session.SendMessage(_currentUpgrade.NewSocket, new() diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/LocalDeviceInfo.cs b/lib/ShortDev.Microsoft.ConnectedDevices/LocalDeviceInfo.cs similarity index 87% rename from lib/ShortDev.Microsoft.ConnectedDevices/Platforms/LocalDeviceInfo.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/LocalDeviceInfo.cs index f772f0a..0bc301e 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/LocalDeviceInfo.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/LocalDeviceInfo.cs @@ -1,13 +1,11 @@ -using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.DeviceInfo; -using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.TransportUpgrade; -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Text; -namespace ShortDev.Microsoft.ConnectedDevices.Platforms; +namespace ShortDev.Microsoft.ConnectedDevices; public sealed class LocalDeviceInfo { diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Messages/Discovery/PresenceResponse.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Messages/Discovery/PresenceResponse.cs index a57a8dd..83a9c29 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Messages/Discovery/PresenceResponse.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Messages/Discovery/PresenceResponse.cs @@ -1,5 +1,4 @@ using ShortDev.Microsoft.ConnectedDevices.Messages.Connection; -using ShortDev.Microsoft.ConnectedDevices.Platforms; using System.Security.Cryptography; namespace ShortDev.Microsoft.ConnectedDevices.Messages.Discovery; diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Bluetooth/AdvertiseOptions.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/AdvertiseOptions.cs similarity index 54% rename from lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Bluetooth/AdvertiseOptions.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/AdvertiseOptions.cs index 544b2d3..0a475d0 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Bluetooth/AdvertiseOptions.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/AdvertiseOptions.cs @@ -1,6 +1,4 @@ -using ShortDev.Microsoft.ConnectedDevices.Transports; - -namespace ShortDev.Microsoft.ConnectedDevices.Platforms.Bluetooth; +namespace ShortDev.Microsoft.ConnectedDevices.Transports.Bluetooth; public sealed class AdvertiseOptions { diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/BLeBeacon.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/BLeBeacon.cs similarity index 97% rename from lib/ShortDev.Microsoft.ConnectedDevices/Transports/BLeBeacon.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/BLeBeacon.cs index e4f2779..63ffe90 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/BLeBeacon.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/BLeBeacon.cs @@ -2,7 +2,7 @@ using System.Net.NetworkInformation; using System.Text; -namespace ShortDev.Microsoft.ConnectedDevices.Transports; +namespace ShortDev.Microsoft.ConnectedDevices.Transports.Bluetooth; public sealed record BLeBeacon(DeviceType DeviceType, PhysicalAddress MacAddress, string DeviceName) { diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/BluetoothTransport.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/BluetoothTransport.cs similarity index 83% rename from lib/ShortDev.Microsoft.ConnectedDevices/Transports/BluetoothTransport.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/BluetoothTransport.cs index f685355..d36d1fd 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/BluetoothTransport.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/BluetoothTransport.cs @@ -1,11 +1,4 @@ -using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.TransportUpgrade; -using ShortDev.Microsoft.ConnectedDevices.Platforms; -using ShortDev.Microsoft.ConnectedDevices.Platforms.Bluetooth; -using System.Threading; -using System.Threading.Tasks; - -namespace ShortDev.Microsoft.ConnectedDevices.Transports; - +namespace ShortDev.Microsoft.ConnectedDevices.Transports.Bluetooth; public sealed class BluetoothTransport(IBluetoothHandler handler) : ICdpTransport, ICdpDiscoverableTransport { public CdpTransportType TransportType { get; } = CdpTransportType.Rfcomm; @@ -26,8 +19,8 @@ public void Listen(CancellationToken cancellationToken) ); } - public async Task ConnectAsync(CdpDevice device) - => await Handler.ConnectRfcommAsync(device, new RfcommOptions() + public async Task ConnectAsync(EndpointInfo endpoint) + => await Handler.ConnectRfcommAsync(endpoint, new RfcommOptions() { ServiceId = Constants.RfcommServiceId, ServiceName = Constants.RfcommServiceName, diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Bluetooth/IBluetoothHandler.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/IBluetoothHandler.cs similarity index 64% rename from lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Bluetooth/IBluetoothHandler.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/IBluetoothHandler.cs index a64400b..9b6a1c7 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Bluetooth/IBluetoothHandler.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/IBluetoothHandler.cs @@ -1,13 +1,11 @@ using System.Net.NetworkInformation; -using System.Threading; -using System.Threading.Tasks; -namespace ShortDev.Microsoft.ConnectedDevices.Platforms.Bluetooth; +namespace ShortDev.Microsoft.ConnectedDevices.Transports.Bluetooth; public interface IBluetoothHandler { Task ScanBLeAsync(ScanOptions scanOptions, CancellationToken cancellationToken = default); - Task ConnectRfcommAsync(CdpDevice device, RfcommOptions options, CancellationToken cancellationToken = default); + Task ConnectRfcommAsync(EndpointInfo device, RfcommOptions options, CancellationToken cancellationToken = default); Task AdvertiseBLeBeaconAsync(AdvertiseOptions options, CancellationToken cancellationToken = default); Task ListenRfcommAsync(RfcommOptions options, CancellationToken cancellationToken = default); diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Bluetooth/RfcommOptions.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/RfcommOptions.cs similarity index 75% rename from lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Bluetooth/RfcommOptions.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/RfcommOptions.cs index 798cb05..68b87d8 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Bluetooth/RfcommOptions.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/RfcommOptions.cs @@ -1,6 +1,6 @@ using System; -namespace ShortDev.Microsoft.ConnectedDevices.Platforms.Bluetooth; +namespace ShortDev.Microsoft.ConnectedDevices.Transports.Bluetooth; public sealed class RfcommOptions { diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Bluetooth/ScanOptions.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/ScanOptions.cs similarity index 61% rename from lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Bluetooth/ScanOptions.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/ScanOptions.cs index 59c8d33..b8fc537 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Bluetooth/ScanOptions.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Bluetooth/ScanOptions.cs @@ -1,6 +1,4 @@ -using ShortDev.Microsoft.ConnectedDevices.Transports; - -namespace ShortDev.Microsoft.ConnectedDevices.Platforms.Bluetooth; +namespace ShortDev.Microsoft.ConnectedDevices.Transports.Bluetooth; public sealed class ScanOptions { diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/CdpSocket.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/CdpSocket.cs similarity index 76% rename from lib/ShortDev.Microsoft.ConnectedDevices/Platforms/CdpSocket.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/Transports/CdpSocket.cs index 47b7f88..16c7897 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/CdpSocket.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/CdpSocket.cs @@ -1,13 +1,12 @@ -using ShortDev.Microsoft.ConnectedDevices.Transports; - -namespace ShortDev.Microsoft.ConnectedDevices.Platforms; +namespace ShortDev.Microsoft.ConnectedDevices.Transports; /// /// Provides direct low-level access to inter-device communication. /// public sealed class CdpSocket : IDisposable { - public required CdpTransportType TransportType { get; init; } + public CdpTransportType TransportType => Endpoint.TransportType; + public required EndpointInfo Endpoint { get; init; } public required Stream InputStream { get; init; } public required Stream OutputStream { get; init; } @@ -17,8 +16,6 @@ public void SendData(EndianWriter writer) writer.CopyTo(OutputStream); } - public required CdpDevice RemoteDevice { get; init; } - public bool IsClosed { get; private set; } public Action? Close { private get; set; } diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/DeviceConnectedEventHandler.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/DeviceConnectedEventHandler.cs index dbef5cb..1b88783 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/DeviceConnectedEventHandler.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/DeviceConnectedEventHandler.cs @@ -1,5 +1,3 @@ -using ShortDev.Microsoft.ConnectedDevices.Platforms; - -namespace ShortDev.Microsoft.ConnectedDevices.Transports; +namespace ShortDev.Microsoft.ConnectedDevices.Transports; public delegate void DeviceConnectedEventHandler(ICdpTransport sender, CdpSocket socket); diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/DeviceDiscoveredEventHandler.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/DeviceDiscoveredEventHandler.cs index 11e7061..1585cfd 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/DeviceDiscoveredEventHandler.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/DeviceDiscoveredEventHandler.cs @@ -1,5 +1,3 @@ -using ShortDev.Microsoft.ConnectedDevices.Platforms; - -namespace ShortDev.Microsoft.ConnectedDevices.Transports; +namespace ShortDev.Microsoft.ConnectedDevices.Transports; public delegate void DeviceDiscoveredEventHandler(ICdpTransport sender, CdpDevice device); diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/ICdpDiscoverableTransport.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/ICdpDiscoverableTransport.cs index 139efd1..5c09b5f 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/ICdpDiscoverableTransport.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/ICdpDiscoverableTransport.cs @@ -1,5 +1,4 @@ -using ShortDev.Microsoft.ConnectedDevices.Platforms; -using System.Threading; +using System.Threading; namespace ShortDev.Microsoft.ConnectedDevices.Transports; diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/ICdpTransport.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/ICdpTransport.cs index 4373269..97da20f 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/ICdpTransport.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/ICdpTransport.cs @@ -1,11 +1,4 @@ -using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.TransportUpgrade; -using ShortDev.Microsoft.ConnectedDevices.Platforms; -using System; -using System.Diagnostics.CodeAnalysis; -using System.Threading; -using System.Threading.Tasks; - -namespace ShortDev.Microsoft.ConnectedDevices.Transports; +namespace ShortDev.Microsoft.ConnectedDevices.Transports; public interface ICdpTransport : IDisposable { @@ -13,12 +6,12 @@ public interface ICdpTransport : IDisposable EndpointInfo GetEndpoint(); - Task ConnectAsync(CdpDevice device); - async Task TryConnectAsync(CdpDevice device, TimeSpan timeout) + Task ConnectAsync(EndpointInfo endpoint); + async Task TryConnectAsync(EndpointInfo endpoint, TimeSpan timeout) { try { - return await ConnectAsync(device).WithTimeout(timeout); + return await ConnectAsync(endpoint).WithTimeout(timeout); } catch { } return null; diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Network/INetworkHandler.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Network/INetworkHandler.cs similarity index 91% rename from lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Network/INetworkHandler.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/Transports/Network/INetworkHandler.cs index 33810d5..43c4793 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Platforms/Network/INetworkHandler.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Network/INetworkHandler.cs @@ -3,7 +3,7 @@ using System.Net; using System.Net.Sockets; -namespace ShortDev.Microsoft.ConnectedDevices.Platforms.Network; +namespace ShortDev.Microsoft.ConnectedDevices.Transports.Network; public interface INetworkHandler { diff --git a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/NetworkTransport.cs b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Network/NetworkTransport.cs similarity index 85% rename from lib/ShortDev.Microsoft.ConnectedDevices/Transports/NetworkTransport.cs rename to lib/ShortDev.Microsoft.ConnectedDevices/Transports/Network/NetworkTransport.cs index 42868a5..0a7bbb5 100644 --- a/lib/ShortDev.Microsoft.ConnectedDevices/Transports/NetworkTransport.cs +++ b/lib/ShortDev.Microsoft.ConnectedDevices/Transports/Network/NetworkTransport.cs @@ -1,12 +1,9 @@ using ShortDev.Microsoft.ConnectedDevices.Messages; -using ShortDev.Microsoft.ConnectedDevices.Messages.Connection.TransportUpgrade; using ShortDev.Microsoft.ConnectedDevices.Messages.Discovery; -using ShortDev.Microsoft.ConnectedDevices.Platforms; -using ShortDev.Microsoft.ConnectedDevices.Platforms.Network; using System.Net; using System.Net.Sockets; -namespace ShortDev.Microsoft.ConnectedDevices.Transports; +namespace ShortDev.Microsoft.ConnectedDevices.Transports.Network; public sealed class NetworkTransport(INetworkHandler handler) : ICdpTransport, ICdpDiscoverableTransport { @@ -30,18 +27,13 @@ public async void Listen(CancellationToken cancellationToken) var stream = client.GetStream(); DeviceConnected?.Invoke(this, new() { - TransportType = CdpTransportType.Tcp, Close = client.Close, InputStream = stream, OutputStream = stream, - RemoteDevice = new( - Name: "UNKNOWN", // ToDo: Find device name - DeviceType.Invalid, - new EndpointInfo( - TransportType, - ((IPEndPoint?)client.Client.RemoteEndPoint)?.Address.ToString() ?? throw new InvalidDataException("No ip address"), - Constants.TcpPort.ToString() - ) + Endpoint = new EndpointInfo( + TransportType, + ((IPEndPoint?)client.Client.RemoteEndPoint)?.Address.ToString() ?? throw new InvalidDataException("No ip address"), + Constants.TcpPort.ToString() ) }); } @@ -49,15 +41,14 @@ public async void Listen(CancellationToken cancellationToken) catch (OperationCanceledException) { } } - public async Task ConnectAsync(CdpDevice device) + public async Task ConnectAsync(EndpointInfo endpoint) { // ToDo: If the windows machine tries to connect back it uses the port assigned here not 5040!! TcpClient client = new(); - await client.ConnectAsync(device.Endpoint.ToIPEndPoint()); + await client.ConnectAsync(endpoint.ToIPEndPoint()); return new() { - TransportType = TransportType, - RemoteDevice = device, + Endpoint = endpoint, InputStream = client.GetStream(), OutputStream = client.GetStream(), Close = client.Close, diff --git a/src/AndroidBluetoothHandler.cs b/src/AndroidBluetoothHandler.cs index 4b47389..eca047c 100644 --- a/src/AndroidBluetoothHandler.cs +++ b/src/AndroidBluetoothHandler.cs @@ -1,12 +1,11 @@ using Android.Bluetooth; using Android.Bluetooth.LE; using Android.Runtime; -using ShortDev.Microsoft.ConnectedDevices.Platforms; using ShortDev.Microsoft.ConnectedDevices; -using ShortDev.Microsoft.ConnectedDevices.Platforms.Bluetooth; using BLeScanResult = Android.Bluetooth.LE.ScanResult; using ShortDev.Microsoft.ConnectedDevices.Transports; using System.Net.NetworkInformation; +using ShortDev.Microsoft.ConnectedDevices.Transports.Bluetooth; namespace NearShare.Droid; @@ -58,12 +57,12 @@ public override void OnBatchScanResults(IList? results) } } - public async Task ConnectRfcommAsync(CdpDevice device, RfcommOptions options, CancellationToken cancellationToken = default) + public async Task ConnectRfcommAsync(EndpointInfo endpoint, RfcommOptions options, CancellationToken cancellationToken = default) { if (Adapter == null) throw new InvalidOperationException($"{nameof(Adapter)} is not initialized!"); - var btDevice = Adapter.GetRemoteDevice(device.Endpoint.Address) ?? throw new ArgumentException($"Could not find bt device with address \"{device.Endpoint.Address}\""); + var btDevice = Adapter.GetRemoteDevice(endpoint.Address) ?? throw new ArgumentException($"Could not find bt device with address \"{endpoint.Address}\""); var btSocket = btDevice.CreateInsecureRfcommSocketToServiceRecord(Java.Util.UUID.FromString(options.ServiceId)) ?? throw new ArgumentException("Could not create service socket"); await btSocket.ConnectAsync(); return btSocket.ToCdp(); diff --git a/src/AndroidNetworkHandler.cs b/src/AndroidNetworkHandler.cs index c9136c6..bb916b5 100644 --- a/src/AndroidNetworkHandler.cs +++ b/src/AndroidNetworkHandler.cs @@ -1,7 +1,7 @@ using Android.Content; using Android.Net; using Android.Net.Wifi; -using ShortDev.Microsoft.ConnectedDevices.Platforms.Network; +using ShortDev.Microsoft.ConnectedDevices.Transports.Network; using System.Net; namespace NearShare.Droid; diff --git a/src/ReceiveActivity.cs b/src/ReceiveActivity.cs index cb7dea8..260781e 100644 --- a/src/ReceiveActivity.cs +++ b/src/ReceiveActivity.cs @@ -14,10 +14,9 @@ using ShortDev.Microsoft.ConnectedDevices; using ShortDev.Microsoft.ConnectedDevices.Encryption; using ShortDev.Microsoft.ConnectedDevices.NearShare; -using ShortDev.Microsoft.ConnectedDevices.Platforms; -using ShortDev.Microsoft.ConnectedDevices.Platforms.Bluetooth; -using ShortDev.Microsoft.ConnectedDevices.Platforms.Network; using ShortDev.Microsoft.ConnectedDevices.Transports; +using ShortDev.Microsoft.ConnectedDevices.Transports.Bluetooth; +using ShortDev.Microsoft.ConnectedDevices.Transports.Network; using System.Collections.ObjectModel; using System.Diagnostics; using System.Net.NetworkInformation; @@ -238,24 +237,19 @@ void OnTransfer(TransferToken transfer) static class Extensions { - public static CdpDevice ToCdp(this BluetoothDevice @this) + public static EndpointInfo ToCdp(this BluetoothDevice @this) => new( - @this.Name ?? throw new InvalidDataException("Empty name"), - DeviceType.Invalid, - new( - CdpTransportType.Rfcomm, - @this.Address ?? throw new InvalidDataException("Empty address"), - Constants.RfcommServiceId - ) + CdpTransportType.Rfcomm, + @this.Address ?? throw new InvalidDataException("Empty address"), + Constants.RfcommServiceId ); public static CdpSocket ToCdp(this BluetoothSocket @this) => new() { - TransportType = CdpTransportType.Rfcomm, InputStream = @this.InputStream ?? throw new NullReferenceException(), OutputStream = @this.OutputStream ?? throw new NullReferenceException(), - RemoteDevice = @this.RemoteDevice!.ToCdp(), + Endpoint = @this.RemoteDevice!.ToCdp(), Close = @this.Close }; } \ No newline at end of file diff --git a/src/SendActivity.cs b/src/SendActivity.cs index 118f0e2..471cae9 100644 --- a/src/SendActivity.cs +++ b/src/SendActivity.cs @@ -16,8 +16,9 @@ using ShortDev.Microsoft.ConnectedDevices; using ShortDev.Microsoft.ConnectedDevices.Encryption; using ShortDev.Microsoft.ConnectedDevices.NearShare; -using ShortDev.Microsoft.ConnectedDevices.Platforms; using ShortDev.Microsoft.ConnectedDevices.Transports; +using ShortDev.Microsoft.ConnectedDevices.Transports.Bluetooth; +using ShortDev.Microsoft.ConnectedDevices.Transports.Network; using System.Collections.ObjectModel; using System.Net.NetworkInformation;