private void CheckForMissedSends() { for (int i = 0; i < NetworkManager.Singleton.ConnectedClientsList.Count; i++) { if (!m_ClientSendInfo.ContainsKey(NetworkManager.Singleton.ConnectedClientsList[i].ClientId)) { m_ClientSendInfo.Add(NetworkManager.Singleton.ConnectedClientsList[i].ClientId, new ClientSendInfo() { LastMissedPosition = null, LastMissedRotation = null, LastSent = 0 }); } ClientSendInfo info = m_ClientSendInfo[NetworkManager.Singleton.ConnectedClientsList[i].ClientId]; Vector3? receiverPosition = NetworkManager.Singleton.ConnectedClientsList[i].PlayerObject == null ? null : new Vector3?(NetworkManager.Singleton.ConnectedClientsList[i].PlayerObject.transform.position); Vector3? senderPosition = NetworkManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject == null ? null : new Vector3?(NetworkManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject.transform.position); if ((receiverPosition == null || senderPosition == null && NetworkManager.Singleton.NetworkTime - info.LastSent >= (1f / FixedSendsPerSecond)) || NetworkManager.Singleton.NetworkTime - info.LastSent >= GetTimeForLerp(receiverPosition.Value, senderPosition.Value)) { /* why is this??? ->*/ Vector3?pos = NetworkManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject == null ? null : new Vector3?(NetworkManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject.transform.position); /* why is this??? ->*/ Vector3?rot = NetworkManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject == null ? null : new Vector3?(NetworkManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject.transform.rotation.eulerAngles); if (info.LastMissedPosition != null && info.LastMissedRotation != null) { info.LastSent = NetworkManager.Singleton.NetworkTime; ApplyTransformClientRpc(info.LastMissedPosition.Value, info.LastMissedRotation.Value.eulerAngles, new ClientRpcParams { Send = new ClientRpcSendParams { TargetClientIds = new[] { NetworkManager.Singleton.ConnectedClientsList[i].ClientId } } }); info.LastMissedPosition = null; info.LastMissedRotation = null; } } } }
private void SubmitTransform(ulong clientId, Stream stream) { if (!enabled) { return; } using (PooledBitReader reader = PooledBitReader.Get(stream)) { float xPos = reader.ReadSinglePacked(); float yPos = reader.ReadSinglePacked(); float zPos = reader.ReadSinglePacked(); float xRot = reader.ReadSinglePacked(); float yRot = reader.ReadSinglePacked(); float zRot = reader.ReadSinglePacked(); if (IsMoveValidDelegate != null && !IsMoveValidDelegate(lerpEndPos, new Vector3(xPos, yPos, zPos))) { //Invalid move! //TODO: Add rubber band (just a message telling them to go back) return; } using (PooledBitStream writeStream = PooledBitStream.Get()) { using (PooledBitWriter writer = PooledBitWriter.Get(writeStream)) { writer.WriteSinglePacked(xPos); writer.WriteSinglePacked(yPos); writer.WriteSinglePacked(zPos); writer.WriteSinglePacked(xRot); writer.WriteSinglePacked(yRot); writer.WriteSinglePacked(zRot); if (EnableRange) { for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++) { if (!clientSendInfo.ContainsKey(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId)) { clientSendInfo.Add(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, new ClientSendInfo() { clientId = NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, lastMissedPosition = null, lastMissedRotation = null, lastSent = 0 }); } ClientSendInfo info = clientSendInfo[NetworkingManager.Singleton.ConnectedClientsList[i].ClientId]; Vector3? receiverPosition = NetworkingManager.Singleton.ConnectedClientsList[i].PlayerObject == null ? null : new Vector3?(NetworkingManager.Singleton.ConnectedClientsList[i].PlayerObject.transform.position); Vector3? senderPosition = NetworkingManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject == null ? null : new Vector3?(NetworkingManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject.transform.position); if ((receiverPosition == null || senderPosition == null && NetworkingManager.Singleton.NetworkTime - info.lastSent >= (1f / FixedSendsPerSecond)) || NetworkingManager.Singleton.NetworkTime - info.lastSent >= GetTimeForLerp(receiverPosition.Value, senderPosition.Value)) { info.lastSent = NetworkingManager.Singleton.NetworkTime; info.lastMissedPosition = null; info.lastMissedRotation = null; InvokeClientRpcOnClientPerformance(ApplyTransform, NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, writeStream, string.IsNullOrEmpty(Channel) ? "MLAPI_DEFAULT_MESSAGE" : Channel); } else { info.lastMissedPosition = new Vector3(xPos, yPos, zPos); info.lastMissedRotation = Quaternion.Euler(xRot, yRot, zRot); } } } else { InvokeClientRpcOnEveryoneExceptPerformance(ApplyTransform, OwnerClientId, writeStream, string.IsNullOrEmpty(Channel) ? "MLAPI_DEFAULT_MESSAGE" : Channel); } } } } }
private void SubmitTransform(uint clientId, Stream stream) { if (!enabled) { return; } using (PooledBitReader reader = PooledBitReader.Get(stream)) { float xPos = reader.ReadSinglePacked(); float yPos = reader.ReadSinglePacked(); float zPos = reader.ReadSinglePacked(); float xRot = reader.ReadSinglePacked(); float yRot = reader.ReadSinglePacked(); float zRot = reader.ReadSinglePacked(); if (IsMoveValidDelegate != null && !IsMoveValidDelegate(lerpEndPos, new Vector3(xPos, yPos, zPos))) { //Invalid move! //TODO: Add rubber band (just a message telling them to go back) return; } using (PooledBitStream writeStream = PooledBitStream.Get()) { using (PooledBitWriter writer = PooledBitWriter.Get(writeStream)) { writer.WriteSinglePacked(xPos); writer.WriteSinglePacked(yPos); writer.WriteSinglePacked(zPos); writer.WriteSinglePacked(xRot); writer.WriteSinglePacked(yRot); writer.WriteSinglePacked(zRot); if (EnableRange) { for (int i = 0; i < NetworkingManager.singleton.ConnectedClientsList.Count; i++) { if (!clientSendInfo.ContainsKey(NetworkingManager.singleton.ConnectedClientsList[i].ClientId)) { clientSendInfo.Add(NetworkingManager.singleton.ConnectedClientsList[i].ClientId, new ClientSendInfo() { clientId = NetworkingManager.singleton.ConnectedClientsList[i].ClientId, lastMissedPosition = null, lastMissedRotation = null, lastSent = 0 }); } ClientSendInfo info = clientSendInfo[NetworkingManager.singleton.ConnectedClientsList[i].ClientId]; Vector3 receiverPosition = NetworkingManager.singleton.ConnectedClientsList[i].PlayerObject.transform.position; Vector3 senderPosition = NetworkingManager.singleton.ConnectedClients[OwnerClientId].PlayerObject.transform.position; if (NetworkingManager.singleton.NetworkTime - info.lastSent >= GetTimeForLerp(receiverPosition, senderPosition)) { info.lastSent = NetworkingManager.singleton.NetworkTime; info.lastMissedPosition = null; info.lastMissedRotation = null; InvokeClientRpcOnClient(ApplyTransform, NetworkingManager.singleton.ConnectedClientsList[i].ClientId, writeStream); } else { info.lastMissedPosition = new Vector3(xPos, yPos, zPos); info.lastMissedRotation = Quaternion.Euler(xRot, yRot, zRot); } } } else { InvokeClientRpcOnEveryoneExcept(ApplyTransform, OwnerClientId, writeStream); } } } } }
private void SubmitTransformServerRpc(Vector3 position, Vector3 eulerAngles, ServerRpcParams rpcParams = default) { if (!enabled) { return; } if (IsMoveValidDelegate != null && !IsMoveValidDelegate(rpcParams.Receive.SenderClientId, m_LerpEndPos, position)) { //Invalid move! //TODO: Add rubber band (just a message telling them to go back) return; } if (!IsClient) { // Dedicated server ApplyTransformInternal(position, Quaternion.Euler(eulerAngles)); } if (EnableRange) { for (int i = 0; i < NetworkManager.Singleton.ConnectedClientsList.Count; i++) { if (!m_ClientSendInfo.ContainsKey(NetworkManager.Singleton.ConnectedClientsList[i].ClientId)) { m_ClientSendInfo.Add(NetworkManager.Singleton.ConnectedClientsList[i].ClientId, new ClientSendInfo() { LastMissedPosition = null, LastMissedRotation = null, LastSent = 0 }); } ClientSendInfo info = m_ClientSendInfo[NetworkManager.Singleton.ConnectedClientsList[i].ClientId]; Vector3? receiverPosition = NetworkManager.Singleton.ConnectedClientsList[i].PlayerObject == null ? null : new Vector3?(NetworkManager.Singleton.ConnectedClientsList[i].PlayerObject.transform.position); Vector3? senderPosition = NetworkManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject == null ? null : new Vector3?(NetworkManager.Singleton.ConnectedClients[OwnerClientId].PlayerObject.transform.position); if ((receiverPosition == null || senderPosition == null && NetworkManager.Singleton.NetworkTime - info.LastSent >= (1f / FixedSendsPerSecond)) || NetworkManager.Singleton.NetworkTime - info.LastSent >= GetTimeForLerp(receiverPosition.Value, senderPosition.Value)) { info.LastSent = NetworkManager.Singleton.NetworkTime; info.LastMissedPosition = null; info.LastMissedRotation = null; ApplyTransformClientRpc(position, eulerAngles, new ClientRpcParams { Send = new ClientRpcSendParams { TargetClientIds = new[] { NetworkManager.Singleton.ConnectedClientsList[i].ClientId } } }); } else { info.LastMissedPosition = position; info.LastMissedRotation = Quaternion.Euler(eulerAngles); } } } else { ApplyTransformClientRpc(position, eulerAngles, new ClientRpcParams { Send = new ClientRpcSendParams { TargetClientIds = NetworkManager.Singleton.ConnectedClientsList.Where(c => c.ClientId != OwnerClientId).Select(c => c.ClientId).ToArray() } }); } }
private void OnRecieveTransformFromClient(uint clientId, BitReader reader) { if (!enabled) { return; } byte[] data = reader.ReadByteArray(); using (MemoryStream readStream = new MemoryStream(data)) { using (BinaryReader bReader = new BinaryReader(readStream)) { float xPos = bReader.ReadSingle(); float yPos = bReader.ReadSingle(); float zPos = bReader.ReadSingle(); float xRot = bReader.ReadSingle(); float yRot = bReader.ReadSingle(); float zRot = bReader.ReadSingle(); if (IsMoveValidDelegate != null && !IsMoveValidDelegate(lerpEndPos, new Vector3(xPos, yPos, zPos))) { //Invalid move! //TODO: Add rubber band (just a message telling them to go back) return; } if (InterpolateServer) { lerpStartPos = transform.position; lerpStartRot = transform.rotation; lerpEndPos = new Vector3(xPos, yPos, zPos); lerpEndRot = Quaternion.Euler(xRot, yRot, zRot); lerpT = 0; } else { transform.position = new Vector3(xPos, yPos, zPos); transform.rotation = Quaternion.Euler(new Vector3(xRot, yRot, zRot)); } using (MemoryStream writeStream = new MemoryStream(positionUpdateBuffer)) { using (BinaryWriter writer = new BinaryWriter(writeStream)) { writer.Write(xPos); writer.Write(yPos); writer.Write(zPos); writer.Write(xRot); writer.Write(yRot); writer.Write(zRot); } if (EnableRange) { // For instead of Foreach?! TODO!!! for (int i = 0; i < NetworkingManager.singleton.ConnectedClientsList.Count; i++) { if (!clientSendInfo.ContainsKey(NetworkingManager.singleton.ConnectedClientsList[i].ClientId)) { clientSendInfo.Add(NetworkingManager.singleton.ConnectedClientsList[i].ClientId, new ClientSendInfo() { clientId = NetworkingManager.singleton.ConnectedClientsList[i].ClientId, lastMissedPosition = null, lastMissedRotation = null, lastSent = 0 }); } ClientSendInfo info = clientSendInfo[NetworkingManager.singleton.ConnectedClientsList[i].ClientId]; Vector3 receiverPosition = NetworkingManager.singleton.ConnectedClientsList[i].PlayerObject.transform.position; Vector3 senderPosition = NetworkingManager.singleton.ConnectedClients[OwnerClientId].PlayerObject.transform.position; if (NetworkingManager.singleton.NetworkTime - info.lastSent >= GetTimeForLerp(receiverPosition, senderPosition)) { info.lastSent = NetworkingManager.singleton.NetworkTime; info.lastMissedPosition = null; info.lastMissedRotation = null; SendToClientTarget(NetworkingManager.singleton.ConnectedClientsList[i].ClientId, "MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer, true); } else { info.lastMissedPosition = new Vector3(xPos, yPos, zPos); info.lastMissedRotation = Quaternion.Euler(xRot, yRot, zRot); } } } else { SendToNonLocalClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer, true); } } } } }