| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using System.Threading; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | | using DCL.CRDT; |
| | 6 | | using Google.Protobuf; |
| | 7 | | using KernelCommunication; |
| | 8 | | using rpc_csharp; |
| | 9 | | using UnityEngine; |
| | 10 | | using BinaryWriter = KernelCommunication.BinaryWriter; |
| | 11 | |
|
| | 12 | | namespace RPC.Services |
| | 13 | | { |
| | 14 | | public class CRDTServiceImpl : ICRDTService<RPCContext> |
| | 15 | | { |
| 1 | 16 | | private static readonly CRDTResponse defaultResponse = new CRDTResponse(); |
| 1 | 17 | | private static readonly UniTask<CRDTManyMessages> emptyResponse = UniTask.FromResult(new CRDTManyMessages() { Sc |
| | 18 | |
|
| 1 | 19 | | private static readonly CRDTManyMessages reusableCrdtMessage = new CRDTManyMessages(); |
| | 20 | |
|
| 1 | 21 | | private static readonly MemoryStream memoryStream = new MemoryStream(); |
| 1 | 22 | | private static readonly BinaryWriter binaryWriter = new BinaryWriter(memoryStream); |
| | 23 | |
|
| | 24 | | public static void RegisterService(RpcServerPort<RPCContext> port) |
| | 25 | | { |
| 0 | 26 | | CRDTServiceCodeGen.RegisterService(port, new CRDTServiceImpl()); |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | public async UniTask<CRDTResponse> SendCrdt(CRDTManyMessages messages, RPCContext context, CancellationToken ct) |
| | 30 | | { |
| | 31 | | // This line is to avoid a race condition because a CRDT message could be sent before the scene was loaded |
| | 32 | | // more info: https://github.com/decentraland/sdk/issues/480#issuecomment-1331309908 |
| 11 | 33 | | await UniTask.WaitUntil(() => context.crdt.MessagingControllersManager.ContainsController(messages.SceneNumb |
| | 34 | | cancellationToken: ct); |
| | 35 | |
|
| 8 | 36 | | await UniTask.WaitWhile(() => context.crdt.MessagingControllersManager.HasScenePendingMessages(messages.Scen |
| | 37 | | cancellationToken: ct); |
| | 38 | |
|
| 2 | 39 | | await UniTask.SwitchToMainThread(ct); |
| | 40 | |
|
| | 41 | | try |
| | 42 | | { |
| 2 | 43 | | using (var iterator = CRDTDeserializer.DeserializeBatch(messages.Payload.Memory)) |
| | 44 | | { |
| 4 | 45 | | while (iterator.MoveNext()) |
| | 46 | | { |
| 2 | 47 | | if (!(iterator.Current is CRDTMessage crdtMessage)) |
| | 48 | | continue; |
| | 49 | |
|
| 2 | 50 | | context.crdt.CrdtMessageReceived?.Invoke(messages.SceneNumber, crdtMessage); |
| | 51 | | } |
| 2 | 52 | | } |
| 2 | 53 | | } |
| | 54 | | catch (Exception e) |
| | 55 | | { |
| 0 | 56 | | Debug.LogError(e); |
| 0 | 57 | | } |
| | 58 | |
|
| 2 | 59 | | return defaultResponse; |
| 2 | 60 | | } |
| | 61 | |
|
| | 62 | | public UniTask<CRDTManyMessages> PullCrdt(PullCRDTRequest request, RPCContext context, CancellationToken ct) |
| | 63 | | { |
| | 64 | | try |
| | 65 | | { |
| 2 | 66 | | if (!context.crdt.scenesOutgoingCrdts.TryGetValue(request.SceneNumber, out CRDTProtocol sceneCrdtState)) |
| | 67 | | { |
| 0 | 68 | | return emptyResponse; |
| | 69 | | } |
| | 70 | |
|
| 2 | 71 | | memoryStream.SetLength(0); |
| | 72 | |
|
| 2 | 73 | | context.crdt.scenesOutgoingCrdts.Remove(request.SceneNumber); |
| | 74 | |
|
| 2 | 75 | | KernelBinaryMessageSerializer.Serialize(binaryWriter, sceneCrdtState); |
| 2 | 76 | | sceneCrdtState.ClearOnUpdated(); |
| | 77 | |
|
| 2 | 78 | | reusableCrdtMessage.SceneId = request.SceneId; |
| 2 | 79 | | reusableCrdtMessage.SceneNumber = request.SceneNumber; |
| 2 | 80 | | reusableCrdtMessage.Payload = ByteString.CopyFrom(memoryStream.ToArray()); |
| | 81 | |
|
| 2 | 82 | | return UniTask.FromResult(reusableCrdtMessage); |
| | 83 | | } |
| | 84 | | catch (Exception e) |
| | 85 | | { |
| 0 | 86 | | Debug.LogError(e); |
| 0 | 87 | | return emptyResponse; |
| | 88 | | } |
| 2 | 89 | | } |
| | 90 | | } |
| | 91 | | } |