-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c29187b
commit 3390897
Showing
5 changed files
with
170 additions
and
127 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
lib/ShortDev.Microsoft.ConnectedDevices/ConnectedDevicesPlatform.MessageLoop.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using ShortDev.Microsoft.ConnectedDevices.Messages; | ||
using ShortDev.Microsoft.ConnectedDevices.Transports; | ||
using System.Buffers; | ||
|
||
namespace ShortDev.Microsoft.ConnectedDevices; | ||
|
||
partial class ConnectedDevicesPlatform | ||
{ | ||
static readonly ArrayPool<byte> _messagePool = ArrayPool<byte>.Create(); | ||
private void ReceiveLoop(CdpSocket socket) | ||
{ | ||
RegisterKnownSocket(socket); | ||
Task.Run(() => | ||
{ | ||
EndianReader streamReader = new(Endianness.BigEndian, socket.InputStream); | ||
using (socket) | ||
{ | ||
ReceiveLoop(socket, ref streamReader); | ||
} | ||
}); | ||
} | ||
|
||
void ReceiveLoop(CdpSocket socket, ref EndianReader streamReader) | ||
{ | ||
do | ||
{ | ||
CdpSession? session = null; | ||
try | ||
{ | ||
var header = CommonHeader.Parse(ref streamReader); | ||
|
||
if (socket.IsClosed) | ||
return; | ||
|
||
session = CdpSession.GetOrCreate( | ||
this, | ||
socket.Endpoint, | ||
header | ||
); | ||
|
||
using var payload = _messagePool.RentToken(header.PayloadSize); | ||
streamReader.ReadBytes(payload.Span); | ||
|
||
if (socket.IsClosed) | ||
return; | ||
|
||
EndianReader reader = new(Endianness.BigEndian, payload.Span); | ||
session.HandleMessage(socket, header, ref reader); | ||
} | ||
catch (IOException) | ||
{ | ||
break; | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (socket.IsClosed) | ||
return; | ||
|
||
if (session != null) | ||
_logger.ExceptionInSession(ex, session.SessionId.AsNumber()); | ||
else | ||
_logger.ExceptionInReceiveLoop(ex, socket.TransportType); | ||
|
||
break; | ||
} | ||
} while (!socket.IsClosed); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
lib/ShortDev.Microsoft.ConnectedDevices/ConnectedDevicesPlatform.Sockets.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using ShortDev.Microsoft.ConnectedDevices.Transports; | ||
using System.Collections.Concurrent; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace ShortDev.Microsoft.ConnectedDevices; | ||
|
||
partial class ConnectedDevicesPlatform | ||
{ | ||
readonly ConcurrentDictionary<EndpointInfo, CdpSocket> _knownSockets = new(); | ||
|
||
void RegisterKnownSocket(CdpSocket socket) | ||
{ | ||
Debug.Assert(!socket.IsClosed); | ||
|
||
socket.Disposed += OnSocketClosed; | ||
void OnSocketClosed() | ||
{ | ||
socket.Disposed -= OnSocketClosed; | ||
|
||
var couldRemove = _knownSockets.TryRemove(KeyValuePair.Create(socket.Endpoint, socket)); | ||
Debug.Assert(couldRemove); | ||
} | ||
|
||
_knownSockets.AddOrUpdate( | ||
socket.Endpoint, | ||
static (key, newSocket) => newSocket, | ||
static (key, newSocket, currentSocket) => newSocket, | ||
socket | ||
); | ||
} | ||
|
||
bool TryGetKnownSocket(EndpointInfo endpoint, [MaybeNullWhen(false)] out CdpSocket socket) | ||
{ | ||
if (!_knownSockets.TryGetValue(endpoint, out socket)) | ||
return false; | ||
|
||
// ToDo: Alive check!! | ||
if (socket.IsClosed) | ||
return false; | ||
|
||
return true; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
lib/ShortDev.Microsoft.ConnectedDevices/ConnectedDevicesPlatform.Transport.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using ShortDev.Microsoft.ConnectedDevices.Transports; | ||
using System.Collections.Concurrent; | ||
|
||
namespace ShortDev.Microsoft.ConnectedDevices; | ||
|
||
partial class ConnectedDevicesPlatform | ||
{ | ||
readonly ConcurrentDictionary<CdpTransportType, ICdpTransport> _transportMap = new(); | ||
public void AddTransport<T>(T transport) where T : ICdpTransport | ||
{ | ||
_transportMap.AddOrUpdate( | ||
transport.TransportType, | ||
static (key, newTansport) => newTansport, | ||
(key, newTansport, oldTransport) => | ||
{ | ||
oldTransport.Dispose(); | ||
return newTansport; | ||
}, | ||
transport | ||
); | ||
} | ||
|
||
[Obsolete("Use overload instead")] | ||
public T? TryGetTransport<T>() where T : ICdpTransport | ||
=> (T?)_transportMap.Values.SingleOrDefault(x => x is T); | ||
|
||
public ICdpTransport? TryGetTransport(CdpTransportType transportType) | ||
=> _transportMap.GetValueOrDefault(transportType); | ||
} |
27 changes: 27 additions & 0 deletions
27
lib/ShortDev.Microsoft.ConnectedDevices/ConnectedDevicesPlatform.Utils.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Microsoft.Extensions.Logging; | ||
using ShortDev.Microsoft.ConnectedDevices.Encryption; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Security.Cryptography; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace ShortDev.Microsoft.ConnectedDevices; | ||
|
||
partial class ConnectedDevicesPlatform | ||
{ | ||
public static X509Certificate2 CreateDeviceCertificate([NotNull] CdpEncryptionParams encryptionParams) | ||
{ | ||
using var key = ECDsa.Create(encryptionParams.Curve); | ||
CertificateRequest certRequest = new("CN=Ms-Cdp", key, HashAlgorithmName.SHA256); | ||
return certRequest.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now.AddYears(5)); | ||
} | ||
|
||
public static ILoggerFactory CreateLoggerFactory(string filePattern, LogLevel logLevel = LogLevel.Debug) | ||
=> LoggerFactory.Create(builder => | ||
{ | ||
builder.ClearProviders(); | ||
|
||
builder.SetMinimumLevel(logLevel); | ||
|
||
builder.AddFile(filePattern, logLevel); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters