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

Refactor session message handling #148

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void HandleStartTransfer(CdpMessage msg, ValueSet payload)
}
_fileTransferToken = new()
{
DeviceName = Channel.Session.DeviceName,
DeviceName = Channel.Session.DeviceInfo?.Name ?? "UNKNOWM",
TotalBytes = bytesToSend,
Files = files
};
Expand All @@ -88,7 +88,7 @@ void HandleStartTransfer(CdpMessage msg, ValueSet payload)

NearShareReceiver.OnReceivedUri(new()
{
DeviceName = Channel.Session.DeviceName,
DeviceName = Channel.Session.DeviceInfo?.Name ?? "UNKNOWM",
Uri = uri
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async Task<SenderStateMachine> PrepareTransferInternalAsync(EndpointInfo endpoin
// var cv = handshakeResultMsg.Header.TryGetCorrelationVector() ?? throw new InvalidDataException("No Correlation Vector");

SenderStateMachine senderStateMachine = new(Platform);
var channel = await session.StartClientChannelAsync(operationId.ToString("D").ToUpper(), NearShareApp.Name, senderStateMachine, handShakeChannel.Socket, cancellationToken);
var channel = await session.StartClientChannelAsync(operationId.ToString("D").ToUpper(), NearShareApp.Name, senderStateMachine, cancellationToken);
return senderStateMachine;
}

Expand Down
20 changes: 12 additions & 8 deletions lib/ShortDev.Microsoft.ConnectedDevices/CdpChannel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ShortDev.Microsoft.ConnectedDevices.Messages;
using ShortDev.Microsoft.ConnectedDevices.Messages.Control;
using ShortDev.Microsoft.ConnectedDevices.Messages.Session;
using ShortDev.Microsoft.ConnectedDevices.Session.Channels;
using ShortDev.Microsoft.ConnectedDevices.Transports;

namespace ShortDev.Microsoft.ConnectedDevices;
Expand All @@ -10,9 +11,12 @@
/// </summary>
public sealed class CdpChannel : IDisposable
{
private CdpChannel(CdpSession session, ulong channelId, CdpSocket socket, CdpAppBase app)
readonly ChannelHandler _handler;
private CdpChannel(ChannelHandler handler, ulong channelId, CdpSocket socket, CdpAppBase app)
{
Session = session;
_handler = handler;

Session = handler.Session;
ChannelId = channelId;
Socket = socket;
App = app;
Expand Down Expand Up @@ -66,29 +70,29 @@
Session.SendMessage(Socket, header, writer);
}

void IDisposable.Dispose()

Check warning on line 73 in lib/ShortDev.Microsoft.ConnectedDevices/CdpChannel.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Ensure that 'CdpChannel.System.IDisposable.Dispose' is declared as public and sealed (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1063)
=> Dispose();

public void Dispose(bool closeSession = false, bool closeSocket = false)
{
Session.UnregisterChannel(this);
_handler.UnregisterChannel(this);
if (closeSocket)
Socket.Dispose(); // ToDo: Heartbeat!
if (closeSession)
Session.Dispose(); // ToDo: Heartbeat!
}

internal static CdpChannel CreateServerChannel(CdpSession session, ulong channelId, CdpSocket socket, StartChannelRequest request)
internal static CdpChannel CreateServerChannel(ChannelHandler handler, CdpSocket socket, StartChannelRequest request, ulong channelId)
{
var app = CdpAppRegistration.InstantiateApp(request.Id, request.Name, session.Platform);
CdpChannel channel = new(session, channelId, socket, app);
var app = CdpAppRegistration.InstantiateApp(request.Id, request.Name, handler.Session.Platform);
CdpChannel channel = new(handler, channelId, socket, app);
app.Initialize(channel);
return channel;
}

internal static CdpChannel CreateClientChannel(CdpSession session, CdpSocket socket, StartChannelResponse response, CdpAppBase app)
internal static CdpChannel CreateClientChannel(ChannelHandler handler, CdpSocket socket, StartChannelResponse response, CdpAppBase app)
{
CdpChannel channel = new(session, response.ChannelId, socket, app);
CdpChannel channel = new(handler, response.ChannelId, socket, app);
app.Initialize(channel);
return channel;
}
Expand Down
22 changes: 19 additions & 3 deletions lib/ShortDev.Microsoft.ConnectedDevices/CdpLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,48 @@
using ShortDev.Microsoft.ConnectedDevices.Messages.Connection;
using ShortDev.Microsoft.ConnectedDevices.Messages.Control;
using ShortDev.Microsoft.ConnectedDevices.Transports;
using System;
using System.Collections.Generic;

namespace ShortDev.Microsoft.ConnectedDevices;

internal static partial class CdpLog
{
#region Advertising
[LoggerMessage(EventId = 101, Level = LogLevel.Information, Message = "Advertising started")]
public static partial void AdvertisingStarted(this ILogger logger);

[LoggerMessage(EventId = 102, Level = LogLevel.Information, Message = "Advertising stopped")]
public static partial void AdvertisingStopped(this ILogger logger);

[LoggerMessage(EventId = 106, Level = LogLevel.Information, Message = "Error while advertising")]
public static partial void AdvertisingError(this ILogger logger, Exception ex);
#endregion

#region Listening
[LoggerMessage(EventId = 103, Level = LogLevel.Information, Message = "Listening started")]
public static partial void ListeningStarted(this ILogger logger);

[LoggerMessage(EventId = 104, Level = LogLevel.Information, Message = "Listening stopped")]
public static partial void ListeningStopped(this ILogger logger);

[LoggerMessage(EventId = 107, Level = LogLevel.Information, Message = "Error while listening")]
public static partial void ListeningError(this ILogger logger, Exception ex);
#endregion

#region Discovery
[LoggerMessage(EventId = 108, Level = LogLevel.Information, Message = "Discovery started on {TransportTypes}")]
public static partial void DiscoveryStarted(this ILogger logger, IEnumerable<CdpTransportType> transportTypes);

[LoggerMessage(EventId = 109, Level = LogLevel.Information, Message = "Discovery stopped")]
public static partial void DiscoveryStopped(this ILogger logger);

[LoggerMessage(EventId = 110, Level = LogLevel.Information, Message = "Error during discovery")]
public static partial void DiscoveryError(this ILogger logger, Exception ex);
#endregion

[LoggerMessage(EventId = 105, Level = LogLevel.Information, Message = "New socket from endpoint {Endpoint}")]
public static partial void NewSocket(this ILogger logger, EndpointInfo endpoint);



[LoggerMessage(EventId = 201, Level = LogLevel.Error, Message = "Exception in session {SessionId:X}")]
public static partial void ExceptionInSession(this ILogger logger, Exception ex, ulong sessionId);

Expand Down
Loading
Loading