From e5f524b0a89fb0eb2256d53a10e2fec2c6c7de26 Mon Sep 17 00:00:00 2001 From: Vysheng Date: Fri, 22 Aug 2014 15:45:16 +0400 Subject: [PATCH] fix queries_num --- loop.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- main.c | 14 ++++++++----- queries.c | 8 +++---- tgl.h | 1 + 4 files changed, 74 insertions(+), 12 deletions(-) diff --git a/loop.c b/loop.c index c99968e..0f4e819 100644 --- a/loop.c +++ b/loop.c @@ -64,10 +64,10 @@ extern int unknown_user_list_pos; extern int unknown_user_list[]; int register_mode; extern int safe_quit; -int queries_num; extern int sync_from_start; void got_it (char *line, int len); +void write_state_file (void); static void stdin_read_callback (evutil_socket_t fd, short what, void *arg) { if (((long)arg) & 1) { @@ -95,11 +95,12 @@ void net_loop (int flags, int (*is_end)(void)) { #ifdef USE_LUA lua_do_all (); #endif - if (safe_quit && !queries_num) { + if (safe_quit && !tgl_state.active_queries) { printf ("All done. Exit\n"); rl_callback_handler_remove (); exit (0); } + write_state_file (); if (unknown_user_list_pos) { int i; for (i = 0; i < unknown_user_list_pos; i++) { @@ -231,6 +232,62 @@ int wait_dialog_list; extern struct tgl_update_callback upd_cb; #define DC_SERIALIZED_MAGIC 0x868aa81d +#define STATE_FILE_MAGIC 0x28949a93 +#define SECRET_FILE_MAGIX 0x37a1988a +char *get_auth_key_filename (void); +char *get_state_filename (void); + +void read_state_file (void) { + if (binlog_enabled) { return; } + int state_file_fd = open (get_state_filename (), O_CREAT | O_RDWR, 0600); + if (state_file_fd < 0) { + return; + } + int version, magic; + if (read (state_file_fd, &magic, 4) < 4) { close (state_file_fd); return; } + if (magic != (int)STATE_FILE_MAGIC) { close (state_file_fd); return; } + if (read (state_file_fd, &version, 4) < 4) { close (state_file_fd); return; } + assert (version >= 0); + int x[4]; + if (read (state_file_fd, x, 16) < 16) { + close (state_file_fd); + return; + } + int pts = x[0]; + int qts = x[1]; + int seq = x[2]; + int date = x[3]; + close (state_file_fd); + bl_do_set_seq (seq); + bl_do_set_pts (pts); + bl_do_set_qts (qts); + bl_do_set_date (date); +} + + +void write_state_file (void) { + if (binlog_enabled) { return; } + static int wseq; + static int wpts; + static int wqts; + static int wdate; + if (wseq >= tgl_state.seq && wpts >= tgl_state.pts && wqts >= tgl_state.qts && wdate >= tgl_state.date) { return; } + wseq = tgl_state.seq; wpts = tgl_state.pts; wqts = tgl_state.qts; wdate = tgl_state.date; + int state_file_fd = open (get_state_filename (), O_CREAT | O_RDWR, 0600); + if (state_file_fd < 0) { + logprintf ("Can not write state file '%s': %m\n", get_state_filename ()); + exit (2); + } + int x[6]; + x[0] = STATE_FILE_MAGIC; + x[1] = 0; + x[2] = wpts; + x[3] = wqts; + x[4] = wseq; + x[5] = wdate; + assert (write (state_file_fd, x, 24) == 24); + close (state_file_fd); +} void write_dc (struct tgl_dc *DC, void *extra) { int auth_file_fd = *(int *)extra; @@ -253,7 +310,6 @@ void write_dc (struct tgl_dc *DC, void *extra) { assert (write (auth_file_fd, DC->auth_key, 256) == 256); } -char *get_auth_key_filename (void); void write_auth_file (void) { if (binlog_enabled) { return; } int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, 0600); @@ -350,6 +406,7 @@ int loop (void) { tgl_reopen_binlog_for_writing (); } else { read_auth_file (); + read_state_file (); } binlog_read = 1; //exit (0); diff --git a/main.c b/main.c index cf9eb77..1a2c413 100644 --- a/main.c +++ b/main.c @@ -321,7 +321,6 @@ void parse_config (void) { parse_config_val (&conf, &auth_file_name, "auth_file", AUTH_KEY_FILE, config_directory); parse_config_val (&conf, &downloads_directory, "downloads", DOWNLOADS_DIRECTORY, config_directory); - parse_config_val (&conf, &binlog_file_name, "binlog", BINLOG_FILE, config_directory); if (!lua_file) { parse_config_val (&conf, &lua_file, "lua_script", 0, config_directory); @@ -331,11 +330,14 @@ void parse_config (void) { config_lookup_bool (&conf, buf, &binlog_enabled); if (binlog_enabled) { + parse_config_val (&conf, &binlog_file_name, "binlog", BINLOG_FILE, config_directory); tgl_set_binlog_mode (1); tgl_set_binlog_path (binlog_file_name); } else { tgl_set_binlog_mode (0); - tgl_set_auth_file_path (auth_file_name); + parse_config_val (&conf, &state_file_name, "state_file", STATE_FILE, config_directory); + parse_config_val (&conf, &secret_chat_file_name, "secret", SECRET_CHAT_FILE, config_directory); + //tgl_set_auth_file_path (auth_file_name); } tgl_set_download_directory (downloads_directory); @@ -349,16 +351,18 @@ void parse_config (void) { #else void parse_config (void) { printf ("libconfig not enabled\n"); - tasprintf (&auth_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, AUTH_KEY_FILE); tasprintf (&downloads_directory, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, DOWNLOADS_DIRECTORY); - tasprintf (&binlog_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, BINLOG_FILE); if (binlog_enabled) { + tasprintf (&binlog_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, BINLOG_FILE); tgl_set_binlog_mode (1); tgl_set_binlog_path (binlog_file_name); } else { tgl_set_binlog_mode (0); - tgl_set_auth_file_path (auth_file_name; + //tgl_set_auth_file_path (auth_file_name; + tasprintf (&auth_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, AUTH_KEY_FILE); + tasprintf (&state_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, STATE_FILE); + tasprintf (&secret_chat_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, SECRET_CHAT_FILE); } tgl_set_download_directory (downloads_directory); } diff --git a/queries.c b/queries.c index fa93689..2a40968 100644 --- a/queries.c +++ b/queries.c @@ -73,7 +73,7 @@ char *get_downloads_directory (void); //extern int binlog_enabled; //extern int sync_from_start; -static int queries_num; +//static int queries_num; static void out_peer_id (tgl_peer_id_t id); #define QUERY_TIMEOUT 6.0 @@ -159,7 +159,7 @@ struct query *tglq_send_query (struct tgl_dc *DC, int ints, void *data, struct q q->extra = extra; q->callback = callback; q->callback_extra = callback_extra; - queries_num ++; + tgl_state.active_queries ++; return q; } @@ -201,7 +201,7 @@ void tglq_query_error (long long id) { event_free (q->ev); tfree (q, sizeof (*q)); } - queries_num --; + tgl_state.active_queries --; } #define MAX_PACKED_SIZE (1 << 24) @@ -267,7 +267,7 @@ void tglq_query_result (long long id UU) { in_ptr = end; in_end = eend; } - queries_num --; + tgl_state.active_queries --; } diff --git a/tgl.h b/tgl.h index ad11d1c..34ff6ae 100644 --- a/tgl.h +++ b/tgl.h @@ -109,6 +109,7 @@ struct tgl_state { int test_mode; int verbosity; int unread_messages; + int active_queries; long long locks; struct tgl_dc *DC_list[TGL_MAX_DC_NUM];