/// <summary> /// Check if bytes stack contains internal id for the register /// </summary> /// <param name="sender">The peer from which message has been received</param> /// <param name="distributedMessage">Checked message</param> /// <returns>True if bytes stack contains internal id for register, false otherwise</returns> public bool IsInitializationMessage(IPeerManager sender, DistributedMessage distributedMessage) { if (isInternalIdBound || AssignIds) { return(false); } //Check if this is not an initial message var command = (IdsRegisterCommandType)distributedMessage.Content.PeekInt(BytesPerCommandType); if (command != IdsRegisterCommandType.BindIdAndKey) { return(false); } try { var offset = BytesPerCommandType; var key = distributedMessage.Content.PeekString(offset); if (Key == key) { offset += 4 + Encoding.UTF8.GetBytes(key).Length; var id = distributedMessage.Content.PeekInt(BytesPerId, offset); offset += BytesPerId; isInternalIdBound = true; var timeDifference = distributedMessage.Content.PeekLong(8, offset); var calculatedTimestamp = messagesManager.TimeManager.GetTimestamp(timeDifference); InternalIdBindUtcTime = distributedMessage.ServerTimestamp; idRegistrationTimestamp.Add(id, InternalIdBindUtcTime); idToObjectDictionary.Add(id, this); keyToIdDictionary.Add(Key, id); ObjectBoundToId?.Invoke(this, id); return(true); } } catch (IndexOutOfRangeException) { return(false); } return(false); }
private void SpawnNPCMock(DistributedMessage message) { var data = new NPCSpawnData() { Active = message.Content.PopBool(), GenId = message.Content.PopString(), Template = NPCVehicles[message.Content.PopInt(2)], Seed = message.Content.PopInt(), Color = message.Content.PopDecompressedColor(1), Position = message.Content.PopDecompressedPosition(), Rotation = message.Content.PopDecompressedRotation() }; var npc = SpawnNPC(data); Destroy(npc.ActiveBehaviour); var rb = npc.GetComponentInChildren <Rigidbody>(); rb.interpolation = RigidbodyInterpolation.Interpolate; rb.collisionDetectionMode = CollisionDetectionMode.Discrete; }
public void ReceiveMessage(IPeerManager sender, DistributedMessage distributedMessage) { var isMaster = Loader.Instance.Network.IsMaster; if (!isMaster) { return; } var messageType = distributedMessage.Content.PopEnum <MessageType>(); switch (messageType) { case MessageType.LockingCommandExecuted: ActionsSemaphore.Unlock(); break; default: throw new ArgumentOutOfRangeException(); } }
public void ReceiveMessage(IPeerManager sender, DistributedMessage distributedMessage) { var commandType = distributedMessage.Content.PopEnum <NPCManagerCommandType>(); switch (commandType) { case NPCManagerCommandType.SpawnNPC: SpawnNPCMock(distributedMessage.Content.PopString(), NPCVehicles[distributedMessage.Content.PopInt(2)], distributedMessage.Content.PopInt(), distributedMessage.Content.PopDecompressedColor(1), distributedMessage.Content.PopDecompressedPosition(), distributedMessage.Content.PopDecompressedRotation()); break; case NPCManagerCommandType.DespawnNPC: var npcId = distributedMessage.Content.PopInt(2); //TODO Preserve despawn command if it arrives before spawn command if (npcId >= 0 && npcId < CurrentPooledNPCs.Count) { Destroy(CurrentPooledNPCs[npcId].gameObject); } break; } }
/// <summary> /// Return message to the pool /// </summary> /// <param name="message">Message to be returned</param> public void ReleaseMessage(DistributedMessage message) { if (message.OriginPool != this) { return; } var poolIndex = BytesToIndex(message.Content.StackSize); message.OriginPool = null; message.Content.Reset(); message.Timestamp = DateTime.MinValue; if (ClearKey) { message.AddressKey = null; } message.Type = DistributedMessageType.ReliableOrdered; lock (pools) { pools[poolIndex].Add(message); } }
/// <inheritdoc/> public void ReceiveMessage(IPeerManager sender, DistributedMessage distributedMessage) { var methodName = distributedMessage.Content.PopEnum <NPCControllerMethodName>(); switch (methodName) { case NPCControllerMethodName.SetLights: SetLights(distributedMessage.Content.PopInt()); break; case NPCControllerMethodName.SetBrakeLights: SetBrakeLights(distributedMessage.Content.PopBool()); break; case NPCControllerMethodName.SetNPCTurnSignal: SetNPCTurnSignal(distributedMessage.Content.PopBool(), distributedMessage.Content.PopBool(), distributedMessage.Content.PopBool()); break; case NPCControllerMethodName.SetNPCHazards: SetNPCHazards(distributedMessage.Content.PopBool()); break; case NPCControllerMethodName.SetIndicatorReverse: SetIndicatorReverse(distributedMessage.Content.PopBool()); break; case NPCControllerMethodName.ResetLights: ResetLights(); break; case NPCControllerMethodName.SetSpeed: ActiveBehaviour.currentSpeed = distributedMessage.Content.PopFloat(); break; default: throw new ArgumentOutOfRangeException(); } }
public void ReceiveMessage(IPeerManager sender, DistributedMessage distributedMessage) { if (!Loader.Instance.Network.IsClusterSimulation || !Loader.Instance.Network.IsClient) { return; } //Currently single control actions are supported var controllableId = idsRegister.PopId(distributedMessage.Content); var controller = idsRegister.ResolveObject(controllableId) as ControllableController; if (controller == null) { return; } var action = new ControlAction { Action = distributedMessage.Content.PopString(), Value = distributedMessage.Content.PopString() }; controller.Controllable.Control(new List <ControlAction> { action }); }
public void ReceiveMessage(IPeerManager sender, DistributedMessage distributedMessage) { var commandType = distributedMessage.Content.PopEnum <NPCManagerCommandType>(); switch (commandType) { case NPCManagerCommandType.SpawnNPC: SpawnNPCMock(distributedMessage); break; case NPCManagerCommandType.DespawnNPC: var npcId = distributedMessage.Content.PopInt(2); //TODO Preserve despawn command if it arrives before spawn command if (npcId >= 0 && npcId < CurrentPooledNPCs.Count) { var controller = CurrentPooledNPCs[npcId]; if (controller != null) { Destroy(controller.gameObject); } } break; } }
public void BroadcastMessage(DistributedMessage distributedMessage) { Loader.Instance.Network.MessagesManager?.BroadcastMessage(distributedMessage); }
/// <inheritdoc/> public void ReceiveMessage(IPeerManager sender, DistributedMessage distributedMessage) { var command = (IdsRegisterCommandType)distributedMessage.Content.PopInt( ByteCompression .RequiredBytes <IdsRegisterCommandType>()); var key = distributedMessage.Content.PopString(); var id = distributedMessage.Content.PopInt(BytesPerId); IIdentifiedObject registeredObject; int awaitingId; switch (command) { case IdsRegisterCommandType.BindIdAndKey: if (AssignIds) { return; } //Check if object is already registered if ((idToObjectDictionary.TryGetValue(id, out registeredObject) && registeredObject.Key == key) || (awaitingKeyIdBinds.TryGetValue(key, out awaitingId) && awaitingId == id)) { return; } //New bind to the id received before receiving unbind command if (registeredObject != null) { UnbindKeyId(key, id); idRegistrationTimestamp.Remove(id); } idRegistrationTimestamp.Add(id, distributedMessage.ServerTimestamp); TryBindReceiver(key, id); break; case IdsRegisterCommandType.UnbindIdAndKey: if (AssignIds) { return; } //Remove awaiting binding if it is available if (awaitingKeyIdBinds.TryGetValue(key, out awaitingId) && awaitingId == id) { awaitingKeyIdBinds.Remove(key); idRegistrationTimestamp.Remove(id); return; } //Check if object have not been unbounded already if (!idToObjectDictionary.TryGetValue(id, out registeredObject)) { return; } //Check if bound object has the same key if (registeredObject.Key != key) { return; } UnbindKeyId(key, id); idRegistrationTimestamp.Remove(id); break; } }
/// <inheritdoc/> public void Broadcast(DistributedMessage distributedMessage) { masterPeer?.Send(distributedMessage); distributedMessage.Release(); }
public void UnicastMessage(IPEndPoint endPoint, DistributedMessage distributedMessage) { Loader.Instance.Network.MessagesManager?.UnicastMessage(endPoint, distributedMessage); }
/// <summary> /// Pushes current time difference from epoch time to the message (in milliseconds) /// </summary> /// <param name="distributedMessage">Message where time difference will be pushed</param> public void PushTimeDifference(DistributedMessage distributedMessage) { distributedMessage.TimeTicksDifference = CurrentTicksDifference; distributedMessage.Content.PushLong(distributedMessage.TimeTicksDifference); }
/// <inheritdoc /> public void ReceiveMessage(IPeerManager sender, DistributedMessage distributedMessage) { Debug.Assert(MasterPeer == null || MasterPeer == sender); PacketsProcessor.ReadAllPackets(new NetDataReader(distributedMessage.Content.GetDataCopy()), sender); }
/// <summary> /// Parsing received snapshot /// </summary> /// <param name="distributedMessage">Received snapshot in the message</param> protected abstract void ApplySnapshot(DistributedMessage distributedMessage);
/// <summary> /// Method broadcasting the delta message /// </summary> /// <param name="deltaMessage">Delta message to send</param> public virtual void BroadcastDelta(DistributedMessage deltaMessage) { deltaMessage.Content.PushEnum <ComponentMessageType>((int)ComponentMessageType.Delta); BroadcastMessage(deltaMessage); }
/// <summary> /// Parses the received message and calls proper abstract method /// </summary> /// <param name="distributedMessage">Received snapshot in the message</param> protected virtual void ParseMessage(DistributedMessage distributedMessage) { ApplySnapshot(distributedMessage); }
/// <summary> /// Method handling received message from the connection manager /// </summary> /// <param name="distributedMessage">Received message</param> private void ConnectionManagerOnMessageReceived(DistributedMessage distributedMessage) { MessageReceived(distributedMessage); }
/// <inheritdoc/> public void UnicastMessage(IPEndPoint endPoint, DistributedMessage distributedMessage) { messagesManager.UnicastMessage(endPoint, distributedMessage); }
/// <summary> /// Method unicasting the delta message /// </summary> /// <param name="endPoint">Endpoint of the target client</param> /// <param name="deltaMessage">Delta message to send</param> public virtual void UnicastDelta(IPEndPoint endPoint, DistributedMessage deltaMessage) { deltaMessage.Content.PushEnum <ComponentMessageType>((int)ComponentMessageType.Delta); UnicastMessage(endPoint, deltaMessage); }
/// <summary> /// Parsing received delta /// </summary> /// <param name="distributedMessage">Received delta in a message</param> protected abstract void ApplyDelta(DistributedMessage distributedMessage);
/// <inheritdoc/> public void BroadcastMessage(DistributedMessage distributedMessage) { messagesManager.BroadcastMessage(distributedMessage); }
/// <inheritdoc/> public void Unicast(IPEndPoint endPoint, DistributedMessage distributedMessage) { peers[endPoint].Send(distributedMessage); distributedMessage.Release(); }