migrate to new client api

This commit is contained in:
Aleksandr Zelenin 2021-04-05 06:04:54 +03:00
parent ec36320d03
commit 98e8cb003f
2 changed files with 12 additions and 14 deletions

View file

@ -150,5 +150,4 @@ func (client *Client) GetListener() *Listener {
func (client *Client) Stop() {
client.Destroy()
client.jsonClient.Destroy()
}

View file

@ -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 {