Commit 22c37ffe authored by vvaltman's avatar vvaltman

Merge github.com:vysheng/tg

parents e1255b0d 7f75a004
......@@ -3,16 +3,23 @@
typedef int evutil_socket_t;
static inline struct event *event_new (struct event_base *base, int fd, int what, void(*callback)(int, short, void *), void *arg) __attribute__ ((unused));
static inline struct event *event_new (struct event_base *base, int fd, int what, void(*callback)(int, short, void *), void *arg) {
struct event *ev = malloc (sizeof (*ec));
event_set (ev, base, fd, what, callback, arg);
struct event *ev = malloc (sizeof (*ev));
event_set (ev, fd, what, callback, arg);
event_base_set (base, ev);
return ev;
}
static inline struct event *evtimer_new (struct event_base *base, void(*callback)(int, short, void *), void *arg) __attribute__ ((unused));
static inline struct event *evtimer_new (struct event_base *base, void(*callback)(int, short, void *), void *arg) {
struct event *ev = malloc (sizeof (*ec));
evtimer_set (ev, base, callback, arg);
struct event *ev = malloc (sizeof (*ev));
event_set (ev, -1, 0, callback, arg);
event_base_set (base, ev);
return ev;
}
static void event_free (struct event *ev) __attribute__ ((unused));
static void event_free (struct event *ev) {
event_del (ev);
free (ev);
......
......@@ -20,6 +20,7 @@
Copyright 2012-2013 Vkontakte Ltd
2012-2013 Vitaliy Valtman
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <signal.h>
......
......@@ -73,11 +73,11 @@ static void ping_alarm (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg;
vlogprintf (E_DEBUG + 2,"ping alarm\n");
assert (c->state == conn_ready || c->state == conn_connecting);
if (tglt_get_double_time () - c->last_receive_time > 20 * PING_TIMEOUT) {
if (tglt_get_double_time () - c->last_receive_time > 6 * PING_TIMEOUT) {
vlogprintf (E_WARNING, "fail connection: reason: ping timeout\n");
c->state = conn_failed;
fail_connection (c);
} else if (tglt_get_double_time () - c->last_receive_time > 5 * PING_TIMEOUT && c->state == conn_ready) {
} else if (tglt_get_double_time () - c->last_receive_time > 3 * PING_TIMEOUT && c->state == conn_ready) {
tgl_do_send_ping (c);
start_ping_timer (c);
} else {
......@@ -365,6 +365,12 @@ static void restart_connection (struct connection *c) {
start_ping_timer (c);
Connections[fd] = c;
c->write_ev = event_new (tgl_state.ev_base, c->fd, EV_WRITE, conn_try_write, c);
struct timeval tv = {5, 0};
c->read_ev = event_new (tgl_state.ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c);
event_add (c->read_ev, &tv);
char byte = 0xef;
assert (tgln_write_out (c, &byte, 1) == 1);
tgln_flush_out (c);
......@@ -374,7 +380,9 @@ static void fail_connection (struct connection *c) {
if (c->state == conn_ready || c->state == conn_connecting) {
stop_ping_timer (c);
}
event_del (c->write_ev);
event_free (c->write_ev);
event_free (c->read_ev);
rotate_port (c);
struct connection_buffer *b = c->out_head;
while (b) {
......@@ -403,15 +411,6 @@ static void try_write (struct connection *c) {
int x = 0;
while (c->out_head) {
int r = write (c->fd, c->out_head->rptr, c->out_head->wptr - c->out_head->rptr);
/*if (r > 0 && log_net_f) {
fprintf (log_net_f, "%.02lf %d OUT %s:%d", get_utime (CLOCK_REALTIME), r, c->ip, c->port);
int i;
for (i = 0; i < r; i++) {
fprintf (log_net_f, " %02x", *(unsigned char *)(c->out_head->rptr + i));
}
fprintf (log_net_f, "\n");
fflush (log_net_f);
}*/
if (r >= 0) {
x += r;
c->out_head->rptr += r;
......@@ -438,30 +437,6 @@ static void try_write (struct connection *c) {
c->out_bytes -= x;
}
/*static void hexdump_buf (struct connection_buffer *b) {
int pos = 0;
int rem = 8;
while (b) {
unsigned char *c = b->rptr;
while (c != b->wptr) {
if (rem == 8) {
if (pos) { printf ("\n"); }
printf ("%04d", pos);
}
printf (" %02x", (int)*c);
rem --;
pos ++;
if (!rem) {
rem = 8;
}
c ++;
}
b = b->next;
}
printf ("\n");
}*/
static void try_rpc_read (struct connection *c) {
assert (c->in_head);
......@@ -502,18 +477,13 @@ static void try_read (struct connection *c) {
if (!c->in_tail) {
c->in_head = c->in_tail = new_connection_buffer (1 << 20);
}
#ifdef EVENT_V1
struct timeval tv = {5, 0};
event_add (c->read_ev, &tv);
#endif
int x = 0;
while (1) {
int r = read (c->fd, c->in_tail->wptr, c->in_tail->end - c->in_tail->wptr);
/*if (r > 0 && log_net_f) {
fprintf (log_net_f, "%.02lf %d IN %s:%d", get_utime (CLOCK_REALTIME), r, c->ip, c->port);
int i;
for (i = 0; i < r; i++) {
fprintf (log_net_f, " %02x", *(unsigned char *)(c->in_tail->wptr + i));
}
fprintf (log_net_f, "\n");
fflush (log_net_f);
}*/
if (r > 0) {
c->last_receive_time = tglt_get_double_time ();
stop_ping_timer (c);
......
......@@ -826,7 +826,9 @@ static int msg_send_on_answer (struct query *q UU) {
assert (x == CODE_messages_sent_message || x == CODE_messages_sent_message_link);
int id = fetch_int (); // id
struct tgl_message *M = q->extra;
if (M->id != id) {
bl_do_set_msg_id (M, id);
}
int date = fetch_int ();
int pts = fetch_int ();
//tglu_fetch_seq ();
......@@ -837,9 +839,11 @@ static int msg_send_on_answer (struct query *q UU) {
bl_do_set_pts (pts);
bl_do_msg_seq_update (id);
} else {
if (seq > tgl_state.seq + 1) {
vlogprintf (E_NOTICE, "Hole in seq\n");
tgl_do_get_difference (0, 0, 0);
}
}
if (x == CODE_messages_sent_message_link) {
assert (skip_type_any (TYPE_TO_PARAM_1 (vector, TYPE_TO_PARAM (contacts_link))) >= 0);
}
......@@ -1066,8 +1070,11 @@ static int mark_read_on_receive (struct query *q UU) {
bl_do_set_pts (pts);
bl_do_set_seq (seq);
} else {
if (seq > tgl_state.seq + 1) {
vlogprintf (E_NOTICE, "Hole in seq\n");
tgl_do_get_difference (0, 0, 0);
}
}
int offset = fetch_int (); // offset
int *t = q->extra;
......@@ -1394,9 +1401,11 @@ static int send_file_on_answer (struct query *q UU) {
bl_do_set_pts (pts);
bl_do_msg_seq_update (M->id);
} else {
if (seq > tgl_state.seq + 1) {
vlogprintf (E_NOTICE, "Hole in seq\n");
tgl_do_get_difference (0, 0, 0);
}
}
if (q->callback) {
((void (*)(void *, int, struct tgl_message *))q->callback)(q->callback_extra, 1, M);
......@@ -1814,9 +1823,11 @@ static int fwd_msg_on_answer (struct query *q UU) {
bl_do_set_pts (pts);
bl_do_msg_seq_update (M->id);
} else {
if (seq > tgl_state.seq + 1) {
vlogprintf (E_NOTICE, "Hole in seq\n");
tgl_do_get_difference (0, 0, 0);
}
}
//print_message (M);
if (q->callback) {
((void (*)(void *, int, struct tgl_message *))q->callback) (q->callback_extra, 1, M);
......@@ -1952,9 +1963,11 @@ static int rename_chat_on_answer (struct query *q UU) {
bl_do_set_pts (pts);
bl_do_msg_seq_update (M->id);
} else {
if (seq > tgl_state.seq + 1) {
vlogprintf (E_NOTICE, "Hole in seq\n");
tgl_do_get_difference (0, 0, 0);
}
}
//print_message (M);
if (q->callback) {
((void (*)(void *, int, struct tgl_message *))q->callback) (q->callback_extra, 1, M);
......
......@@ -28,8 +28,8 @@
#define TG_SERVER_TEST "173.240.5.253"
// JUST RANDOM STRING
#define TGL_BUILD "1934"
#define TGL_VERSION "1.0.1"
#define TGL_BUILD "2012"
#define TGL_VERSION "1.0.2"
#define TGL_ENCRYPTED_LAYER 16
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment