2018-08-30 14:55:42 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2021-09-05 01:36:38 +00:00
|
|
|
"context"
|
2018-10-23 12:38:10 +00:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2018-08-30 14:55:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2018-10-23 12:38:10 +00:00
|
|
|
jsonClient *JsonClient
|
|
|
|
extraGenerator ExtraGenerator
|
2022-01-04 12:22:29 +00:00
|
|
|
responses chan *Response
|
2018-10-23 12:38:10 +00:00
|
|
|
listenerStore *listenerStore
|
|
|
|
catchersStore *sync.Map
|
|
|
|
updatesTimeout time.Duration
|
|
|
|
catchTimeout time.Duration
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*Client)
|
|
|
|
|
|
|
|
func WithExtraGenerator(extraGenerator ExtraGenerator) Option {
|
2018-10-23 12:38:10 +00:00
|
|
|
return func(client *Client) {
|
|
|
|
client.extraGenerator = extraGenerator
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 03:08:28 +00:00
|
|
|
func WithCatchTimeout(timeout time.Duration) Option {
|
2018-10-23 12:38:10 +00:00
|
|
|
return func(client *Client) {
|
|
|
|
client.catchTimeout = timeout
|
|
|
|
}
|
2018-10-09 03:08:28 +00:00
|
|
|
}
|
|
|
|
|
2018-12-23 21:45:16 +00:00
|
|
|
func WithProxy(req *AddProxyRequest) Option {
|
|
|
|
return func(client *Client) {
|
|
|
|
client.AddProxy(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 05:41:34 +00:00
|
|
|
func SetLogVerbosityLevel(level int32) Option {
|
2019-09-10 12:36:54 +00:00
|
|
|
return func(client *Client) {
|
2022-01-27 05:41:34 +00:00
|
|
|
client.SetLogVerbosityLevel(&SetLogVerbosityLevelRequest{
|
|
|
|
NewVerbosityLevel: level,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetFilePath(path string) Option {
|
|
|
|
return func(client *Client) {
|
|
|
|
client.SetLogStream(&SetLogStreamRequest{
|
|
|
|
LogStream: &LogStreamFile{
|
|
|
|
Path: path,
|
|
|
|
MaxFileSize: 10485760,
|
|
|
|
RedirectStderr: true,
|
|
|
|
},
|
|
|
|
})
|
2019-09-10 12:36:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 14:55:42 +00:00
|
|
|
func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...Option) (*Client, error) {
|
2018-10-23 12:38:10 +00:00
|
|
|
client := &Client{
|
|
|
|
jsonClient: NewJsonClient(),
|
2022-01-04 12:22:29 +00:00
|
|
|
responses: make(chan *Response, 1000),
|
2018-10-23 12:38:10 +00:00
|
|
|
listenerStore: newListenerStore(),
|
|
|
|
catchersStore: &sync.Map{},
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-12-23 21:45:16 +00:00
|
|
|
client.extraGenerator = UuidV4Generator()
|
|
|
|
client.catchTimeout = 60 * time.Second
|
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
for _, option := range options {
|
|
|
|
option(client)
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2022-01-04 12:22:29 +00:00
|
|
|
tdlibInstance.addClient(client)
|
|
|
|
|
|
|
|
go client.receiver()
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
err := Authorize(client, authorizationStateHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
return client, nil
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 12:22:29 +00:00
|
|
|
func (client *Client) receiver() {
|
|
|
|
for response := range client.responses {
|
|
|
|
if response.Extra != "" {
|
|
|
|
value, ok := client.catchersStore.Load(response.Extra)
|
|
|
|
if ok {
|
|
|
|
value.(chan *Response) <- response
|
|
|
|
}
|
2021-12-31 19:03:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 12:22:29 +00:00
|
|
|
typ, err := UnmarshalType(response.Data)
|
2018-10-23 12:38:10 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
needGc := false
|
|
|
|
for _, listener := range client.listenerStore.Listeners() {
|
|
|
|
if listener.IsActive() {
|
|
|
|
listener.Updates <- typ
|
|
|
|
} else {
|
|
|
|
needGc = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if needGc {
|
|
|
|
client.listenerStore.gc()
|
|
|
|
}
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) Send(req Request) (*Response, error) {
|
2018-10-23 12:38:10 +00:00
|
|
|
req.Extra = client.extraGenerator()
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
catcher := make(chan *Response, 1)
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
client.catchersStore.Store(req.Extra, catcher)
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
defer func() {
|
|
|
|
client.catchersStore.Delete(req.Extra)
|
2021-09-05 01:36:38 +00:00
|
|
|
close(catcher)
|
2018-10-23 12:38:10 +00:00
|
|
|
}()
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
client.jsonClient.Send(req)
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2021-09-05 01:36:38 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), client.catchTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
select {
|
|
|
|
case response := <-catcher:
|
|
|
|
return response, nil
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2021-09-05 01:36:38 +00:00
|
|
|
case <-ctx.Done():
|
2018-10-23 12:38:10 +00:00
|
|
|
return nil, errors.New("response catching timeout")
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
2018-09-10 22:30:14 +00:00
|
|
|
|
|
|
|
func (client *Client) GetListener() *Listener {
|
2018-10-23 12:38:10 +00:00
|
|
|
listener := &Listener{
|
|
|
|
isActive: true,
|
|
|
|
Updates: make(chan Type, 1000),
|
|
|
|
}
|
|
|
|
client.listenerStore.Add(listener)
|
2018-09-10 22:30:14 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
return listener
|
2018-09-10 22:30:14 +00:00
|
|
|
}
|
2018-10-24 14:58:48 +00:00
|
|
|
|
|
|
|
func (client *Client) Stop() {
|
|
|
|
client.Destroy()
|
|
|
|
}
|