Many fixes.

This commit is contained in:
Vysheng 2013-12-02 21:19:08 +04:00
parent b1b4cd8b21
commit 8a39f039b0
8 changed files with 93 additions and 39 deletions

View File

@ -1033,8 +1033,13 @@ void replay_log_event (void) {
message_remove_tree (M); message_remove_tree (M);
message_del_peer (M); message_del_peer (M);
M->id = *(rptr ++); M->id = *(rptr ++);
message_insert_tree (M); if (message_get (M->id)) {
message_add_peer (M); free_message (M);
free (M);
} else {
message_insert_tree (M);
message_add_peer (M);
}
} }
break; break;
case CODE_update_user_photo: case CODE_update_user_photo:

31
main.c
View File

@ -86,24 +86,33 @@ void set_default_username (const char *s) {
default_username = strdup (s); default_username = strdup (s);
} }
/* {{{ TERMINAL */ /* {{{ TERMINAL */
tcflag_t old_lflag; static struct termios term_in, term_out;
cc_t old_vtime; static int term_set_in;
struct termios term; static int term_set_out;
void get_terminal_attributes (void) { void get_terminal_attributes (void) {
if (tcgetattr (STDIN_FILENO, &term) < 0) { if (tcgetattr (STDIN_FILENO, &term_in) < 0) {
perror ("tcgetattr()"); } else {
exit (EXIT_FAILURE); 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) { void set_terminal_attributes (void) {
if (tcsetattr (STDIN_FILENO, 0, &term) < 0) { if (term_set_in) {
perror ("tcsetattr()"); if (tcsetattr (STDIN_FILENO, 0, &term_in) < 0) {
exit (EXIT_FAILURE); perror ("tcsetattr()");
}
}
if (term_set_out) {
if (tcsetattr (STDOUT_FILENO, 0, &term_out) < 0) {
perror ("tcsetattr()");
}
} }
} }
/* }}} */ /* }}} */

View File

@ -1284,8 +1284,8 @@ void work_updates (struct connection *c, long long msg_id) {
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
fetch_alloc_chat (); fetch_alloc_chat ();
} }
fetch_date (); // date bl_do_set_date (fetch_int ());
fetch_seq (); // seq bl_do_set_seq (fetch_int ());
} }
void work_update_short_message (struct connection *c UU, long long msg_id UU) { void work_update_short_message (struct connection *c UU, long long msg_id UU) {

2
net.c
View File

@ -411,7 +411,7 @@ void hexdump_buf (struct connection_buffer *b) {
void try_rpc_read (struct connection *c) { void try_rpc_read (struct connection *c) {
assert (c->in_head); assert (c->in_head);
if (verbosity >= 1) { if (verbosity >= 3) {
hexdump_buf (c->in_head); hexdump_buf (c->in_head);
} }

View File

@ -52,19 +52,27 @@ struct tree_message *message_unsent_tree;
int users_allocated; int users_allocated;
int chats_allocated; int chats_allocated;
int messages_allocated; int messages_allocated;
int our_id;
int verbosity;
peer_t *Peers[MAX_USER_NUM];
int peer_num; int peer_num;
int encr_chats_allocated; int encr_chats_allocated;
int geo_chats_allocated; int geo_chats_allocated;
int our_id;
int verbosity;
peer_t *Peers[MAX_PEER_NUM];
extern int binlog_enabled; extern int binlog_enabled;
void fetch_skip_photo (void); 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_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 fetch_file_location (struct file_location *loc) {
int x = fetch_int (); int x = fetch_int ();
@ -84,18 +92,6 @@ int fetch_file_location (struct file_location *loc) {
return 0; 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) { int fetch_user_status (struct user_status *S) {
unsigned x = fetch_int (); unsigned x = fetch_int ();
code_assert (x == CODE_user_status_empty || x == CODE_user_status_online || x == CODE_user_status_offline); 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; 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) { char *create_print_name (peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4) {
const char *d[4]; const char *d[4];
d[0] = a1; d[1] = a2; d[2] = a3; d[3] = a4; 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); return strdup (s);
} }
/*
*
* Fetch with log event
*
*/
long long fetch_user_photo (struct user *U) { long long fetch_user_photo (struct user *U) {
unsigned x = fetch_int (); unsigned x = fetch_int ();
code_assert (x == CODE_user_profile_photo || x == CODE_user_profile_photo_old || x == CODE_user_profile_photo_empty); 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 big;
struct file_location small; struct file_location small;
if (fetch_file_location (&small) < 0) { return -1; } code_try (fetch_file_location (&small));
if (fetch_file_location (&big) < 0) { return -1; } code_try (fetch_file_location (&big));
bl_do_set_user_profile_photo (U, photo_id, &big, &small); bl_do_set_user_profile_photo (U, photo_id, &big, &small);
return 0; return 0;
@ -183,9 +212,6 @@ int fetch_user (struct user *U) {
unsigned x = fetch_int (); 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); 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 ()); U->id = MK_USER (fetch_int ());
if ((U->flags & FLAG_CREATED) && x == CODE_user_empty) {
return 0;
}
if (x == CODE_user_empty) { if (x == CODE_user_empty) {
return 0; return 0;
} }
@ -204,8 +230,10 @@ int fetch_user (struct user *U) {
} }
if (new) { if (new) {
int l1 = prefetch_strlen (); int l1 = prefetch_strlen ();
code_assert (l1 >= 0);
char *s1 = fetch_str (l1); char *s1 = fetch_str (l1);
int l2 = prefetch_strlen (); int l2 = prefetch_strlen ();
code_assert (l2 >= 0);
char *s2 = fetch_str (l2); char *s2 = fetch_str (l2);
if (x == CODE_user_deleted && !(U->flags & FLAG_DELETED)) { if (x == CODE_user_deleted && !(U->flags & FLAG_DELETED)) {
@ -221,6 +249,7 @@ int fetch_user (struct user *U) {
char *phone = 0; char *phone = 0;
if (x != CODE_user_foreign) { if (x != CODE_user_foreign) {
phone_len = prefetch_strlen (); phone_len = prefetch_strlen ();
code_assert (phone_len >= 0);
phone = fetch_str (phone_len); 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); 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)); memset (U, 0, sizeof (*U));
U->id = MK_USER (data[1]); U->id = MK_USER (data[1]);
peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); peer_tree = tree_insert_peer (peer_tree, U, lrand48 ());
assert (peer_num < MAX_PEER_NUM);
Peers[peer_num ++] = U; Peers[peer_num ++] = U;
} }
fetch_user (&U->user); fetch_user (&U->user);
@ -1512,6 +1542,7 @@ struct secret_chat *fetch_alloc_encrypted_chat (void) {
U->id = MK_ENCR_CHAT (data[1]); U->id = MK_ENCR_CHAT (data[1]);
encr_chats_allocated ++; encr_chats_allocated ++;
peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); peer_tree = tree_insert_peer (peer_tree, U, lrand48 ());
assert (peer_num < MAX_PEER_NUM);
Peers[peer_num ++] = U; Peers[peer_num ++] = U;
} }
fetch_encrypted_chat (&U->encr_chat); 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) { void insert_encrypted_chat (peer_t *P) {
encr_chats_allocated ++; encr_chats_allocated ++;
peer_tree = tree_insert_peer (peer_tree, P, lrand48 ()); peer_tree = tree_insert_peer (peer_tree, P, lrand48 ());
assert (peer_num < MAX_PEER_NUM);
Peers[peer_num ++] = P; Peers[peer_num ++] = P;
} }
void insert_user (peer_t *P) { void insert_user (peer_t *P) {
users_allocated ++; users_allocated ++;
peer_tree = tree_insert_peer (peer_tree, P, lrand48 ()); peer_tree = tree_insert_peer (peer_tree, P, lrand48 ());
assert (peer_num < MAX_PEER_NUM);
Peers[peer_num ++] = P; Peers[peer_num ++] = P;
} }
void insert_chat (peer_t *P) { void insert_chat (peer_t *P) {
chats_allocated ++; chats_allocated ++;
peer_tree = tree_insert_peer (peer_tree, P, lrand48 ()); peer_tree = tree_insert_peer (peer_tree, P, lrand48 ());
assert (peer_num < MAX_PEER_NUM);
Peers[peer_num ++] = P; Peers[peer_num ++] = P;
} }
@ -1550,6 +1584,7 @@ struct user *fetch_alloc_user_full (void) {
U->id = MK_USER (data[2]); U->id = MK_USER (data[2]);
peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); peer_tree = tree_insert_peer (peer_tree, U, lrand48 ());
fetch_user_full (&U->user); fetch_user_full (&U->user);
assert (peer_num < MAX_PEER_NUM);
Peers[peer_num ++] = U; Peers[peer_num ++] = U;
return &U->user; return &U->user;
} }
@ -1707,6 +1742,7 @@ void message_add_peer (struct message *M) {
break; break;
} }
peer_tree = tree_insert_peer (peer_tree, P, lrand48 ()); peer_tree = tree_insert_peer (peer_tree, P, lrand48 ());
assert (peer_num < MAX_PEER_NUM);
Peers[peer_num ++] = P; Peers[peer_num ++] = P;
} }
if (!P->last) { if (!P->last) {
@ -1856,6 +1892,7 @@ struct chat *fetch_alloc_chat (void) {
memset (U, 0, sizeof (*U)); memset (U, 0, sizeof (*U));
U->id = MK_CHAT (data[1]); U->id = MK_CHAT (data[1]);
peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); peer_tree = tree_insert_peer (peer_tree, U, lrand48 ());
assert (peer_num < MAX_PEER_NUM);
Peers[peer_num ++] = U; Peers[peer_num ++] = U;
} }
fetch_chat (&U->chat); fetch_chat (&U->chat);
@ -1876,6 +1913,7 @@ struct chat *fetch_alloc_chat_full (void) {
U->id = MK_CHAT (data[2]); U->id = MK_CHAT (data[2]);
peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); peer_tree = tree_insert_peer (peer_tree, U, lrand48 ());
fetch_chat_full (&U->chat); fetch_chat_full (&U->chat);
assert (peer_num < MAX_PEER_NUM);
Peers[peer_num ++] = U; Peers[peer_num ++] = U;
return &U->chat; return &U->chat;
} }

View File

@ -371,6 +371,7 @@ void send_all_unsent (void);
void message_remove_tree (struct message *M); void message_remove_tree (struct message *M);
void message_add_peer (struct message *M); void message_add_peer (struct message *M);
void message_del_peer (struct message *M); void message_del_peer (struct message *M);
void free_message (struct message *M);
#define PEER_USER 1 #define PEER_USER 1
#define PEER_CHAT 2 #define PEER_CHAT 2

View File

@ -17,5 +17,4 @@
Copyright Vitaly Valtman 2013 Copyright Vitaly Valtman 2013
*/ */
#define MAX_DC_NUM 9 #define MAX_DC_NUM 9
#define MAX_USER_NUM 100000 #define MAX_PEER_NUM 100000
#define MAX_CHAT_NUM 100000

2
tree.h
View File

@ -147,6 +147,8 @@ void tree_check_ ## X_NAME (struct tree_ ## X_NAME *T) { \
assert (T->right->y <= T->y);\ assert (T->right->y <= T->y);\
assert (X_CMP (T->right->x, T->x) > 0); \ 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)) #define int_cmp(a,b) ((a) - (b))