From f496648d335bd4d5a75b331accda69452d4a4092 Mon Sep 17 00:00:00 2001 From: vvaltman Date: Sun, 21 Sep 2014 14:13:40 +0400 Subject: [PATCH] Make get_state once per hour --- loop.c | 5 +++++ queries.c | 30 ++++++++++++++++++++++++++++++ tgl.h | 1 + 3 files changed, 36 insertions(+) diff --git a/loop.c b/loop.c index 5bc51d9..8a276ac 100644 --- a/loop.c +++ b/loop.c @@ -164,6 +164,7 @@ void net_loop (int flags, int (*is_end)(void)) { } event_add (ev, 0); } + int last_get_state = time (0); while (!is_end || !is_end ()) { event_base_loop (tgl_state.ev_base, EVLOOP_ONCE); @@ -183,6 +184,10 @@ void net_loop (int flags, int (*is_end)(void)) { } exit (0); } + if (time (0) - last_get_state > 3600) { + tgl_do_lookup_state (); + last_get_state = time (0); + } write_state_file (); update_prompt (); if (unknown_user_list_pos) { diff --git a/queries.c b/queries.c index f70ff88..c5c30ff 100644 --- a/queries.c +++ b/queries.c @@ -3216,6 +3216,21 @@ static int get_state_on_answer (struct query *q UU) { return 0; } +static int lookup_state_on_answer (struct query *q UU) { + assert (fetch_int () == (int)CODE_updates_state); + int pts = fetch_int (); + int qts = fetch_int (); + fetch_int (); + int seq = fetch_int (); + fetch_int (); + + if (pts > tgl_state.pts || qts > tgl_state.qts || seq > tgl_state.seq) { + tgl_do_get_difference (0, 0, 0); + } + return 0; +} + + //int get_difference_active; static int get_difference_on_answer (struct query *q UU) { //get_difference_active = 0; @@ -3307,6 +3322,11 @@ static int get_difference_on_answer (struct query *q UU) { return 0; } +static struct query_methods lookup_state_methods = { + .on_answer = lookup_state_on_answer, + .type = TYPE_TO_PARAM(updates_state) +}; + static struct query_methods get_state_methods = { .on_answer = get_state_on_answer, .type = TYPE_TO_PARAM(updates_state) @@ -3317,6 +3337,16 @@ static struct query_methods get_difference_methods = { .type = TYPE_TO_PARAM(updates_difference) }; +void tgl_do_lookup_state (void) { + if (tgl_state.locks & TGL_LOCK_DIFF) { + return; + } + clear_packet (); + tgl_do_insert_header (); + out_int (CODE_updates_get_state); + tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &lookup_state_methods, 0, 0, 0); +} + void tgl_do_get_difference (int sync_from_start, void (*callback)(void *callback_extra, int success), void *callback_extra) { //get_difference_active = 1; //difference_got = 0; diff --git a/tgl.h b/tgl.h index cc0cc1a..8ccaf06 100644 --- a/tgl.h +++ b/tgl.h @@ -273,6 +273,7 @@ void tgl_do_create_encr_chat_request (int user_id, void (*callback)(void *callba void tgl_do_create_secret_chat (tgl_peer_id_t id, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra); void tgl_do_accept_encr_chat_request (struct tgl_secret_chat *E, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra); void tgl_do_get_difference (int sync_from_start, void (*callback)(void *callback_extra, int success), void *callback_extra); +void tgl_do_lookup_state (void); void tgl_do_add_user_to_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, int limit, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); void tgl_do_del_user_from_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); void tgl_do_create_group_chat (tgl_peer_id_t id, char *chat_topic, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);