예제 #1
0
        public static byte GetHeader(RpcMode mode, MsgType msg)
        {
            byte ret = (byte)mode;

            ret |= (byte)msg;
            return(ret);
        }
예제 #2
0
        private static void ConfigHost(ServerHostBuilder builder, RpcMode mode, bool isLibuv)
        {
            void action(IServiceProvider i, IChannelPipeline j, bool hasLengthField)
            {
                var config  = i.GetRequiredService <IConfigurationRoot>();
                var codec   = i.GetRequiredService <TarsCodecAttribute>();
                var handler = i.GetRequiredService <ServerHandler>();

                if (hasLengthField)
                {
                    var packetMaxSize     = config.GetValue(ServerHostOptions.PacketMaxSize, 100 * 1024 * 1024);
                    var lengthFieldLength = config.GetValue(ServerHostOptions.LengthFieldLength, 4);
                    j.AddLengthFieldHanlder(packetMaxSize, lengthFieldLength);
                }
                j.AddLast(new TarsRequestDecoder(codec), new TarsResponseEncoder(codec), handler);
            }

            switch (mode)
            {
            case RpcMode.Udp:
                builder.UseUdp((x, y) => action(x, y, false));
                break;

            case RpcMode.Tcp when isLibuv:
                builder.UseLibuvTcp((x, y) => action(x, y, true));
                break;

            default:
                builder.UseTcp((x, y) => action(x, y, true));
                break;
            }
        }
예제 #3
0
        public static ServerHostBuilder UseRpc(this ServerHostBuilder builder, RpcMode mode = RpcMode.Tcp, bool isLibuv = true, ServerConfig config = null, params Assembly[] assemblies)
        {
            var ms = new ServerRpcMetadata(assemblies);

            UseTarsCodec(builder);
            builder.ConfigureServices(i =>
            {
                var services = i.AddSingleton <ServerHandler, ServerHandler>();
                foreach (var item in ms.metadatas)
                {
                    services.AddSingleton(item.Value.InterfaceType, item.Value.ServantType);
                }
                services.AddSingleton(j =>
                {
                    foreach (var item in ms.metadatas)
                    {
                        var configMetadata = config?.ServantAdapterConfMap.Values.FirstOrDefault(x => x.ClassType == item.Value.ServantType);

                        if (configMetadata != null)
                        {
                            item.Value.Servant = configMetadata.Servant;
                        }
                        item.Value.ServantInstance = j.GetRequiredService(item.Value.InterfaceType);
                        item.Value.Codec           = j.GetRequiredService(item.Value.CodecType) as CodecAttribute;
                    }
                    return(ms.ChangeRpcMetadataKeyForConfig());
                });
            });
            ConfigHost(builder, mode, isLibuv);
            return(builder);
        }
        public void Rpc <T>(byte compId, byte rpcId, RpcMode mode, T arg)
            where T : INetSerializable
        {
            var msg = StartMessage(compId, rpcId, mode, arg.AllocSize);

            arg.OnSerialize(msg);
            SendMessage(msg, mode);
        }
 public void Rpc <TComp>(byte rpcId, RpcMode mode)
 {
     if (!GetCompId <TComp>(out var cid))
     {
         return;
     }
     Rpc(cid, rpcId, mode);
 }
 public void Rpc <TComp>(byte rpcId, RpcMode mode, params object[] args)
     where TComp : class
 {
     if (!GetCompId <TComp>(out var cid))
     {
         return;
     }
     Rpc(cid, rpcId, mode, args);
 }
        NetMessage StartMessage(byte cid, byte rpcId, RpcMode mode, int size)
        {
            var msg = Room.RoomGetMessage(size + 5);

            msg.Write(RpcUtils.GetHeader(mode, MsgType.Netview));
            Id.OnSerialize(msg);
            msg.Write(cid);
            msg.Write(rpcId);
            return(msg);
        }
예제 #8
0
        NetMessage StartMessage(byte rpcId, RpcMode mode, int size)
        {
            var msg = _room.RoomGetMessage(size + 5);

            msg.Write(RpcUtils.GetHeader(mode, MsgType.Internal));
            msg.Write(RandPRpcs.SceneObjectRpc);
            msg.Write(NetworkID);
            msg.Write(rpcId);
            return(msg);
        }
예제 #9
0
 /// <summary>
 /// the value set from Proxy(INetComponentProxy proxy)
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public T Proxy <T>(RpcMode mode)
 {
     if (_proxies.TryGetValue(typeof(T), out var proxy))
     {
         var ret = (T)proxy;
         proxy.CurrentRpcMode = mode;
         proxy.CurrentSendTo  = null;
         return(ret);
     }
     return(default(T));
 }
예제 #10
0
        public static void NetviewDelivery(RpcMode mode, out NetDeliveryMethod method, out int sequenceChannel)
        {
            switch (mode)
            {
            case RpcMode.AllBuffered:
                method          = NetDeliveryMethod.ReliableOrdered;
                sequenceChannel = 5;
                return;

            case RpcMode.OthersBuffered:
                method          = NetDeliveryMethod.ReliableOrdered;
                sequenceChannel = 6;
                return;

            case RpcMode.AllOrdered:
                method          = NetDeliveryMethod.ReliableOrdered;
                sequenceChannel = 7;
                return;

            case RpcMode.OthersOrdered:
                method          = NetDeliveryMethod.ReliableOrdered;
                sequenceChannel = 8;
                return;

            case RpcMode.AllUnordered:
            case RpcMode.OthersUnordered:
            case RpcMode.OwnerUnordered:
                method          = NetDeliveryMethod.ReliableUnordered;
                sequenceChannel = 0;
                return;

            case RpcMode.AllUnreliable:
            case RpcMode.OthersUnreliable:
            case RpcMode.OwnerUnreliable:
                method          = NetDeliveryMethod.Unreliable;
                sequenceChannel = 0;
                return;

            case RpcMode.OwnerBuffered:
                method          = NetDeliveryMethod.ReliableOrdered;
                sequenceChannel = 9;
                return;

            case RpcMode.OwnerOrdered:
                sequenceChannel = 10;
                method          = NetDeliveryMethod.ReliableOrdered;
                return;

            default:
                sequenceChannel = 11;
                method          = NetDeliveryMethod.ReliableOrdered;
                return;
            }
        }
예제 #11
0
        internal void SendMessage(NetMessage msg, RpcMode mode)
        {
            var reliable  = mode.ReliabilityMode();
            var broadcast = mode.BroadcastMode();

            if (!Owner.IsValid)
            {
                if (broadcast == BroadcastMode.Others)
                {
                    broadcast = BroadcastMode.All;
                }
                else if (broadcast == BroadcastMode.Owner)
                {
                    NetMessage.RecycleMessage(msg);
                    return;
                }
            }

            ImplSendMessage(msg, reliable, broadcast);
        }
예제 #12
0
        public void Rpc(byte compId, byte rpcId, RpcMode mode, params object[] args)
        {
            var size = 0;

            foreach (var arg in args)
            {
                if (arg == null)
                {
                    throw new NullReferenceException("Cannot serialize null value");
                }

                size += Manager.Room.Serializer.SizeOf(arg);
            }

            var msg = StartMessage(compId, rpcId, mode, size);

            foreach (var arg in args)
            {
                Manager.Room.Serializer.Serialize(arg, msg);
            }
            SendMessage(msg, mode);
        }
예제 #13
0
        public void Rpc(byte rpcId, RpcMode mode, params object[] args)
        {
            var size = 0;

            foreach (var arg in args)
            {
                if (arg == null)
                {
                    throw new NullReferenceException("Cannot serialize null value");
                }

                size += _room.Serializer.SizeOf(arg);
            }

            var msg = StartMessage(rpcId, mode.ReliabilityMode(), size, mode.BroadcastMode());

            foreach (var arg in args)
            {
                _room.Serializer.Serialize(arg, msg);
            }
            SendMessage(msg, mode.ReliabilityMode());
        }
예제 #14
0
        public void Rpc(byte compId, byte rpcId, RpcMode mode)
        {
            var msg = StartMessage(compId, rpcId, mode, 0);

            SendMessage(msg, mode);
        }
예제 #15
0
 public static BroadcastMode BroadcastMode(this RpcMode value)
 {
     return(((byte)value).BroadcastMode());
 }
예제 #16
0
 private IRpcClient GetClient(RpcMode mode)
 {
     return(clients.GetOrAdd(mode, m => m == RpcMode.Tcp
     ? (IRpcClient)provider.GetRequiredService <RpcClient <ITcpClient> >()
     : provider.GetRequiredService <RpcClient <IUdpClient> >()));
 }
예제 #17
0
 public static ReliabilityMode ReliabilityMode(this RpcMode value)
 {
     return(((byte)value).ReliabilityMode());
 }