Skip to content

Commit

Permalink
work with updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vvaltman committed Apr 23, 2015
1 parent 243dfe5 commit e6af41a
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 82 deletions.
21 changes: 15 additions & 6 deletions queries.c
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,8 @@ static int msg_send_on_answer (struct tgl_state *TLS, struct query *q, void *D)
UPD->pts = talloc (4);
*UPD->pts = DS_LVAL (DS_MSM->pts);

tglu_work_update_new (TLS, UPD);
tglu_work_update_new (TLS, 1, UPD);
tglu_work_update_new (TLS, 0, UPD);
free_ds_type_update (UPD, TYPE_TO_PARAM (update));

M = tgl_message_get (TLS, y);
Expand Down Expand Up @@ -1193,7 +1194,8 @@ static int send_file_part_on_answer (struct tgl_state *TLS, struct query *q, voi
}

static int send_file_on_answer (struct tgl_state *TLS, struct query *q, void *D) {
tglu_work_any_updates_new (TLS, D);
tglu_work_any_updates_new (TLS, 1, D);
tglu_work_any_updates_new (TLS, 0, D);

if (q->callback) {
((void (*)(struct tgl_state *, void *, int, struct tgl_message *))q->callback)(TLS, q->callback_extra, 1, NULL);
Expand Down Expand Up @@ -1604,7 +1606,8 @@ void tgl_do_contact_search (struct tgl_state *TLS, char *name, int limit, void (

/* {{{ Forward */
static int fwd_msg_on_answer (struct tgl_state *TLS, struct query *q, void *D) {
tglu_work_any_updates_new (TLS, D);
tglu_work_any_updates_new (TLS, 1, D);
tglu_work_any_updates_new (TLS, 0, D);

if (q->callback) {
((void (*)(struct tgl_state *, void *, int, struct tgl_message *))q->callback) (TLS, q->callback_extra, 1, q->extra);
Expand Down Expand Up @@ -1788,7 +1791,8 @@ void tgl_do_send_location (struct tgl_state *TLS, tgl_peer_id_t id, double latit

/* {{{ Rename chat */
static int rename_chat_on_answer (struct tgl_state *TLS, struct query *q, void *D) {
tglu_work_any_updates_new (TLS, D);
tglu_work_any_updates_new (TLS, 1, D);
tglu_work_any_updates_new (TLS, 0, D);

if (q->callback) {
((void (*)(struct tgl_state *, void *, int, struct tgl_message *))q->callback) (TLS, q->callback_extra, 1, NULL);
Expand Down Expand Up @@ -2848,7 +2852,11 @@ static int get_difference_on_answer (struct tgl_state *TLS, struct query *q, voi
}

for (i = 0; i < DS_LVAL (DS_UD->other_updates->cnt); i++) {
tglu_work_update_new (TLS, DS_UD->other_updates->data[i]);
tglu_work_update_new (TLS, 1, DS_UD->other_updates->data[i]);
}

for (i = 0; i < DS_LVAL (DS_UD->other_updates->cnt); i++) {
tglu_work_update_new (TLS, -1, DS_UD->other_updates->data[i]);
}

for (i = 0; i < ml_pos; i++) {
Expand Down Expand Up @@ -3591,7 +3599,8 @@ void tgl_do_check_password (struct tgl_state *TLS, void (*callback)(struct tgl_s
}

static int send_broadcast_on_answer (struct tgl_state *TLS, struct query *q, void *D) {
tglu_work_any_updates_new (TLS, D);
tglu_work_any_updates_new (TLS, 1, D);
tglu_work_any_updates_new (TLS, 0, D);

if (q->callback) {
((void (*)(struct tgl_state *, void *, int, int, struct tgl_message **))q->callback)(TLS, q->callback_extra, 1, 0, q->extra);
Expand Down
1 change: 0 additions & 1 deletion structures.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,6 @@ void tglf_fetch_message_action_new (struct tgl_state *TLS, struct tgl_message_ac
{
M->type = tgl_message_action_geo_chat_create;
assert (0);
//vlogprintf (E_ERROR, "Message action: Created geochat %.*s in address %.*s. You are in magic land now, since nobody ever tested geochats in this app\n", DS_STR (DS_MA->title), DS_STR (DS_MA->address));
}
break;
case CODE_message_action_geo_chat_checkin:
Expand Down
183 changes: 123 additions & 60 deletions updates.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,72 @@ static void fetch_dc_option_new (struct tgl_state *TLS, struct tl_ds_dc_option *

int tgl_check_pts_diff (struct tgl_state *TLS, int pts, int pts_count) {
vlogprintf (E_ERROR, "pts = %d, pts_count = %d\n", pts, pts_count);
if (!pts_count) { return 0; }
if (TLS->pts) {
if (pts < TLS->pts + pts_count) {
vlogprintf (E_NOTICE, "Duplicate message with pts=%d\n", pts);
assert (TLS->pts);
if (pts < TLS->pts + pts_count) {
vlogprintf (E_NOTICE, "Duplicate message with pts=%d\n", pts);
return -1;
}
if (pts > TLS->pts + pts_count) {
vlogprintf (E_NOTICE, "Hole in pts (pts = %d, count = %d, cur_pts = %d)\n", pts, pts_count, TLS->pts);
tgl_do_get_difference (TLS, 0, 0, 0);
return -1;
}
if (TLS->locks & TGL_LOCK_DIFF) {
vlogprintf (E_DEBUG, "Update during get_difference. pts = %d\n", pts);
return -1;
}
vlogprintf (E_DEBUG, "Ok update. pts = %d\n", pts);
return 1;
}

int tgl_check_qts_diff (struct tgl_state *TLS, int qts, int qts_count) {
vlogprintf (E_ERROR, "qts = %d, qts_count = %d\n", qts, qts_count);
assert (TLS->qts);
if (qts < TLS->qts + qts_count) {
vlogprintf (E_NOTICE, "Duplicate message with qts=%d\n", qts);
return -1;
}
if (qts > TLS->qts + qts_count) {
vlogprintf (E_NOTICE, "Hole in qts (qts = %d, count = %d, cur_qts = %d)\n", qts, qts_count, TLS->qts);
tgl_do_get_difference (TLS, 0, 0, 0);
return -1;
}
if (TLS->locks & TGL_LOCK_DIFF) {
vlogprintf (E_DEBUG, "Update during get_difference. qts = %d\n", qts);
return -1;
}
vlogprintf (E_DEBUG, "Ok update. qts = %d\n", qts);
return 1;
}

static int do_skip_seq (struct tgl_state *TLS, int seq) {
if (!seq) {
vlogprintf (E_DEBUG, "Ok update. seq = %d\n", seq);
return 0;
}
if (TLS->seq) {
if (seq <= TLS->seq) {
vlogprintf (E_NOTICE, "Duplicate message with seq=%d\n", seq);
return -1;
}
if (pts > TLS->pts + pts_count) {
vlogprintf (E_NOTICE, "Hole in pts (pts = %d, count = %d, cur_pts = %d)\n", pts, pts_count, TLS->pts);
if (seq > TLS->seq + 1) {
vlogprintf (E_NOTICE, "Hole in seq (seq = %d, cur_seq = %d)\n", seq, TLS->seq);
//vlogprintf (E_NOTICE, "lock_diff = %s\n", (TLS->locks & TGL_LOCK_DIFF) ? "true" : "false");
tgl_do_get_difference (TLS, 0, 0, 0);
return -1;
}
if (TLS->locks & TGL_LOCK_DIFF) {
vlogprintf (E_DEBUG, "Update during get_difference. pts = %d\n", pts);
vlogprintf (E_DEBUG, "Update during get_difference. seq = %d\n", seq);
return -1;
}
vlogprintf (E_DEBUG, "Ok update. pts = %d\n", pts);
return 1;
vlogprintf (E_DEBUG, "Ok update. seq = %d\n", seq);
return 0;
} else {
return -1;
}
}

void tglu_work_update_new (struct tgl_state *TLS, struct tl_ds_update *DS_U) {
void tglu_work_update_new (struct tgl_state *TLS, int check_only, struct tl_ds_update *DS_U) {
if (TLS->locks & TGL_LOCK_DIFF) {
vlogprintf (E_DEBUG, "Update during get_difference. DROP\n");
return;
Expand All @@ -70,11 +113,18 @@ void tglu_work_update_new (struct tgl_state *TLS, struct tl_ds_update *DS_U) {
if (DS_U->pts) {
assert (DS_U->pts_count);

if (tgl_check_pts_diff (TLS, DS_LVAL (DS_U->pts), DS_LVAL (DS_U->pts_count)) <= 0) {
if (!check_only && tgl_check_pts_diff (TLS, DS_LVAL (DS_U->pts), DS_LVAL (DS_U->pts_count)) <= 0) {
return;
}
}

if (DS_U->pts) {
if (!check_only && tgl_check_qts_diff (TLS, DS_LVAL (DS_U->qts), 1) <= 0) {
return;
}
}

if (check_only > 0 && DS_U->magic != CODE_update_message_i_d) { return; }
switch (DS_U->magic) {
case CODE_update_new_message:
{
Expand Down Expand Up @@ -381,42 +431,21 @@ void tglu_work_update_new (struct tgl_state *TLS, struct tl_ds_update *DS_U) {
assert (0);
}

if (check_only) { return; }

if (DS_U->pts) {
assert (DS_U->pts_count);

bl_do_set_pts (TLS, DS_LVAL (DS_U->pts));
}
}

static int do_skip_seq (struct tgl_state *TLS, int seq) {
if (TLS->seq) {
if (!seq) {
vlogprintf (E_DEBUG, "Ok update. seq = %d\n", seq);
return 0;
}
if (seq <= TLS->seq) {
vlogprintf (E_NOTICE, "Duplicate message with seq=%d\n", seq);
return -1;
}
if (seq > TLS->seq + 1) {
vlogprintf (E_NOTICE, "Hole in seq (seq = %d, cur_seq = %d)\n", seq, TLS->seq);
//vlogprintf (E_NOTICE, "lock_diff = %s\n", (TLS->locks & TGL_LOCK_DIFF) ? "true" : "false");
tgl_do_get_difference (TLS, 0, 0, 0);
return -1;
}
if (TLS->locks & TGL_LOCK_DIFF) {
vlogprintf (E_DEBUG, "Update during get_difference. seq = %d\n", seq);
return -1;
}
vlogprintf (E_DEBUG, "Ok update. seq = %d\n", seq);
return 0;
} else {
return -1;

void tglu_work_updates_new (struct tgl_state *TLS, int check_only, struct tl_ds_updates *DS_U) {
if (TLS->locks & TGL_LOCK_DIFF) {
return;
}
}

void tglu_work_updates_new (struct tgl_state *TLS, struct tl_ds_updates *DS_U) {
if (do_skip_seq (TLS, DS_LVAL (DS_U->seq)) < 0) {
if (!check_only && do_skip_seq (TLS, DS_LVAL (DS_U->seq)) < 0) {
return;
}
int i;
Expand All @@ -427,14 +456,20 @@ void tglu_work_updates_new (struct tgl_state *TLS, struct tl_ds_updates *DS_U) {
tglf_fetch_alloc_chat_new (TLS, DS_U->chats->data[i]);
}
for (i = 0; i < DS_LVAL (DS_U->updates->cnt); i++) {
tglu_work_update_new (TLS, DS_U->updates->data[i]);
tglu_work_update_new (TLS, check_only, DS_U->updates->data[i]);
}

if (check_only) { return; }
bl_do_set_date (TLS, DS_LVAL (DS_U->date));
bl_do_set_seq (TLS, DS_LVAL (DS_U->seq));
}

void tglu_work_updates_combined_new (struct tgl_state *TLS, struct tl_ds_updates *DS_U) {
if (do_skip_seq (TLS, DS_LVAL (DS_U->seq_start)) < 0) {
void tglu_work_updates_combined_new (struct tgl_state *TLS, int check_only, struct tl_ds_updates *DS_U) {
if (TLS->locks & TGL_LOCK_DIFF) {
return;
}

if (!check_only && do_skip_seq (TLS, DS_LVAL (DS_U->seq_start)) < 0) {
return;
}

Expand All @@ -446,65 +481,92 @@ void tglu_work_updates_combined_new (struct tgl_state *TLS, struct tl_ds_updates
tglf_fetch_alloc_chat_new (TLS, DS_U->chats->data[i]);
}
for (i = 0; i < DS_LVAL (DS_U->updates->cnt); i++) {
tglu_work_update_new (TLS, DS_U->updates->data[i]);
tglu_work_update_new (TLS, check_only, DS_U->updates->data[i]);
}

if (check_only) { return; }
bl_do_set_date (TLS, DS_LVAL (DS_U->date));
bl_do_set_seq (TLS, DS_LVAL (DS_U->seq));
}

void tglu_work_update_short_message_new (struct tgl_state *TLS, struct tl_ds_updates *DS_U) {
if (tgl_check_pts_diff (TLS, DS_LVAL (DS_U->pts), DS_LVAL (DS_U->pts_count)) <= 0) {
void tglu_work_update_short_message_new (struct tgl_state *TLS, int check_only, struct tl_ds_updates *DS_U) {
if (TLS->locks & TGL_LOCK_DIFF) {
return;
}

if (!check_only && tgl_check_pts_diff (TLS, DS_LVAL (DS_U->pts), DS_LVAL (DS_U->pts_count)) <= 0) {
return;
}

if (check_only > 0) { return; }

struct tgl_message *M = tglf_fetch_alloc_message_short_new (TLS, DS_U);
assert (M);

bl_do_msg_update (TLS, M->id);

if (check_only) { return; }
bl_do_set_pts (TLS, DS_LVAL (DS_U->pts));
}

void tglu_work_update_short_chat_message_new (struct tgl_state *TLS, struct tl_ds_updates *DS_U) {
if (tgl_check_pts_diff (TLS, DS_LVAL (DS_U->pts), DS_LVAL (DS_U->pts_count)) <= 0) {
void tglu_work_update_short_chat_message_new (struct tgl_state *TLS, int check_only, struct tl_ds_updates *DS_U) {
if (TLS->locks & TGL_LOCK_DIFF) {
return;
}

if (!check_only && tgl_check_pts_diff (TLS, DS_LVAL (DS_U->pts), DS_LVAL (DS_U->pts_count)) <= 0) {
return;
}

if (check_only > 0) { return; }

struct tgl_message *M = tglf_fetch_alloc_message_short_chat_new (TLS, DS_U);
assert (M);

bl_do_msg_update (TLS, M->id);

if (check_only) { return; }
bl_do_set_pts (TLS, DS_LVAL (DS_U->pts));
}

void tglu_work_updates_to_long_new (struct tgl_state *TLS, struct tl_ds_updates *DS_U) {
void tglu_work_updates_to_long_new (struct tgl_state *TLS, int check_only, struct tl_ds_updates *DS_U) {
if (TLS->locks & TGL_LOCK_DIFF) {
return;
}
vlogprintf (E_NOTICE, "updates too long... Getting difference\n");
if (check_only) { return; }
tgl_do_get_difference (TLS, 0, 0, 0);
}

void tglu_work_update_short_new (struct tgl_state *TLS, struct tl_ds_updates *DS_U) {
tglu_work_update_new (TLS, DS_U->update);
if (!(TLS->locks & TGL_LOCK_DIFF)) {
bl_do_set_date (TLS, DS_LVAL (DS_U->date));
void tglu_work_update_short_new (struct tgl_state *TLS, int check_only, struct tl_ds_updates *DS_U) {
if (TLS->locks & TGL_LOCK_DIFF) {
return;
}
tglu_work_update_new (TLS, check_only, DS_U->update);
}

void tglu_work_any_updates_new (struct tgl_state *TLS, struct tl_ds_updates *DS_U) {
void tglu_work_any_updates_new (struct tgl_state *TLS, int check_only, struct tl_ds_updates *DS_U) {
if (TLS->locks & TGL_LOCK_DIFF) {
return;
}
switch (DS_U->magic) {
case CODE_updates_too_long:
tglu_work_updates_to_long_new (TLS, DS_U);
tglu_work_updates_to_long_new (TLS, check_only, DS_U);
return;
case CODE_update_short_message:
tglu_work_update_short_message_new (TLS, DS_U);
tglu_work_update_short_message_new (TLS, check_only, DS_U);
return;
case CODE_update_short_chat_message:
tglu_work_update_short_chat_message_new (TLS, DS_U);
tglu_work_update_short_chat_message_new (TLS, check_only, DS_U);
return;
case CODE_update_short:
tglu_work_update_short_new (TLS, DS_U);
tglu_work_update_short_new (TLS, check_only, DS_U);
return;
case CODE_updates_combined:
tglu_work_updates_combined_new (TLS, DS_U);
tglu_work_updates_combined_new (TLS, check_only, DS_U);
return;
case CODE_updates:
tglu_work_updates_new (TLS, DS_U);
tglu_work_updates_new (TLS, check_only, DS_U);
return;
default:
assert (0);
Expand All @@ -514,7 +576,8 @@ void tglu_work_any_updates_new (struct tgl_state *TLS, struct tl_ds_updates *DS_
void tglu_work_any_updates (struct tgl_state *TLS) {
struct tl_ds_updates *DS_U = fetch_ds_type_updates (TYPE_TO_PARAM (updates));
assert (DS_U);
tglu_work_any_updates_new (TLS, DS_U);
tglu_work_any_updates_new (TLS, 1, DS_U);
tglu_work_any_updates_new (TLS, 0, DS_U);
free_ds_type_updates (DS_U, TYPE_TO_PARAM (updates));
}

Expand Down
18 changes: 3 additions & 15 deletions updates.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,13 @@
#define __UPDATES_H__
struct tl_ds_updates;
struct tl_ds_update;
void tglu_work_update (struct tgl_state *TLS);
void tglu_work_update_new (struct tgl_state *TLS, struct tl_ds_update *DS_U);
void tglu_work_updates_to_long (struct tgl_state *TLS);
void tglu_work_update_short_chat_message (struct tgl_state *TLS);
void tglu_work_update_short_message (struct tgl_state *TLS);
void tglu_work_update_short (struct tgl_state *TLS);
void tglu_work_updates (struct tgl_state *TLS);

void tglu_fetch_pts (struct tgl_state *TLS);
void tglu_fetch_qts (struct tgl_state *TLS);
void tglu_fetch_seq (struct tgl_state *TLS);
void tglu_fetch_date (struct tgl_state *TLS);

void tgl_insert_status_update (struct tgl_state *TLS, struct tgl_user *U);
void tgl_insert_status_expire (struct tgl_state *TLS, struct tgl_user *U);
void tgl_remove_status_expire (struct tgl_state *TLS, struct tgl_user *U);

int tgl_check_pts_diff (struct tgl_state *TLS, int pts, int pts_count);
void tglu_work_updates_new (struct tgl_state *TLS, struct tl_ds_updates *DS_U);
void tglu_work_update_new (struct tgl_state *TLS, int check_only, struct tl_ds_update *DS_U);
void tglu_work_updates_new (struct tgl_state *TLS, int check_only, struct tl_ds_updates *DS_U);
void tglu_work_any_updates (struct tgl_state *TLS);
void tglu_work_any_updates_new (struct tgl_state *TLS, struct tl_ds_updates *DS_U);
void tglu_work_any_updates_new (struct tgl_state *TLS, int check_only, struct tl_ds_updates *DS_U);
#endif

0 comments on commit e6af41a

Please sign in to comment.