Skip to content

Commit

Permalink
Extract session handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ShortDevelopment committed Apr 3, 2024
1 parent 4c74252 commit 8ab3360
Show file tree
Hide file tree
Showing 16 changed files with 1,012 additions and 901 deletions.
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 @@ namespace ShortDev.Microsoft.ConnectedDevices;
/// </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 @@ -71,24 +75,24 @@ void IDisposable.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
Loading

0 comments on commit 8ab3360

Please sign in to comment.