/// <summary> /// Invokes the named method on the behaviours of all connected clients. /// Therefore, this is only valid when called on the server. /// If the server is a host (client & server at the same time), then the method is also invoked /// locally. /// </summary> /// <param name="methodName">The name of the method to invoke.</param> /// <param name="args"></param> protected void InvokeRPC(string methodName, params object[] args) { if (!JNet.IsServer) { JNet.Error($"Cannot call Rpc {methodName} because server is not active!"); return; } if (!NetObject.HasNetID) { JNet.Error($"Cannot call Rpc {methodName} because the object that {GetType().FullName} is on is not net spawned."); return; } MethodInfo method = GetRpc(methodName); if (method == null) { JNet.Error($"Could not find valid method with Rpc tag {methodName}. Check tags, name spelling and regenerate netcode!"); return; } methodName = methodName.Trim().ToLower(); byte methodID = CustomGeneratedBehaviour.GetMethodID(methodName); CheckDeliveryMethod(); NetOutgoingMessage msg = JNet.GetServer().CreateMessage(Internal.JDataType.RMC); msg.Write(NetObject.NetID); msg.Write(this.BehaviourID); msg.Write(methodID); bool worked = ArgsToMsg(method, args, msg); if (!worked) { JNet.Error("Did not invoke Rpc due to error."); return; } var server = JNet.GetServer(); server.SendToAllExcept(server.LocalClientConnection, msg, RMCDeliveryMethod, 24); // Call on local client, if we are a local client... if (JNet.IsClient) { method.Invoke(this, args); } }
public void SerializeAll() { if (!JNet.IsServer) { JNet.Error("Not on server, cannot serialize all."); return; } for (int i = 0; i < ActiveObjects.Count; i++) { var obj = ActiveObjects[i]; if(obj == null) { JNet.Error("Null object in tracked objects list."); ActiveObjects.RemoveAt(i); i--; continue; } if(obj.NetBehaviours != null) { foreach (var b in obj.NetBehaviours) { if(b != null && b.NetDirty) { JNetServer server = JNet.GetServer(); NetOutgoingMessage msg = server.CreateMessage(); msg.Write((byte)JDataType.SERIALIZED); msg.Write(obj.NetID); msg.Write(b.BehaviourID); b.NetSerialize(msg, false); server.SendToAllExcept(server.LocalClientConnection, msg, b.SerializationDeliveryMethod, 1); b.NetDirty = false; } } } } }