diff --git a/mtproto-client.c b/mtproto-client.c index 741c205..f2c267c 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -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*/); } diff --git a/queries.c b/queries.c index 57b6c7f..811a169 100644 --- a/queries.c +++ b/queries.c @@ -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; } diff --git a/telegram.c b/telegram.c index 9c06e28..cea7a32 100755 --- a/telegram.c +++ b/telegram.c @@ -73,6 +73,9 @@ void on_state_change(struct telegram *instance, int state, void *data) err = ""; } 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; } }