go-tdlib/client/client.go

180 lines
3.4 KiB
Go
Raw Normal View History

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"
2021-12-31 19:03:50 +00:00
"log"
2018-10-23 12:38:10 +00:00
"sync"
2021-12-31 19:03:50 +00:00
"sync/atomic"
2018-10-23 12:38:10 +00:00
"time"
2018-08-30 14:55:42 +00:00
)
2021-12-31 19:03:50 +00:00
var clients = &sync.Map{}
2018-08-30 14:55:42 +00:00
type Client struct {
2018-10-23 12:38:10 +00:00
jsonClient *JsonClient
extraGenerator ExtraGenerator
catcher chan *Response
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
}
func WithUpdatesTimeout(timeout time.Duration) Option {
2018-10-23 12:38:10 +00:00
return func(client *Client) {
client.updatesTimeout = 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)
}
}
2019-09-10 12:36:54 +00:00
func WithLogVerbosity(req *SetLogVerbosityLevelRequest) Option {
return func(client *Client) {
client.SetLogVerbosityLevel(req)
}
}
2018-08-30 14:55:42 +00:00
func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...Option) (*Client, error) {
2018-10-23 12:38:10 +00:00
catchersListener := make(chan *Response, 1000)
2018-08-30 14:55:42 +00:00
2018-10-23 12:38:10 +00:00
client := &Client{
jsonClient: NewJsonClient(),
catcher: catchersListener,
listenerStore: newListenerStore(),
catchersStore: &sync.Map{},
}
2018-08-30 14:55:42 +00:00
2021-12-31 19:03:50 +00:00
clients.Store(client.jsonClient.id, client)
2018-12-23 21:45:16 +00:00
client.extraGenerator = UuidV4Generator()
client.catchTimeout = 60 * time.Second
client.updatesTimeout = 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
2021-12-31 19:03:50 +00:00
go receive(client)
2018-10-23 12:38:10 +00:00
go client.catch(catchersListener)
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
}
2021-12-31 19:03:50 +00:00
var alreadyRunning uint32
func receive(client *Client) {
if atomic.LoadUint32(&alreadyRunning) != 0 {
return
}
atomic.StoreUint32(&alreadyRunning, 1)
2018-10-23 12:38:10 +00:00
for {
2021-12-31 19:03:50 +00:00
resp, err := Receive(client.updatesTimeout)
2018-10-23 12:38:10 +00:00
if err != nil {
continue
}
2021-12-31 19:03:50 +00:00
value, ok := clients.Load(resp.ClientId)
if !ok {
log.Printf("Response with wrong clientId: %d", resp.ClientId)
continue
}
client := value.(*Client)
2018-10-23 12:38:10 +00:00
client.catcher <- resp
typ, err := UnmarshalType(resp.Data)
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) catch(updates chan *Response) {
2018-10-23 12:38:10 +00:00
for update := range updates {
if update.Extra != "" {
value, ok := client.catchersStore.Load(update.Extra)
if ok {
value.(chan *Response) <- update
}
}
}
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()
}