From 0acb1e30ca74c5edffaaf8ffef357946c9105395 Mon Sep 17 00:00:00 2001 From: Nick Couchman Date: Wed, 30 Dec 2020 23:13:35 -0500 Subject: [PATCH] TUTORIAL: Account for network lag. --- src/protocols/ball/ball.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/protocols/ball/ball.c b/src/protocols/ball/ball.c index 97f9947a..66f01874 100644 --- a/src/protocols/ball/ball.c +++ b/src/protocols/ball/ball.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -37,15 +38,30 @@ void* ball_render_thread(void* arg) { guac_client* client = (guac_client*) arg; ball_client_data* data = (ball_client_data*) client->data; + /* Init time of last frame to current time */ + guac_timestamp last_frame = guac_timestamp_current(); + /* Update ball position as long as client is running */ while (client->state == GUAC_CLIENT_RUNNING) { - /* Sleep a bit */ - usleep(30000); + /* Default to 30ms frames */ + int frame_duration = 30; + + /* Lengthen frame duration if client is lagging */ + int processing_lag = guac_client_get_processing_lag(client); + if (processing_lag > frame_duration) + frame_duration = processing_lag; + + /* Sleep for duration of frame, then get timestamp */ + usleep(frame_duration); + guac_timestamp current = guac_timestamp_current(); + + /* Calculate change in time */ + int delta_t = current - last_frame; /* Update position */ - data->ball_x += data->ball_velocity_x * 30 / 1000; - data->ball_y += data->ball_velocity_y * 30 / 1000; + data->ball_x += data->ball_velocity_x * delta_t / 1000; + data->ball_y += data->ball_velocity_y * delta_t / 1000; /* Bounce if necessary */ if (data->ball_x < 0) { @@ -72,6 +88,9 @@ void* ball_render_thread(void* arg) { /* End frame and flush socket */ guac_client_end_frame(client); guac_socket_flush(client->socket); + + /* Update timestamp */ + last_frame = current; }