| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using Decentraland.Social.Friendships; |
| | 3 | | using rpc_csharp; |
| | 4 | | using rpc_csharp.transport; |
| | 5 | | using RPC.Transports; |
| | 6 | | using System; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCL.Social.Friends |
| | 11 | | { |
| | 12 | | public class RPCSocialClientProvider : ISocialClientProvider |
| | 13 | | { |
| | 14 | | private const string RPC_URL = "wss://rpc-social-service.decentraland"; |
| | 15 | | private const string MAIN_NET = "mainnet"; |
| | 16 | |
|
| | 17 | | private readonly KernelConfig kernelConfig; |
| | 18 | |
|
| 423 | 19 | | private string url = $"{RPC_URL}.org"; |
| 423 | 20 | | private string network = MAIN_NET; |
| | 21 | |
|
| | 22 | | public event Action OnTransportError; |
| | 23 | |
|
| 423 | 24 | | public RPCSocialClientProvider(KernelConfig kernelConfig) |
| | 25 | | { |
| 423 | 26 | | this.kernelConfig = kernelConfig; |
| 423 | 27 | | } |
| | 28 | |
|
| | 29 | | public async UniTask<IClientFriendshipsService> Provide(CancellationToken cancellationToken) |
| | 30 | | { |
| 0 | 31 | | await kernelConfig.EnsureConfigInitialized(); |
| 0 | 32 | | string currentNetwork = kernelConfig.Get().network; |
| 0 | 33 | | if (!currentNetwork.Equals(network, StringComparison.OrdinalIgnoreCase)) |
| | 34 | | { |
| 0 | 35 | | string tld = currentNetwork.Equals(MAIN_NET, StringComparison.OrdinalIgnoreCase) ? ".org" : ".zone"; |
| 0 | 36 | | url = $"{RPC_URL}{tld}"; |
| 0 | 37 | | network = currentNetwork; |
| | 38 | | } |
| | 39 | |
|
| 0 | 40 | | var transport = await CreateWebSocketAndConnect(cancellationToken); |
| 0 | 41 | | var client = new RpcClient(transport); |
| 0 | 42 | | var socialPort = await client.CreatePort("social-service-port"); |
| | 43 | |
|
| 0 | 44 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 45 | |
|
| 0 | 46 | | var module = await socialPort.LoadModule(FriendshipsServiceCodeGen.ServiceName); |
| | 47 | |
|
| 0 | 48 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 49 | |
|
| 0 | 50 | | return new ClientFriendshipsService(module); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | private UniTask<ITransport> CreateWebSocketAndConnect(CancellationToken cancellationToken) |
| | 54 | | { |
| 0 | 55 | | UniTaskCompletionSource<ITransport> task = new (); |
| 0 | 56 | | WebSocketClientTransport transport = new (url); |
| | 57 | |
|
| | 58 | | void CompleteTaskAndUnsubscribe() |
| | 59 | | { |
| 0 | 60 | | Debug.Log("SocialClient.Transport.Connected"); |
| | 61 | |
|
| 0 | 62 | | if (cancellationToken.IsCancellationRequested) |
| | 63 | | { |
| 0 | 64 | | task.TrySetCanceled(cancellationToken); |
| 0 | 65 | | return; |
| | 66 | | } |
| | 67 | |
|
| 0 | 68 | | task.TrySetResult(transport); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | void FailTaskAndUnsubscribe(string error) |
| | 72 | | { |
| 0 | 73 | | Debug.LogError($"SocialClient.Transport.Error: {error}"); |
| | 74 | |
|
| 0 | 75 | | if (cancellationToken.IsCancellationRequested) |
| | 76 | | { |
| 0 | 77 | | task.TrySetCanceled(cancellationToken); |
| 0 | 78 | | return; |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | task.TrySetException(new Exception(error)); |
| | 82 | |
|
| 0 | 83 | | OnTransportError?.Invoke(); |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | void FailTaskByDisconnectionAndUnsubscribe() |
| | 87 | | { |
| 0 | 88 | | Debug.Log("SocialClient.Transport.Disconnected"); |
| | 89 | |
|
| 0 | 90 | | if (cancellationToken.IsCancellationRequested) |
| | 91 | | { |
| 0 | 92 | | task.TrySetCanceled(cancellationToken); |
| 0 | 93 | | return; |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | task.TrySetException(new Exception("Cannot connect to social service server, connection closed")); |
| 0 | 97 | | } |
| | 98 | |
|
| 0 | 99 | | transport.OnConnectEvent += CompleteTaskAndUnsubscribe; |
| 0 | 100 | | transport.OnErrorEvent += FailTaskAndUnsubscribe; |
| 0 | 101 | | transport.OnCloseEvent += FailTaskByDisconnectionAndUnsubscribe; |
| | 102 | |
|
| 0 | 103 | | try { transport.Connect(); } |
| 0 | 104 | | catch (Exception e) { Debug.LogException(e); } |
| | 105 | |
|
| 0 | 106 | | return task.Task.AttachExternalCancellation(cancellationToken); |
| | 107 | | } |
| | 108 | | } |
| | 109 | | } |