| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using System.Threading; |
| | 5 | | using Cysharp.Threading.Tasks; |
| | 6 | | using DCL; |
| | 7 | | using DCL.CRDT; |
| | 8 | | using Google.Protobuf; |
| | 9 | | using KernelCommunication; |
| | 10 | | using rpc_csharp; |
| | 11 | | using UnityEngine; |
| | 12 | | using BinaryWriter = KernelCommunication.BinaryWriter; |
| | 13 | |
|
| | 14 | | namespace RPC.Services |
| | 15 | | { |
| | 16 | | public static class CRDTServiceImpl |
| | 17 | | { |
| 1 | 18 | | private static readonly UniTask<CRDTResponse> defaultResponse = UniTask.FromResult(new CRDTResponse()); |
| 1 | 19 | | private static readonly UniTask<CRDTManyMessages> emptyResponse = UniTask.FromResult(new CRDTManyMessages() { Sc |
| | 20 | |
|
| 1 | 21 | | private static readonly CRDTManyMessages reusableCrdtMessage = new CRDTManyMessages(); |
| | 22 | |
|
| 1 | 23 | | private static readonly CRDTStream crdtStream = new CRDTStream(); |
| 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 | | { |
| 2 | 29 | | CRDTService<RPCContext>.RegisterService( |
| | 30 | | port, |
| | 31 | | sendCrdt: OnCRDTReceived, |
| | 32 | | pullCrdt: SendCRDT, |
| | 33 | | crdtNotificationStream: CrdtNotificationStream |
| | 34 | | ); |
| 2 | 35 | | } |
| | 36 | |
|
| | 37 | | private static UniTask<CRDTResponse> OnCRDTReceived(CRDTManyMessages messages, RPCContext context, CancellationT |
| | 38 | | { |
| 1 | 39 | | messages.Payload.WriteTo(crdtStream); |
| | 40 | |
|
| 1 | 41 | | var sceneMessagesPool = context.crdtContext.messageQueueHandler.sceneMessagesPool; |
| | 42 | |
|
| | 43 | | try |
| | 44 | | { |
| 1 | 45 | | using (var iterator = KernelBinaryMessageDeserializer.Deserialize(crdtStream)) |
| | 46 | | { |
| 2 | 47 | | while (iterator.MoveNext()) |
| | 48 | | { |
| 1 | 49 | | if (!(iterator.Current is CRDTMessage crdtMessage)) |
| | 50 | | continue; |
| | 51 | |
|
| 1 | 52 | | if (!sceneMessagesPool.TryDequeue(out QueuedSceneMessage_Scene queuedMessage)) |
| | 53 | | { |
| 1 | 54 | | queuedMessage = new QueuedSceneMessage_Scene(); |
| | 55 | | } |
| 1 | 56 | | queuedMessage.method = MessagingTypes.CRDT_MESSAGE; |
| 1 | 57 | | queuedMessage.type = QueuedSceneMessage.Type.SCENE_MESSAGE; |
| 1 | 58 | | queuedMessage.sceneId = messages.SceneId; |
| 1 | 59 | | queuedMessage.payload = crdtMessage; |
| | 60 | |
|
| 1 | 61 | | context.crdtContext.messageQueueHandler.EnqueueSceneMessage(queuedMessage); |
| | 62 | | } |
| 1 | 63 | | } |
| 1 | 64 | | } |
| | 65 | | catch (Exception e) |
| | 66 | | { |
| 0 | 67 | | Debug.LogError(e); |
| 0 | 68 | | } |
| | 69 | |
|
| 1 | 70 | | return defaultResponse; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | private static UniTask<CRDTManyMessages> SendCRDT(PullCRDTRequest request, RPCContext context, CancellationToken |
| | 74 | | { |
| 2 | 75 | | string sceneId = request.SceneId; |
| | 76 | |
|
| | 77 | | try |
| | 78 | | { |
| 2 | 79 | | if (!context.crdtContext.scenesOutgoingCrdts.TryGetValue(sceneId, out CRDTProtocol sceneCrdtState)) |
| | 80 | | { |
| 0 | 81 | | return emptyResponse; |
| | 82 | | } |
| | 83 | |
|
| 2 | 84 | | memoryStream.SetLength(0); |
| | 85 | |
|
| 2 | 86 | | context.crdtContext.scenesOutgoingCrdts.Remove(sceneId); |
| | 87 | |
|
| 2 | 88 | | KernelBinaryMessageSerializer.Serialize(binaryWriter, sceneCrdtState); |
| 2 | 89 | | sceneCrdtState.ClearOnUpdated(); |
| | 90 | |
|
| 2 | 91 | | reusableCrdtMessage.SceneId = sceneId; |
| 2 | 92 | | reusableCrdtMessage.Payload = ByteString.CopyFrom(memoryStream.ToArray()); |
| | 93 | |
|
| 2 | 94 | | return UniTask.FromResult(reusableCrdtMessage); |
| | 95 | | } |
| | 96 | | catch (Exception e) |
| | 97 | | { |
| 0 | 98 | | Debug.LogError(e); |
| 0 | 99 | | return emptyResponse; |
| | 100 | | } |
| 2 | 101 | | } |
| | 102 | |
|
| | 103 | | [Obsolete("deprecated")] |
| | 104 | | private static IEnumerator<CRDTManyMessages> CrdtNotificationStream(CRDTStreamRequest request, RPCContext contex |
| | 105 | | { |
| 0 | 106 | | yield break; |
| | 107 | | } |
| | 108 | | } |
| | 109 | | } |