| | 1 | | using DCl.Social.Friends; |
| | 2 | | using RPC.Services; |
| | 3 | | using RPC.Transports; |
| | 4 | | using rpc_csharp; |
| | 5 | | using rpc_csharp.transport; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | |
|
| | 9 | | namespace RPC |
| | 10 | | { |
| | 11 | | public static class RPCServerBuilder |
| | 12 | | { |
| | 13 | | public static RpcServer<RPCContext> BuildDefaultServer(RPCContext context) |
| | 14 | | { |
| | 15 | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | 16 | | var transport = new WebGLTransport(); |
| | 17 | | #else |
| | 18 | | var transport = new WebSocketTransport(); |
| | 19 | | #endif |
| | 20 | | return BuildDefaultServer(context, transport); |
| | 21 | | } |
| | 22 | |
|
| | 23 | | public static RpcServer<RPCContext> BuildDefaultServer(RPCContext context, ITransport transport) |
| | 24 | | { |
| | 25 | | return BuildServer(context, transport) |
| | 26 | | .RegisterService(CRDTServiceImpl.RegisterService) |
| | 27 | | .RegisterService(TransportServiceImpl.RegisterService) |
| | 28 | | .RegisterService(EmotesRendererServiceImpl.RegisterService) |
| | 29 | | .RegisterService(RPCFriendsApiBridge.RegisterService) |
| | 30 | | .RegisterService(SceneControllerServiceImpl.RegisterService) |
| | 31 | | .RegisterService(RestrictedActionsServiceImpl.RegisterService) |
| | 32 | | .Build(); |
| | 33 | | } |
| | 34 | |
|
| | 35 | | public static RPCServerBuilder<RPCContext> BuildServer(RPCContext context, ITransport transport) |
| | 36 | | { |
| | 37 | | return new RPCServerBuilder<RPCContext>(transport, context); |
| | 38 | | } |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public class RPCServerBuilder<T> |
| | 42 | | { |
| | 43 | | private readonly ITransport transport; |
| | 44 | | private readonly T context; |
| 425 | 45 | | private readonly List<Action<RpcServerPort<T>>> servicesRegisterer = new List<Action<RpcServerPort<T>>>(); |
| | 46 | |
|
| 425 | 47 | | internal RPCServerBuilder(ITransport transport, T context) |
| | 48 | | { |
| 425 | 49 | | this.transport = transport; |
| 425 | 50 | | this.context = context; |
| 425 | 51 | | } |
| | 52 | |
|
| | 53 | | public RPCServerBuilder<T> RegisterService(Action<RpcServerPort<T>> serviceRegisterer) |
| | 54 | | { |
| 2550 | 55 | | servicesRegisterer.Add(serviceRegisterer); |
| 2550 | 56 | | return this; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public RpcServer<T> Build() |
| | 60 | | { |
| 425 | 61 | | var services = servicesRegisterer.ToArray(); |
| 425 | 62 | | var rpcServer = new RpcServer<T>(); |
| 425 | 63 | | rpcServer.AttachTransport(transport, context); |
| | 64 | |
|
| 425 | 65 | | rpcServer.SetHandler((port, t, c) => |
| | 66 | | { |
| 0 | 67 | | for (int i = 0; i < services.Length; i++) |
| | 68 | | { |
| 0 | 69 | | services[i].Invoke(port); |
| | 70 | | } |
| 0 | 71 | | }); |
| 425 | 72 | | return rpcServer; |
| | 73 | | } |
| | 74 | | } |
| | 75 | | } |