From 612fa53409ae24bf4b17e87a0c52e63658b59014 Mon Sep 17 00:00:00 2001 From: Trumeet Date: Tue, 12 Oct 2021 14:12:27 -0700 Subject: handle updateConnectionState --- easy-tg.c | 29 +++++++++++++++++++++++++++++ easy-tg.h | 7 +++++++ example.c | 3 +++ 3 files changed, 39 insertions(+) diff --git a/easy-tg.c b/easy-tg.c index baac4a8..ff2dd00 100644 --- a/easy-tg.c +++ b/easy-tg.c @@ -237,6 +237,33 @@ int tg_destroy() return 0; } +static int handle_update_connection_state(const json_object *json) +{ + int r = TG_IGNORE; + json_object *obj = NULL; + if(!json_object_object_get_ex(json, "state", &obj) || + !json_object_object_get_ex(obj, "@type", &obj)) + goto cleanup; + r = TG_CONNECTION; + tg_reg1 = json_object_get_string(obj); + if(!strcmp(tg_reg1, "connectionStateConnecting")) + tg_errno = TG_CONNECTION_CONNECTING; + else if(!strcmp(tg_reg1, "connectionStateConnectingToProxy")) + tg_errno = TG_CONNECTION_CONNECTING_PROXY; + else if(!strcmp(tg_reg1, "connectionStateReady")) + tg_errno = TG_CONNECTION_READY; + else if(!strcmp(tg_reg1, "connectionStateUpdating")) + tg_errno = TG_CONNECTION_UPDATING; + else if(!strcmp(tg_reg1, "connectionStateWaitingForNetwork")) + tg_errno = TG_CONNECTION_WAITING_NETWORK; + else + r = TG_RUN; + + goto cleanup; +cleanup: + return r; +} + int tg_loop() { if(tg_update != NULL) @@ -267,6 +294,8 @@ int tg_loop() r = handle_update_authorization_state(tg_update); else if(!strcmp(tg_update_type, "error")) r = handle_update_error(tg_update); + else if(!strcmp(tg_update_type, "updateConnectionState")) + r = handle_update_connection_state(tg_update); else r = TG_RUN; goto cleanup; diff --git a/easy-tg.h b/easy-tg.h index fd66314..64e2649 100644 --- a/easy-tg.h +++ b/easy-tg.h @@ -16,12 +16,19 @@ #define TG_PARAMS 11 /* Need to provide TDLib parameters. */ #define TG_ERROR 12 /* TDLib returns an error. td_errno = code, r1 = extra, r2 = msg. */ #define TG_SYSERR 13 /* EasyTD encounters an error. */ +#define TG_CONNECTION 14 /* Telegram connection state update. tg_errno = code, r1 = raw type */ #define TG_REQ_SET_PARAMS "set_params" #define TG_REQ_LOGIN_PHONE "login_phone" #define TG_REQ_LOGIN_CODE "login_code" #define TG_REQ_LOGIN_PASS "login_pass" +#define TG_CONNECTION_CONNECTING 1 +#define TG_CONNECTION_CONNECTING_PROXY 2 +#define TG_CONNECTION_READY 3 +#define TG_CONNECTION_UPDATING 4 +#define TG_CONNECTION_WAITING_NETWORK 5 + #include #include #include diff --git a/example.c b/example.c index 4a2fa1c..0797e6f 100644 --- a/example.c +++ b/example.c @@ -174,6 +174,9 @@ int main(int argc, char **argv) /* Error code is stored in tg_errno. */ printf("Fetal: %d\n", tg_errno); break; + case TG_CONNECTION: /* Telegram connection status is changed. */ + printf("[CONNECTION]: %s (%d)\n", tg_reg1, tg_errno); + break; default: printf("???\n"); break; -- cgit v1.2.3