internal static void HandleNetworkedVarUpdate(ulong clientId, Stream stream, Action <ulong, PreBufferPreset> bufferCallback, PreBufferPreset bufferPreset) { if (!NetworkingManager.Singleton.NetworkConfig.EnableNetworkedVar) { if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) { NetworkLog.LogWarning("NetworkedVar update received but EnableNetworkedVar is false"); } return; } using (PooledBitReader reader = PooledBitReader.Get(stream)) { ulong networkId = reader.ReadUInt64Packed(); ushort orderIndex = reader.ReadUInt16Packed(); if (SpawnManager.SpawnedObjects.ContainsKey(networkId)) { NetworkedBehaviour instance = SpawnManager.SpawnedObjects[networkId].GetBehaviourAtOrderIndex(orderIndex); if (instance == null) { if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) { NetworkLog.LogWarning("NetworkedVarUpdate message received for a non existant behaviour. NetworkId: " + networkId + ", behaviourIndex: " + orderIndex); } } else { NetworkedBehaviour.HandleNetworkedVarUpdate(instance.networkedVarFields, stream, clientId, instance); } } else if (NetworkingManager.Singleton.IsServer || !NetworkingManager.Singleton.NetworkConfig.EnableMessageBuffering) { if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) { NetworkLog.LogWarning("NetworkedVarUpdate message received for a non existant object with id: " + networkId + ". This delta was lost."); } } else { if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) { NetworkLog.LogWarning("NetworkedVarUpdate message received for a non existant object with id: " + networkId + ". This delta will be buffered and might be recovered."); } bufferCallback(networkId, bufferPreset); } } }
private RpcTypeDefinition(Type type) { List <ReflectionMethod> delegateMethodsList = new List <ReflectionMethod>(); List <MethodInfo> methods = GetAllMethods(type, typeof(NetworkedBehaviour)); for (int i = 0; i < methods.Count; i++) { MethodInfo method = methods[i]; ParameterInfo[] parameters = method.GetParameters(); ReflectionMethod rpcMethod = ReflectionMethod.Create(method, parameters, delegateMethodsList.Count); if (rpcMethod == null) { continue; } Dictionary <ulong, ReflectionMethod> lookupTarget = rpcMethod.serverTarget ? serverMethods : clientMethods; ulong nameHash = HashMethodNameAndValidate(method.Name); if (!lookupTarget.ContainsKey(nameHash)) { string p = ""; foreach (ParameterInfo pI in parameters) { p += pI.Name + ", "; } Debug.Log("Added RPC method: " + method.Name + ": " + nameHash + " (" + p + ")"); lookupTarget.Add(nameHash, rpcMethod); } if (parameters.Length > 0) { ulong signatureHash = HashMethodNameAndValidate(NetworkedBehaviour.GetHashableMethodSignature(method)); if (!lookupTarget.ContainsKey(signatureHash)) { lookupTarget.Add(signatureHash, rpcMethod); } } if (rpcMethod.useDelegate) { delegateMethodsList.Add(rpcMethod); } } delegateMethods = delegateMethodsList.ToArray(); }
internal static void HandleClientRPC(uint clientId, Stream stream, int channelId) { using (PooledBitReader reader = PooledBitReader.Get(stream)) { uint networkId = reader.ReadUInt32Packed(); ushort behaviourId = reader.ReadUInt16Packed(); ulong hash = reader.ReadUInt64Packed(); if (SpawnManager.SpawnedObjects.ContainsKey(networkId)) { NetworkedBehaviour behaviour = SpawnManager.SpawnedObjects[networkId].GetBehaviourAtOrderIndex(behaviourId); behaviour.OnRemoteClientRPC(hash, clientId, stream); } } }
internal RpcDelegate[] CreateTargetedDelegates(NetworkedBehaviour target) { if (delegateMethods.Length == 0) { return(null); } RpcDelegate[] rpcDelegates = new RpcDelegate[delegateMethods.Length]; for (int i = 0; i < delegateMethods.Length; i++) { rpcDelegates[i] = (RpcDelegate)Delegate.CreateDelegate(typeof(RpcDelegate), target, delegateMethods[i].method.Name); } return(rpcDelegates); }
/// <summary> /// Sends a named message /// </summary> /// <param name="name">The message name to send</param> /// <param name="clientId">The client to send the message to</param> /// <param name="stream">The message stream containing the data</param> /// <param name="channel">The channel tos end the data on</param> /// <param name="security">The security settings to apply to the message</param> public static void SendNamedMessage(string name, ulong clientId, Stream stream, string channel = null, SecuritySendFlags security = SecuritySendFlags.None) { ulong hash = NetworkedBehaviour.HashMethodName(name); using (PooledBitStream messageStream = PooledBitStream.Get()) { using (PooledBitWriter writer = PooledBitWriter.Get(messageStream)) { writer.WriteUInt64Packed(hash); } messageStream.CopyFrom(stream); InternalMessageSender.Send(clientId, MLAPIConstants.MLAPI_NAMED_MESSAGE, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, messageStream, security, null); } }
void SpawnPlayer(Scene _scene1, Scene _scene2) { if (_scene2.name != "Menu") { if (characterSelection == 0) { Ishost = true; AgentSpawned = GameObject.Instantiate(AgentPrefab); //Local player is agent. AgentHealth = AgentSpawned.GetComponent <Health>(); AgentRigidBody = AgentSpawned.GetComponent <Rigidbody>(); previousPlayerVelocity = new Vector3(AgentRigidBody.velocity.x, AgentRigidBody.velocity.y, AgentRigidBody.velocity.z); previousPlayerRotation = new Quaternion(AgentSpawned.transform.rotation.x, AgentSpawned.transform.rotation.y, AgentSpawned.transform.rotation.z, AgentSpawned.transform.rotation.w); SpawnedHacker = GameObject.Instantiate(RemoteHackerPrefab); } else if (characterSelection == 1) { Ishost = false; AgentSpawned = GameObject.Instantiate(RemoteAgentPrefab); AgentHealth = AgentSpawned.GetComponent <Health>(); Vector3 startPosition = FindObjectOfType <PlayerStartLocation>().transform.position; AgentSpawned.transform.position = new Vector3(startPosition.x, startPosition.y, startPosition.z); AgentSpawned.GetComponent <NetworkedMovement>().receivedPosition = new Vector3(startPosition.x, startPosition.y, startPosition.z); AgentRigidBody = AgentSpawned.GetComponent <Rigidbody>(); AgentPrediction = AgentSpawned.GetComponent <NetworkedBehaviour>(); SpawnedHacker = GameObject.Instantiate(HackerPrefab); //Local Player is Hacker. The order of instantiation here is important! } phantomManager = FindObjectOfType(typeof(PhantomManager)) as PhantomManager; if (phantomManager == null) { Debug.LogError("phantomManager not found. F**k."); } //skipeManager.startSkipe(); } else { int bytesreceived = 0; do { bytesreceived = PhaNetworkingAPI.ReceiveFrom(PhaNetworkingAPI.mainSocket, receiveBuffer, recvBufferSize); Debug.Log("Continuing to flush the f*****g buffer, bytes: " + bytesreceived); } while (bytesreceived != 10035); //skipeManager.closeSkipe(); } }
internal static void HandleServerRPCRequest(ulong clientId, Stream stream, string channelName, SecuritySendFlags security) { using (PooledBitReader reader = PooledBitReader.Get(stream)) { ulong networkId = reader.ReadUInt64Packed(); ushort behaviourId = reader.ReadUInt16Packed(); ulong hash = reader.ReadUInt64Packed(); ulong responseId = reader.ReadUInt64Packed(); if (SpawnManager.SpawnedObjects.ContainsKey(networkId)) { NetworkedBehaviour behaviour = SpawnManager.SpawnedObjects[networkId].GetBehaviourAtOrderIndex(behaviourId); if (behaviour == null) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("ServerRPCRequest message recieved for a non existant behaviour. NetworkId: " + networkId + ", behaviourIndex: " + behaviourId); } } else { object result = behaviour.OnRemoteServerRPC(hash, clientId, stream); using (PooledBitStream responseStream = PooledBitStream.Get()) { using (PooledBitWriter responseWriter = PooledBitWriter.Get(responseStream)) { responseWriter.WriteUInt64Packed(responseId); responseWriter.WriteObjectPacked(result); } InternalMessageSender.Send(clientId, MLAPIConstants.MLAPI_SERVER_RPC_RESPONSE, channelName, responseStream, security, SpawnManager.SpawnedObjects[networkId]); } } } else { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("ServerRPCRequest message recieved for a non existant object with id: " + networkId + ". This message is lost."); } } } }
internal static void HandleTargetRpc(uint clientId, byte[] incommingData, int channelId) { BitReader reader = new BitReader(incommingData); uint networkId = reader.ReadUInt(); ushort orderId = reader.ReadUShort(); ulong hash = reader.ReadULong(); NetworkedBehaviour behaviour = SpawnManager.spawnedObjects[networkId].GetBehaviourAtOrderIndex(orderId); MethodInfo targetMethod = null; if (behaviour.cachedMethods.ContainsKey(Data.Cache.GetAttributeMethodName(hash))) { targetMethod = behaviour.cachedMethods[Data.Cache.GetAttributeMethodName(hash)]; } byte paramCount = reader.ReadBits(5); object[] methodParams = FieldTypeHelper.ReadObjects(reader, paramCount); targetMethod.Invoke(behaviour, methodParams); }
//Reads formatted data that the "WriteFormattedSyncedVarData" has written and applies the values to SyncedVar fields internal void SetFormattedSyncedVarData(BitReader reader) { for (int i = 0; i < childNetworkedBehaviours.Count; i++) { childNetworkedBehaviours[i].SyncVarInit(); if (childNetworkedBehaviours[i].syncedVarFields.Count == 0) { continue; } NetworkedBehaviour behaviour = GetBehaviourAtOrderIndex(reader.ReadUShort()); for (int j = 0; j < childNetworkedBehaviours[i].syncedVarFields.Count; j++) { childNetworkedBehaviours[i].syncedVarFields[j].FieldInfo.SetValue(behaviour, FieldTypeHelper.ReadFieldType(reader, childNetworkedBehaviours[i].syncedVarFields[j].FieldInfo.FieldType)); } behaviour.OnSyncVarUpdate(); } }
internal object Invoke(NetworkedBehaviour target, ulong senderClientId, Stream stream) { if (requireOwnership == true && senderClientId != target.OwnerClientId) { if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) { NetworkLog.LogWarning("Only owner can invoke ServerRPC that is marked to require ownership"); } return(null); } target.executingRpcSender = senderClientId; if (stream.Position == 0) { if (useDelegate) { return(InvokeDelegate(target, senderClientId, stream)); } else { return(InvokeReflected(target, stream)); } } else { // Create a new stream so that the stream they get ONLY contains user data and not MLAPI headers using (PooledBitStream userStream = PooledBitStream.Get()) { userStream.CopyUnreadFrom(stream); userStream.Position = 0; if (useDelegate) { return(InvokeDelegate(target, senderClientId, userStream)); } else { return(InvokeReflected(target, userStream)); } } } }
internal static void HandleClientRPC(ulong clientId, Stream stream, Action <ulong> bufferCallback) { using (PooledBitReader reader = PooledBitReader.Get(stream)) { ulong networkId = reader.ReadUInt64Packed(); ushort behaviourId = reader.ReadUInt16Packed(); ulong hash = reader.ReadUInt64Packed(); if (SpawnManager.SpawnedObjects.ContainsKey(networkId)) { NetworkedBehaviour behaviour = SpawnManager.SpawnedObjects[networkId].GetBehaviourAtOrderIndex(behaviourId); if (behaviour == null) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("ClientRPC message recieved for a non existant behaviour. NetworkId: " + networkId + ", behaviourIndex: " + behaviourId); } } else { behaviour.OnRemoteClientRPC(hash, clientId, stream); } } else if (NetworkingManager.Singleton.IsServer || !NetworkingManager.Singleton.NetworkConfig.EnableMessageBuffering) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("ClientRPC message recieved for a non existant object with id: " + networkId + ". This message is lost."); } } else { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("ClientRPC message recieved for a non existant object with id: " + networkId + ". This message will be buffered and might be recovered."); } bufferCallback(networkId); } } }
internal static void HandleNetworkedVarUpdate(ulong clientId, Stream stream) { if (!NetworkingManager.Singleton.NetworkConfig.EnableNetworkedVar) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("NetworkedVar update received but EnableNetworkedVar is false"); } return; } using (PooledBitReader reader = PooledBitReader.Get(stream)) { ulong networkId = reader.ReadUInt64Packed(); ushort orderIndex = reader.ReadUInt16Packed(); if (SpawnManager.SpawnedObjects.ContainsKey(networkId)) { NetworkedBehaviour instance = SpawnManager.SpawnedObjects[networkId].GetBehaviourAtOrderIndex(orderIndex); if (instance == null) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("NetworkedVar message recieved for a non existant behaviour"); } return; } NetworkedBehaviour.HandleNetworkedVarUpdate(instance.networkedVarFields, stream, clientId, instance); } else { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("NetworkedVar message recieved for a non existant object with id: " + networkId); } return; } } }
private static ulong HashMethodNameAndValidate(string name) { ulong hash = NetworkedBehaviour.HashMethodName(name); if (hashResults.ContainsKey(hash)) { string hashResult = hashResults[hash]; if (hashResult != name) { if (LogHelper.CurrentLogLevel <= LogLevel.Error) { LogHelper.LogError("Hash collision detected for RPC method. The method \"" + name + "\" collides with the method \"" + hashResult + "\". This can be solved by increasing the amount of bytes to use for hashing in the NetworkConfig or changing the name of one of the conflicting methods."); } } } else { hashResults.Add(hash, name); } return(hash); }
/// <summary> /// Sends the named message /// </summary> /// <param name="name">The message name to send</param> /// <param name="clientIds">The clients to send to, sends to everyone if null</param> /// <param name="stream">The message stream containing the data</param> /// <param name="channel">The channel to send the data on</param> /// <param name="security">The security settings to apply to the message</param> public static void SendNamedMessage(string name, List <ulong> clientIds, Stream stream, string channel = null, SecuritySendFlags security = SecuritySendFlags.None) { ulong hash = NetworkedBehaviour.HashMethodName(name); using (PooledBitStream messageStream = PooledBitStream.Get()) { using (PooledBitWriter writer = PooledBitWriter.Get(messageStream)) { writer.WriteUInt64Packed(hash); } messageStream.CopyFrom(stream); if (!NetworkingManager.Singleton.IsServer) { if (NetworkLog.CurrentLogLevel <= LogLevel.Error) { NetworkLog.LogWarning("Can not send named messages to multiple users as a client"); } return; } if (clientIds == null) { for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++) { InternalMessageSender.Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_NAMED_MESSAGE, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, messageStream, security, null); } } else { for (int i = 0; i < clientIds.Count; i++) { InternalMessageSender.Send(clientIds[i], MLAPIConstants.MLAPI_NAMED_MESSAGE, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, messageStream, security, null); } } } }
public SharedItem(NetworkedBehaviour context = null) : base(context) { type = typeof(SharedItem); }
public void SetNetworkedBehaviour(NetworkedBehaviour behaviour) { networkedBehaviour = behaviour; }
private void OnEnable() { script = (NetworkedBehaviour)target; }
/// <summary> /// Registers a named message handler delegate. /// </summary> /// <param name="name">Name of the message.</param> /// <param name="callback">The callback to run when a named message is received.</param> public static void RegisterNamedMessageHandler(string name, HandleNamedMessageDelegate callback) { ulong hash = NetworkedBehaviour.HashMethodName(name); namedMessageHandlers[hash] = callback; }
private object InvokeDelegate(NetworkedBehaviour target, ulong senderClientId, Stream stream) { target.rpcDelegates[index](senderClientId, stream); return(null); }
public AbilityItem(NetworkedBehaviour context = null) : base(context) { type = typeof(AbilityItem); }
public Item(NetworkedBehaviour context) : base(context) { }