From 8a39f039b07e948535e6ea5471a170fed9143fc4 Mon Sep 17 00:00:00 2001 From: Vysheng Date: Mon, 2 Dec 2013 21:19:08 +0400 Subject: [PATCH] Many fixes. --- binlog.c | 9 ++++-- main.c | 31 ++++++++++++------- mtproto-client.c | 4 +-- net.c | 2 +- structures.c | 80 +++++++++++++++++++++++++++++++++++------------- structures.h | 1 + telegram.h | 3 +- tree.h | 2 ++ 8 files changed, 93 insertions(+), 39 deletions(-) diff --git a/binlog.c b/binlog.c index 8f5379e..24624b5 100644 --- a/binlog.c +++ b/binlog.c @@ -1033,8 +1033,13 @@ void replay_log_event (void) { message_remove_tree (M); message_del_peer (M); M->id = *(rptr ++); - message_insert_tree (M); - message_add_peer (M); + if (message_get (M->id)) { + free_message (M); + free (M); + } else { + message_insert_tree (M); + message_add_peer (M); + } } break; case CODE_update_user_photo: diff --git a/main.c b/main.c index 10fc3be..4cdaf9a 100644 --- a/main.c +++ b/main.c @@ -86,24 +86,33 @@ void set_default_username (const char *s) { default_username = strdup (s); } + /* {{{ TERMINAL */ -tcflag_t old_lflag; -cc_t old_vtime; -struct termios term; +static struct termios term_in, term_out; +static int term_set_in; +static int term_set_out; void get_terminal_attributes (void) { - if (tcgetattr (STDIN_FILENO, &term) < 0) { - perror ("tcgetattr()"); - exit (EXIT_FAILURE); + if (tcgetattr (STDIN_FILENO, &term_in) < 0) { + } else { + term_set_in = 1; + } + if (tcgetattr (STDOUT_FILENO, &term_out) < 0) { + } else { + term_set_out = 1; } - old_lflag = term.c_lflag; - old_vtime = term.c_cc[VTIME]; } void set_terminal_attributes (void) { - if (tcsetattr (STDIN_FILENO, 0, &term) < 0) { - perror ("tcsetattr()"); - exit (EXIT_FAILURE); + if (term_set_in) { + if (tcsetattr (STDIN_FILENO, 0, &term_in) < 0) { + perror ("tcsetattr()"); + } + } + if (term_set_out) { + if (tcsetattr (STDOUT_FILENO, 0, &term_out) < 0) { + perror ("tcsetattr()"); + } } } /* }}} */ diff --git a/mtproto-client.c b/mtproto-client.c index 83e43f6..27034ef 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -1284,8 +1284,8 @@ void work_updates (struct connection *c, long long msg_id) { for (i = 0; i < n; i++) { fetch_alloc_chat (); } - fetch_date (); // date - fetch_seq (); // seq + bl_do_set_date (fetch_int ()); + bl_do_set_seq (fetch_int ()); } void work_update_short_message (struct connection *c UU, long long msg_id UU) { diff --git a/net.c b/net.c index 546f48d..7d5db84 100644 --- a/net.c +++ b/net.c @@ -411,7 +411,7 @@ void hexdump_buf (struct connection_buffer *b) { void try_rpc_read (struct connection *c) { assert (c->in_head); - if (verbosity >= 1) { + if (verbosity >= 3) { hexdump_buf (c->in_head); } diff --git a/structures.c b/structures.c index 6356ed6..3c88a24 100644 --- a/structures.c +++ b/structures.c @@ -52,19 +52,27 @@ struct tree_message *message_unsent_tree; int users_allocated; int chats_allocated; int messages_allocated; - -int our_id; -int verbosity; -peer_t *Peers[MAX_USER_NUM]; int peer_num; int encr_chats_allocated; int geo_chats_allocated; + +int our_id; +int verbosity; + +peer_t *Peers[MAX_PEER_NUM]; extern int binlog_enabled; void fetch_skip_photo (void); #define code_assert(x) if (!(x)) { logprintf ("Can not parse at line %d\n", __LINE__); assert (0); return -1; } +#define code_try(x) if ((x) == -1) { return -1; } + +/* + * + * Fetch simple structures (immediate fetch into buffer) + * + */ int fetch_file_location (struct file_location *loc) { int x = fetch_int (); @@ -84,18 +92,6 @@ int fetch_file_location (struct file_location *loc) { return 0; } -int fetch_skip_file_location (void) { - int x = fetch_int (); - code_assert (x == CODE_file_location_unavailable || x == CODE_file_location); - - if (x == CODE_file_location_unavailable) { - in_ptr += 5; - } else { - in_ptr += 6; - } - return 0; -} - int fetch_user_status (struct user_status *S) { unsigned x = fetch_int (); code_assert (x == CODE_user_status_empty || x == CODE_user_status_online || x == CODE_user_status_offline); @@ -118,6 +114,33 @@ int fetch_user_status (struct user_status *S) { return 0; } +/* + * + * Skip simple structures + * + */ + +int fetch_skip_file_location (void) { + int x = fetch_int (); + code_assert (x == CODE_file_location_unavailable || x == CODE_file_location); + + if (x == CODE_file_location_unavailable) { + in_ptr += 5; + } else { + in_ptr += 6; + } + return 0; +} + +int fetch_skip_user_status (void) { + unsigned x = fetch_int (); + code_assert (x == CODE_user_status_empty || x == CODE_user_status_online || x == CODE_user_status_offline); + if (x != CODE_user_status_empty) { + fetch_int (); + } + return 0; +} + char *create_print_name (peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4) { const char *d[4]; d[0] = a1; d[1] = a2; d[2] = a3; d[3] = a4; @@ -159,6 +182,12 @@ char *create_print_name (peer_id_t id, const char *a1, const char *a2, const cha return strdup (s); } +/* + * + * Fetch with log event + * + */ + long long fetch_user_photo (struct user *U) { unsigned x = fetch_int (); code_assert (x == CODE_user_profile_photo || x == CODE_user_profile_photo_old || x == CODE_user_profile_photo_empty); @@ -172,8 +201,8 @@ long long fetch_user_photo (struct user *U) { } struct file_location big; struct file_location small; - if (fetch_file_location (&small) < 0) { return -1; } - if (fetch_file_location (&big) < 0) { return -1; } + code_try (fetch_file_location (&small)); + code_try (fetch_file_location (&big)); bl_do_set_user_profile_photo (U, photo_id, &big, &small); return 0; @@ -183,9 +212,6 @@ int fetch_user (struct user *U) { unsigned x = fetch_int (); code_assert (x == CODE_user_empty || x == CODE_user_self || x == CODE_user_contact || x == CODE_user_request || x == CODE_user_foreign || x == CODE_user_deleted); U->id = MK_USER (fetch_int ()); - if ((U->flags & FLAG_CREATED) && x == CODE_user_empty) { - return 0; - } if (x == CODE_user_empty) { return 0; } @@ -204,8 +230,10 @@ int fetch_user (struct user *U) { } if (new) { int l1 = prefetch_strlen (); + code_assert (l1 >= 0); char *s1 = fetch_str (l1); int l2 = prefetch_strlen (); + code_assert (l2 >= 0); char *s2 = fetch_str (l2); if (x == CODE_user_deleted && !(U->flags & FLAG_DELETED)) { @@ -221,6 +249,7 @@ int fetch_user (struct user *U) { char *phone = 0; if (x != CODE_user_foreign) { phone_len = prefetch_strlen (); + code_assert (phone_len >= 0); phone = fetch_str (phone_len); } bl_do_new_user (get_peer_id (U->id), s1, l1, s2, l2, access_token, phone, phone_len, x == CODE_user_contact); @@ -1496,6 +1525,7 @@ struct user *fetch_alloc_user (void) { memset (U, 0, sizeof (*U)); U->id = MK_USER (data[1]); peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); + assert (peer_num < MAX_PEER_NUM); Peers[peer_num ++] = U; } fetch_user (&U->user); @@ -1512,6 +1542,7 @@ struct secret_chat *fetch_alloc_encrypted_chat (void) { U->id = MK_ENCR_CHAT (data[1]); encr_chats_allocated ++; peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); + assert (peer_num < MAX_PEER_NUM); Peers[peer_num ++] = U; } fetch_encrypted_chat (&U->encr_chat); @@ -1521,18 +1552,21 @@ struct secret_chat *fetch_alloc_encrypted_chat (void) { void insert_encrypted_chat (peer_t *P) { encr_chats_allocated ++; peer_tree = tree_insert_peer (peer_tree, P, lrand48 ()); + assert (peer_num < MAX_PEER_NUM); Peers[peer_num ++] = P; } void insert_user (peer_t *P) { users_allocated ++; peer_tree = tree_insert_peer (peer_tree, P, lrand48 ()); + assert (peer_num < MAX_PEER_NUM); Peers[peer_num ++] = P; } void insert_chat (peer_t *P) { chats_allocated ++; peer_tree = tree_insert_peer (peer_tree, P, lrand48 ()); + assert (peer_num < MAX_PEER_NUM); Peers[peer_num ++] = P; } @@ -1550,6 +1584,7 @@ struct user *fetch_alloc_user_full (void) { U->id = MK_USER (data[2]); peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); fetch_user_full (&U->user); + assert (peer_num < MAX_PEER_NUM); Peers[peer_num ++] = U; return &U->user; } @@ -1707,6 +1742,7 @@ void message_add_peer (struct message *M) { break; } peer_tree = tree_insert_peer (peer_tree, P, lrand48 ()); + assert (peer_num < MAX_PEER_NUM); Peers[peer_num ++] = P; } if (!P->last) { @@ -1856,6 +1892,7 @@ struct chat *fetch_alloc_chat (void) { memset (U, 0, sizeof (*U)); U->id = MK_CHAT (data[1]); peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); + assert (peer_num < MAX_PEER_NUM); Peers[peer_num ++] = U; } fetch_chat (&U->chat); @@ -1876,6 +1913,7 @@ struct chat *fetch_alloc_chat_full (void) { U->id = MK_CHAT (data[2]); peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); fetch_chat_full (&U->chat); + assert (peer_num < MAX_PEER_NUM); Peers[peer_num ++] = U; return &U->chat; } diff --git a/structures.h b/structures.h index 202e522..7e316fe 100644 --- a/structures.h +++ b/structures.h @@ -371,6 +371,7 @@ void send_all_unsent (void); void message_remove_tree (struct message *M); void message_add_peer (struct message *M); void message_del_peer (struct message *M); +void free_message (struct message *M); #define PEER_USER 1 #define PEER_CHAT 2 diff --git a/telegram.h b/telegram.h index 0ec1fd5..41315ce 100644 --- a/telegram.h +++ b/telegram.h @@ -17,5 +17,4 @@ Copyright Vitaly Valtman 2013 */ #define MAX_DC_NUM 9 -#define MAX_USER_NUM 100000 -#define MAX_CHAT_NUM 100000 +#define MAX_PEER_NUM 100000 diff --git a/tree.h b/tree.h index d5cdb73..2c86e1c 100644 --- a/tree.h +++ b/tree.h @@ -147,6 +147,8 @@ void tree_check_ ## X_NAME (struct tree_ ## X_NAME *T) { \ assert (T->right->y <= T->y);\ assert (X_CMP (T->right->x, T->x) > 0); \ }\ + tree_check_ ## X_NAME (T->left); \ + tree_check_ ## X_NAME (T->right); \ }\ #define int_cmp(a,b) ((a) - (b))