| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.CRDT; |
| | 5 | | using DCL.Models; |
| | 6 | | using Decentraland.Renderer.RendererServices; |
| | 7 | | using Google.Protobuf; |
| | 8 | | using rpc_csharp; |
| | 9 | | using System; |
| | 10 | | using System.IO; |
| | 11 | | using System.Threading; |
| | 12 | | using UnityEngine; |
| | 13 | | using BinaryWriter = KernelCommunication.BinaryWriter; |
| | 14 | |
|
| | 15 | | namespace RPC.Services |
| | 16 | | { |
| | 17 | | public class CRDTServiceImpl : ICRDTService<RPCContext> |
| | 18 | | { |
| 1 | 19 | | private static readonly CRDTResponse defaultResponse = new CRDTResponse(); |
| 1 | 20 | | private static readonly UniTask<CRDTManyMessages> emptyResponse = UniTask.FromResult(new CRDTManyMessages() { Sc |
| | 21 | |
|
| 1 | 22 | | private static readonly CRDTManyMessages reusableCrdtMessage = new CRDTManyMessages(); |
| | 23 | |
|
| 1 | 24 | | private static readonly MemoryStream memoryStream = new MemoryStream(); |
| 1 | 25 | | private static readonly BinaryWriter binaryWriter = new BinaryWriter(memoryStream); |
| | 26 | |
|
| | 27 | | public static void RegisterService(RpcServerPort<RPCContext> port) |
| | 28 | | { |
| 0 | 29 | | CRDTServiceCodeGen.RegisterService(port, new CRDTServiceImpl()); |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | public async UniTask<CRDTResponse> SendCrdt(CRDTManyMessages messages, RPCContext context, CancellationToken ct) |
| | 33 | | { |
| | 34 | | // This line is to avoid a race condition because a CRDT message could be sent before the scene was loaded |
| | 35 | | // more info: https://github.com/decentraland/sdk/issues/480#issuecomment-1331309908 |
| 12 | 36 | | await UniTask.WaitUntil(() => context.crdt.MessagingControllersManager.ContainsController(messages.SceneNumb |
| | 37 | | cancellationToken: ct); |
| | 38 | |
|
| 12 | 39 | | await UniTask.WaitWhile(() => context.crdt.MessagingControllersManager.HasScenePendingMessages(messages.Scen |
| | 40 | | cancellationToken: ct); |
| | 41 | |
|
| 3 | 42 | | await UniTask.SwitchToMainThread(ct); |
| | 43 | |
|
| | 44 | | try |
| | 45 | | { |
| 3 | 46 | | using (var iterator = CRDTDeserializer.DeserializeBatch(messages.Payload.Memory)) |
| | 47 | | { |
| 6 | 48 | | while (iterator.MoveNext()) |
| | 49 | | { |
| 3 | 50 | | if (!(iterator.Current is CrdtMessage crdtMessage)) |
| | 51 | | continue; |
| | 52 | |
|
| 3 | 53 | | context.crdt.CrdtMessageReceived?.Invoke(messages.SceneNumber, crdtMessage); |
| | 54 | | } |
| 3 | 55 | | } |
| | 56 | |
|
| 3 | 57 | | if (context.crdt.WorldState.TryGetScene(messages.SceneNumber, out IParcelScene scene)) |
| | 58 | | { |
| | 59 | | // When sdk7 scene receive it first crdt we set `InitMessagesDone` since |
| | 60 | | // kernel won't be sending that message for those scenes |
| 2 | 61 | | if (scene.sceneData.sdk7 && !scene.IsInitMessageDone()) |
| | 62 | | { |
| 1 | 63 | | context.crdt.SceneController.EnqueueSceneMessage(new QueuedSceneMessage_Scene() |
| | 64 | | { |
| | 65 | | sceneNumber = messages.SceneNumber, |
| | 66 | | tag = "scene", |
| | 67 | | payload = new Protocol.SceneReady(), |
| | 68 | | method = MessagingTypes.INIT_DONE, |
| | 69 | | type = QueuedSceneMessage.Type.SCENE_MESSAGE |
| | 70 | | }); |
| | 71 | | } |
| | 72 | | } |
| 3 | 73 | | } |
| | 74 | | catch (Exception e) |
| | 75 | | { |
| 0 | 76 | | Debug.LogError(e); |
| 0 | 77 | | } |
| | 78 | |
|
| 3 | 79 | | return defaultResponse; |
| 3 | 80 | | } |
| | 81 | |
|
| | 82 | | public UniTask<CRDTManyMessages> PullCrdt(PullCRDTRequest request, RPCContext context, CancellationToken ct) |
| | 83 | | { |
| | 84 | | try |
| | 85 | | { |
| 2 | 86 | | if (!context.crdt.scenesOutgoingCrdts.TryGetValue(request.SceneNumber, out DualKeyValueSet<int, long, Cr |
| | 87 | | { |
| 0 | 88 | | return emptyResponse; |
| | 89 | | } |
| | 90 | |
|
| 2 | 91 | | memoryStream.SetLength(0); |
| | 92 | |
|
| 2 | 93 | | context.crdt.scenesOutgoingCrdts.Remove(request.SceneNumber); |
| | 94 | |
|
| 8 | 95 | | foreach (var msg in sceneCrdtOutgoingMessage) |
| | 96 | | { |
| 2 | 97 | | CRDTSerializer.Serialize(binaryWriter, msg.value); |
| | 98 | | } |
| 2 | 99 | | sceneCrdtOutgoingMessage.Clear(); |
| | 100 | |
|
| 2 | 101 | | reusableCrdtMessage.SceneId = request.SceneId; |
| 2 | 102 | | reusableCrdtMessage.SceneNumber = request.SceneNumber; |
| 2 | 103 | | reusableCrdtMessage.Payload = ByteString.CopyFrom(memoryStream.ToArray()); |
| | 104 | |
|
| 2 | 105 | | return UniTask.FromResult(reusableCrdtMessage); |
| | 106 | | } |
| | 107 | | catch (Exception e) |
| | 108 | | { |
| 0 | 109 | | Debug.LogError(e); |
| 0 | 110 | | return emptyResponse; |
| | 111 | | } |
| 2 | 112 | | } |
| | 113 | | } |
| | 114 | | } |