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

Chore: Streamline the architecture of Connective Rooms #647

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,84 +1,47 @@
using Cysharp.Threading.Tasks;
using Cysharp.Threading.Tasks;
using DCL.Character;
using DCL.Diagnostics;
using DCL.Multiplayer.Connections.Archipelago.AdapterAddress.Current;
using DCL.Multiplayer.Connections.Archipelago.LiveConnections;
using DCL.Multiplayer.Connections.Archipelago.SignFlow;
using DCL.Multiplayer.Connections.Rooms;
using DCL.Multiplayer.Connections.Rooms.Connective;
using DCL.Multiplayer.Connections.Typing;
using DCL.Utilities.Extensions;
using DCL.Web3.Identities;
using LiveKit.Internal.FFIClients.Pools;
using LiveKit.Internal.FFIClients.Pools.Memory;
using LiveKit.Rooms;
using System;
using System.Buffers;
using System.Net.WebSockets;
using System.Threading;
using UnityEngine;
using Utility.Multithreading;

namespace DCL.Multiplayer.Connections.Archipelago.Rooms
{
public class ArchipelagoIslandRoom : IArchipelagoIslandRoom
internal class ArchipelagoIslandRoomStrategy : IRealmRoomStrategy
{
private readonly IWeb3IdentityCache web3IdentityCache;
private readonly IArchipelagoSignFlow signFlow;
private readonly ICharacterObject characterObject;
private readonly IConnectiveRoom connectiveRoom;
private readonly ICurrentAdapterAddress currentAdapterAddress;
private readonly string currentAdapterAddress;

private ConnectToRoomAsyncDelegate? connectToRoomAsyncDelegate;
public ConnectiveRoom ConnectiveRoom { get; }

public ArchipelagoIslandRoom(ICharacterObject characterObject, IWeb3IdentityCache web3IdentityCache, IMultiPool multiPool, ICurrentAdapterAddress currentAdapterAddress) : this(
web3IdentityCache,
new LiveConnectionArchipelagoSignFlow(
new WebSocketArchipelagoLiveConnection(
() => new ClientWebSocket(),
new ArrayMemoryPool(ArrayPool<byte>.Shared!)
).WithLog(),
new ArrayMemoryPool(ArrayPool<byte>.Shared!),
multiPool
).WithLog(),
characterObject,
currentAdapterAddress
) { }
private ConnectToRoomAsyncDelegate? connectToRoomAsyncDelegate;

public ArchipelagoIslandRoom(
public ArchipelagoIslandRoomStrategy(
InteriorRoom sharedRoom,
IWeb3IdentityCache web3IdentityCache,
IArchipelagoSignFlow signFlow,
ICharacterObject characterObject,
ICurrentAdapterAddress currentAdapterAddress
string currentAdapterAddress
)
{
this.web3IdentityCache = web3IdentityCache;
this.signFlow = signFlow;
this.characterObject = characterObject;
this.currentAdapterAddress = currentAdapterAddress;

connectiveRoom = new RenewableConnectiveRoom(
() => new ConnectiveRoom(
PrewarmAsync,
SendHeartbeatAsync
)
);
ConnectiveRoom = new ConnectiveRoom(sharedRoom, PrewarmAsync, SendHeartbeatAsync);
}

public void Start() =>
connectiveRoom.Start();

public UniTask StopAsync() =>
UniTask.WhenAll(
//signFlow.DisconnectAsync(CancellationToken.None),
connectiveRoom.StopAsync()
);

public IConnectiveRoom.State CurrentState() =>
connectiveRoom.CurrentState();

public IRoom Room() =>
connectiveRoom.Room();

private async UniTask PrewarmAsync(CancellationToken token)
{
await ConnectToArchipelagoAsync(token);
Expand All @@ -102,15 +65,14 @@ private async UniTask SendHeartbeatAsync(ConnectToRoomAsyncDelegate connectDeleg

private void OnNewConnectionString(string connectionString, CancellationToken token)
{
if (CurrentState() is IConnectiveRoom.State.Stopped) throw new InvalidOperationException("Room is not running");
if (ConnectiveRoom.CurrentState() is IConnectiveRoom.State.Stopped) throw new InvalidOperationException("Room is not running");
connectToRoomAsyncDelegate.EnsureNotNull("Connection delegate is not passed yet");
connectToRoomAsyncDelegate!(connectionString, token).Forget();
}

private async UniTask ConnectToArchipelagoAsync(CancellationToken token)
{
string adapterUrl = await currentAdapterAddress.AdapterUrlAsync(token);
LightResult<string> welcomePeerId = await WelcomePeerIdAsync(adapterUrl, token);
LightResult<string> welcomePeerId = await WelcomePeerIdAsync(currentAdapterAddress, token);
welcomePeerId.EnsureSuccess("Cannot authorize with current address and signature, peer id is invalid");
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,47 +1,39 @@
using Cysharp.Threading.Tasks;
using Cysharp.Threading.Tasks;
using DCL.Diagnostics;
using DCL.Multiplayer.Connections.Archipelago.AdapterAddress.Current;
using DCL.Multiplayer.Connections.Rooms;
using DCL.Multiplayer.Connections.Rooms.Connective;
using DCL.WebRequests;
using LiveKit.Rooms;
using System;
using System.Threading;
using UnityEngine;

namespace DCL.Multiplayer.Connections.Archipelago.Rooms.Fixed
{
public class FixedConnectiveRoom : IConnectiveRoom
/// <summary>
/// No need for any abstractions - it's a unique behaviour that we can't replace
/// </summary>
internal class FixedConnectionRoomStrategy : IRealmRoomStrategy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define please the "abstractions", something you are naming it and claiming it's bad to have many of it and something not

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super simple interface:

  • One level - no nesting
  • One member IConnectiveRoom ConnectiveRoom { get; }, two implementations
  • Clear purpose: to allow select polymorphic behavior between Fixed and Archipelago realms

{
private readonly IWebRequestController webRequests;
private readonly IConnectiveRoom connectiveRoom;
private readonly ICurrentAdapterAddress currentAdapterAddress;
private readonly string currentAdapterAddress;

public FixedConnectiveRoom(IWebRequestController webRequests, ICurrentAdapterAddress currentAdapterAddress)
public ConnectiveRoom ConnectiveRoom { get; }

public FixedConnectionRoomStrategy(InteriorRoom sharedRoom, IWebRequestController webRequests, string currentAdapterAddress)
{
this.webRequests = webRequests;
this.currentAdapterAddress = currentAdapterAddress;

connectiveRoom = new ConnectiveRoom(
ConnectiveRoom = new ConnectiveRoom(
sharedRoom,
static _ => UniTask.CompletedTask,
RunConnectCycleStepAsync
);
}

public void Start() =>
connectiveRoom.Start();

public UniTask StopAsync() =>
connectiveRoom.StopAsync();

public IConnectiveRoom.State CurrentState() =>
connectiveRoom.CurrentState();

public IRoom Room() =>
connectiveRoom.Room();

private async UniTask RunConnectCycleStepAsync(ConnectToRoomAsyncDelegate connectToRoomAsyncDelegate, CancellationToken token)
{
if (connectiveRoom.CurrentState() is not IConnectiveRoom.State.Running)
if (ConnectiveRoom.CurrentState() is not IConnectiveRoom.State.Running)
{
string connectionString = await ConnectionStringAsync(token);
await connectToRoomAsyncDelegate(connectionString, token);
Expand All @@ -50,9 +42,8 @@ private async UniTask RunConnectCycleStepAsync(ConnectToRoomAsyncDelegate connec

private async UniTask<string> ConnectionStringAsync(CancellationToken token)
{
string adapterUrl = await currentAdapterAddress.AdapterUrlAsync(token);
string metadata = FixedMetadata.Default.ToJson();
GenericPostRequest result = await webRequests.SignedFetchPostAsync(adapterUrl, metadata, token);
GenericPostRequest result = await webRequests.SignedFetchPostAsync(currentAdapterAddress, metadata, token);
AdapterResponse response = await result.CreateFromJson<AdapterResponse>(WRJsonParser.Unity);
string connectionString = response.fixedAdapter;
ReportHub.WithReport(ReportCategory.ARCHIPELAGO_REQUEST).Log($"String is: {connectionString}");
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using DCL.Multiplayer.Connections.Rooms.Connective;

namespace DCL.Multiplayer.Connections.Archipelago.Rooms
{
internal interface IRealmRoomStrategy
{
ConnectiveRoom ConnectiveRoom { get; }
mikhail-dcl marked this conversation as resolved.
Show resolved Hide resolved
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DCL.Multiplayer.Connections.Archipelago.Rooms
{
public interface IRealmRoomsProvider : IRoomProvider
{
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading