Now throttle state updates if there are many pieces in the room.

This should prevent the network traffic from getting too congested
when a lot of pieces are all sending state updates back to the
clients.
This commit is contained in:
drwhut 2022-03-18 19:20:03 +00:00
parent e3a600793e
commit fb883cf39a
3 changed files with 30 additions and 3 deletions

View File

@ -1710,6 +1710,10 @@ func _physics_process(_delta):
return
if get_tree().is_network_server():
# TODO: This does not need to be done every frame, find a way to run
# this just whenever the number of active pieces changes.
_srv_update_bandwidth_throttle()
if _srv_hand_setup_frames >= 0:
_srv_hand_setup_frames -= 1
@ -2086,6 +2090,19 @@ func _set_hidden_area_transform(hidden_area: HiddenArea, point1: Vector2, point2
hidden_area.transform.basis.x.x = point_dif.x / 2
hidden_area.transform.basis.z.z = point_dif.y / 2
# Update the rate at which state updates are sent to the client, based on the
# number of active pieces in the room.
func _srv_update_bandwidth_throttle() -> void:
# TODO: Instead of using the total number of pieces, use the number of
# pieces that are NOT sleeping. This could be done with
# PhysicsServer.get_process_info, but it has not been implemented with the
# Bullet physics engine.
# See: https://github.com/godotengine/godot/issues/59279
var physics_frames_per_update = floor(1.0 + _pieces.get_child_count() / Global.SRV_PIECE_UPDATE_TRANSMIT_LIMIT)
if Global.srv_num_physics_frames_per_state_update != physics_frames_per_update:
Global.srv_num_physics_frames_per_state_update = physics_frames_per_update
print("State update rate set to %dHz." % (60 / physics_frames_per_update))
func _on_container_absorbing_hovered(container: PieceContainer, player_id: int) -> void:
if get_tree().is_network_server():
var names = []

View File

@ -472,9 +472,10 @@ func _physics_process(_delta):
_new_velocity = linear_velocity
if get_tree().is_network_server():
if not _last_server_state_invalid:
if not (sleeping or is_hovering() or is_locked()):
rpc_unreliable("ss", _last_server_state_0, _last_server_state_1)
if Engine.get_physics_frames() % Global.srv_num_physics_frames_per_state_update == 0:
if not _last_server_state_invalid:
if not (sleeping or is_hovering() or is_locked()):
rpc_unreliable("ss", _last_server_state_0, _last_server_state_1)
# Apply forces to the piece to get it to the desired hover position and
# orientation.
@ -534,6 +535,8 @@ func _integrate_forces(state):
var new_transform = Transform(lerp_quat)
new_transform.origin = origin
state.transform = new_transform
_last_server_state_invalid = true
func _on_body_entered(body):
# If we collided with another object...

View File

@ -34,6 +34,11 @@ const LOADING_BLOCK_TIME = 20
var system_locale: String = ""
# Throttle the piece state transmissions if there are many active physics
# objects.
const SRV_PIECE_UPDATE_TRANSMIT_LIMIT = 20
var srv_num_physics_frames_per_state_update: int = 1
var _current_scene: Node = null
var _loader: ResourceInteractiveLoader = null
var _loader_args: Dictionary = {}
@ -243,6 +248,8 @@ func _set_scene(scene: Node, args: Dictionary) -> void:
_current_scene.start_host()
MODE_SINGLEPLAYER:
_current_scene.start_singleplayer()
srv_num_physics_frames_per_state_update = 1
# Terminate the network peer if it exists.
# NOTE: This function should be called via call_deferred.