Пример #1
0
 public static RpcMessage RpcServer(NetworkObject netObj, RpcInfo rpcInfo, object[] args)
 {
     return(new RpcMessage()
     {
         action = Action_RpcServer,
         netObj = netObj,
         rpcInfo = rpcInfo,
         args = args,
     });
 }
Пример #2
0
        public override void Deserialize(NetworkReader reader)
        {
            action = reader.ReadByte();

            if (action == 0)
            {
                throw new Exception("action is 0");
            }

            NetworkInstanceId instanceId;

            instanceId = reader.ReadNetworkInstanceId();

            netObj = null;

            netObj = conn.GetObject(instanceId);
            if (netObj == null)
            {
                return;
            }

            switch (action)
            {
            case Action_RpcClient:
            case Action_RpcServer:

                if (action == Action_RpcClient)
                {
                    if (!netObj.IsClient)
                    {
                        return;
                    }
                }
                else if (action == Action_RpcServer)
                {
                    if (!netObj.IsServer)
                    {
                        return;
                    }
                }

                byte memberIndex = reader.ReadByte();
                rpcInfo = RpcInfo.GetRpcInfo(netObj.GetType(), memberIndex);
                object[] args = null;
                if (rpcInfo.paramCount > 0)
                {
                    args = new object[rpcInfo.paramCount];
                    for (int i = 0; i < rpcInfo.paramCount; i++)
                    {
                        var pInfo = rpcInfo.parameters[i];
                        if (i == 0 && pInfo.ParameterType == typeof(NetworkConnection))
                        {
                            args[i] = conn;
                        }
                        else
                        {
                            args[i] = SyncVarMessage.Read(reader, pInfo.ParameterType);
                        }
                    }
                }

                rpcInfo.method.Invoke(netObj, args);
                break;
            }
        }
Пример #3
0
        public static RpcInfo[] GetRpcInfos(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (cachedRpcs == null)
            {
                cachedRpcs = new Dictionary <Type, RpcInfo[]>();
            }
            RpcInfo[] infos;
            if (!cachedRpcs.TryGetValue(type, out infos))
            {
                List <RpcInfo> list = null;
                foreach (var mInfo in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    var attr = (RpcAttribute)(mInfo.GetCustomAttributes(typeof(RpcAttribute), true).FirstOrDefault());
                    if (attr == null)
                    {
                        continue;
                    }

                    if (mInfo.ReturnType != typeof(void))
                    {
                        throw new Exception("method return type only [void]. " + mInfo);
                    }

                    foreach (var pInfo in mInfo.GetParameters())
                    {
                        if (pInfo.ParameterType == typeof(NetworkConnection))
                        {
                        }
                        else
                        {
                            if (!SyncVarMessage.CanSerializeType(pInfo.ParameterType))
                            {
                                throw new Exception("invalid parameter type :" + pInfo.ParameterType + "," + pInfo);
                            }
                        }
                        if (pInfo.IsOut)
                        {
                            throw new Exception("method parameter can't [out]. " + mInfo);
                        }
                    }

                    if (list == null)
                    {
                        list = new List <RpcInfo>();
                    }
                    RpcInfo info = new RpcInfo()
                    {
                        method     = mInfo,
                        parameters = mInfo.GetParameters()
                    };
                    info.paramCount = info.parameters.Length;
                    list.Add(info);
                }

                if (list != null && list.Count > 0)
                {
                    infos = list.OrderBy(o => o.method.Name).ToArray();
                    for (int i = 0; i < infos.Length; i++)
                    {
                        var info = infos[i];
                        info.memberIndex = (byte)i;
                    }
                    cachedRpcs[type] = infos;
                }
            }
            return(infos);
        }