From 98e8cb003f97653dc31d52bab0cfa6b7d28b99b9 Mon Sep 17 00:00:00 2001 From: Aleksandr Zelenin Date: Mon, 5 Apr 2021 06:04:54 +0300 Subject: [PATCH] migrate to new client api --- client/client.go | 1 - client/tdlib.go | 25 ++++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/client/client.go b/client/client.go index 5ef18a7..12a9656 100644 --- a/client/client.go +++ b/client/client.go @@ -150,5 +150,4 @@ func (client *Client) GetListener() *Listener { func (client *Client) Stop() { client.Destroy() - client.jsonClient.Destroy() } diff --git a/client/tdlib.go b/client/tdlib.go index 9edce04..bbc12bb 100644 --- a/client/tdlib.go +++ b/client/tdlib.go @@ -16,12 +16,12 @@ import ( ) type JsonClient struct { - jsonClient unsafe.Pointer + id int } func NewJsonClient() *JsonClient { return &JsonClient{ - jsonClient: C.td_json_client_create(), + id: int(C.td_create_client_id()), } } @@ -32,7 +32,7 @@ func (jsonClient *JsonClient) Send(req Request) { query := C.CString(string(data)) defer C.free(unsafe.Pointer(query)) - C.td_json_client_send(jsonClient.jsonClient, query) + C.td_send(C.int(jsonClient.id), query) } // Receives incoming updates and request responses from the TDLib client. May be called from any thread, but @@ -40,7 +40,7 @@ func (jsonClient *JsonClient) Send(req Request) { // Returned pointer will be deallocated by TDLib during next call to td_json_client_receive or td_json_client_execute // in the same thread, so it can't be used after that. func (jsonClient *JsonClient) Receive(timeout time.Duration) (*Response, error) { - result := C.td_json_client_receive(jsonClient.jsonClient, C.double(float64(timeout)/float64(time.Second))) + result := C.td_receive(C.double(float64(timeout) / float64(time.Second))) if result == nil { return nil, errors.New("update receiving timeout") } @@ -54,6 +54,10 @@ func (jsonClient *JsonClient) Receive(timeout time.Duration) (*Response, error) return nil, err } + if resp.ClientId != jsonClient.id { + return nil, errors.New("wrong @client_id") + } + resp.Data = data return &resp, nil @@ -68,8 +72,7 @@ func (jsonClient *JsonClient) Execute(req Request) (*Response, error) { query := C.CString(string(data)) defer C.free(unsafe.Pointer(query)) - - result := C.td_json_client_execute(jsonClient.jsonClient, query) + result := C.td_execute(query) if result == nil { return nil, errors.New("request can't be parsed") } @@ -88,14 +91,10 @@ func (jsonClient *JsonClient) Execute(req Request) (*Response, error) { return &resp, nil } -// Destroys the TDLib client instance. After this is called the client instance shouldn't be used anymore. -func (jsonClient *JsonClient) Destroy() { - C.td_json_client_destroy(jsonClient.jsonClient) -} - type meta struct { - Type string `json:"@type"` - Extra string `json:"@extra"` + Type string `json:"@type"` + Extra string `json:"@extra"` + ClientId int `json:"@client_id"` } type Request struct {