| | 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 | | { |
| 3 | 31 | | await UniTask.WaitWhile(() => context.crdtContext.MessagingControllersManager.HasScenePendingMessages(messag |
| | 32 | | cancellationToken: ct); |
| | 33 | |
|
| 0 | 34 | | await UniTask.SwitchToMainThread(ct); |
| | 35 | |
|
| | 36 | | try |
| | 37 | | { |
| 0 | 38 | | using (var iterator = CRDTDeserializer.DeserializeBatch(messages.Payload.Memory)) |
| | 39 | | { |
| 0 | 40 | | while (iterator.MoveNext()) |
| | 41 | | { |
| 0 | 42 | | if (!(iterator.Current is CRDTMessage crdtMessage)) |
| | 43 | | continue; |
| | 44 | |
|
| 0 | 45 | | context.crdtContext.CrdtMessageReceived?.Invoke(messages.SceneId, crdtMessage); |
| | 46 | | } |
| 0 | 47 | | } |
| 0 | 48 | | } |
| | 49 | | catch (Exception e) |
| | 50 | | { |
| 0 | 51 | | Debug.LogError(e); |
| 0 | 52 | | } |
| | 53 | |
|
| 0 | 54 | | return defaultResponse; |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public UniTask<CRDTManyMessages> PullCrdt(PullCRDTRequest request, RPCContext context, CancellationToken ct) |
| | 58 | | { |
| 2 | 59 | | string sceneId = request.SceneId; |
| | 60 | |
|
| | 61 | | try |
| | 62 | | { |
| 2 | 63 | | if (!context.crdtContext.scenesOutgoingCrdts.TryGetValue(sceneId, out CRDTProtocol sceneCrdtState)) |
| | 64 | | { |
| 0 | 65 | | return emptyResponse; |
| | 66 | | } |
| | 67 | |
|
| 2 | 68 | | memoryStream.SetLength(0); |
| | 69 | |
|
| 2 | 70 | | context.crdtContext.scenesOutgoingCrdts.Remove(sceneId); |
| | 71 | |
|
| 2 | 72 | | KernelBinaryMessageSerializer.Serialize(binaryWriter, sceneCrdtState); |
| 2 | 73 | | sceneCrdtState.ClearOnUpdated(); |
| | 74 | |
|
| 2 | 75 | | reusableCrdtMessage.SceneId = sceneId; |
| 2 | 76 | | reusableCrdtMessage.Payload = ByteString.CopyFrom(memoryStream.ToArray()); |
| | 77 | |
|
| 2 | 78 | | return UniTask.FromResult(reusableCrdtMessage); |
| | 79 | | } |
| | 80 | | catch (Exception e) |
| | 81 | | { |
| 0 | 82 | | Debug.LogError(e); |
| 0 | 83 | | return emptyResponse; |
| | 84 | | } |
| 2 | 85 | | } |
| | 86 | | } |
| | 87 | | } |