Handle the SWITCH_DC RPCError by changing the data center and creating a new mtproto_connection

This commit is contained in:
mjentsch 2014-08-22 18:54:25 +02:00
parent 5d8d170927
commit 8ff9734077
3 changed files with 30 additions and 4 deletions

View file

@ -1751,6 +1751,10 @@ int rpc_execute (struct connection *c, int op, int len) {
case st_authorized:
if (op < 0 && op >= -999) {
logprintf ("Server error %d\n", op);
char code[12] = {0};
snprintf (code, 12, "%d", op);
c->mtconnection->c_state = st_error;
telegram_change_state (instance, STATE_ERROR, code);
} else {
process_rpc_message (c, (void *)(Response/* + 8*/), Response_len/* - 12*/);
}

View file

@ -583,8 +583,10 @@ int check_phone_on_error (struct query *q UU, int error_code, int l, char *error
//check_phone_result = 1;
} else {
logprintf ( "error_code = %d, error = %.*s\n", error_code, l, error);
telegram_change_state(instance, STATE_ERROR, error);
}
telegram_change_state(instance, STATE_DISCONNECTED_SWITCH_DC, error);
telegram_change_state(instance,
STATE_DISCONNECTED_SWITCH_DC, &instance->auth.dc_working_num);
return 0;
}

View file

@ -73,6 +73,9 @@ void on_state_change(struct telegram *instance, int state, void *data)
err = "<no description>";
}
logprintf("telegram errored: %s\n", err);
// close the connection
mtproto_close (instance->connection);
}
break;
@ -108,9 +111,26 @@ void on_state_change(struct telegram *instance, int state, void *data)
// wait for user input ...
break;
case STATE_DISCONNECTED_SWITCH_DC:
logprintf("Have to migrate to other DC");
instance->connection
case STATE_DISCONNECTED_SWITCH_DC: {
// telegram demands that we use a different data center, which caused
// the current mtproto_connection to be disconnected
int target_dc = *(int*) data;
logprintf ("Disconnected: Migrate to data center %d\n", target_dc);
// close old connection and mark it for destruction
mtproto_close (instance->connection);
if (instance->proxy_request_cb) {
// tell the proxy to close all connections
instance->proxy_close_cb (instance,
instance->connection->connection->fd);
}
// start a new connection to the demanded data center. The pointer to the
// currently working dc should have already been updated by the
// on_error function of the query
telegram_network_connect (instance);
}
break;
}
}