-
Notifications
You must be signed in to change notification settings - Fork 3
/
manager.go
184 lines (146 loc) · 3.78 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package tdlib
import "C"
import (
"context"
"encoding/json"
"fmt"
"sync"
"unsafe"
"github.com/sirupsen/logrus"
"github.com/aliforever/go-tdlib/config"
)
// #cgo linux CFLAGS: -I/usr/local/include
// #cgo linux LDFLAGS: -Wl,-rpath=/usr/local/lib -ltdjson
// #include <stdlib.h>
// #include "callbacks.h"
import "C"
//export go_td_log_message_callback_ptr
func go_td_log_message_callback_ptr(verbosityLevel C.int, message *C.char) {
goMessage := C.GoString(message)
fmt.Printf("Received log message with verbosity level %d: %s\n", int(verbosityLevel), goMessage)
}
type Manager struct {
handlers *ManagerHandlers
clientUpdateChannels sync.Map
options *ManagerOptions
}
// NewManager creates a new Manager instance
func NewManager(ctx context.Context, handlers *ManagerHandlers, options *ManagerOptions) *Manager {
if options != nil {
C.set_log_message_callback(C.int(options.LogVerbosityLevel))
if options.DisableLogging {
cfgBytes, _ := json.Marshal(map[string]interface{}{
"@type": "setLogStream",
"log_stream": map[string]interface{}{
"@type": "logStreamEmpty",
},
})
query := C.CString(string(cfgBytes))
C.td_execute(query)
C.free(unsafe.Pointer(query))
} else if options.LogPath != "" {
cfgBytes, _ := json.Marshal(map[string]interface{}{
"@type": "setLogStream",
"log_stream": map[string]interface{}{
"@type": "logStreamFile",
"path": options.LogPath,
"max_file_size": 10485760,
},
})
query := C.CString(string(cfgBytes))
C.td_execute(query)
C.free(unsafe.Pointer(query))
}
}
manager := &Manager{
handlers: handlers,
options: options,
}
go manager.receiveUpdates(ctx)
return manager
}
// NewClient creates a new client instance
func (m *Manager) NewClient(
apiID int64,
apiHash string,
handlers *Handlers,
cfg *config.Config,
logger *logrus.Logger,
) *TDLib {
clientID := m.newClientID()
updateChannel := make(chan []byte)
m.clientUpdateChannels.Store(clientID, updateChannel)
return newClientV2(
clientID,
updateChannel,
apiID,
apiHash,
handlers,
cfg,
logger,
)
}
func (m *Manager) newClientID() int {
result := C.td_create_client_id()
return int(result)
}
func (m *Manager) receiveNextUpdate(timeout float64) []byte {
result := C.td_receive(C.double(timeout))
if result == nil {
return nil
}
return []byte(C.GoString(result))
}
func (m *Manager) receiveUpdates(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
// Continue receiving updates
}
updateBytes := m.receiveNextUpdate(10)
if updateBytes == nil || len(updateBytes) == 0 {
continue
}
if m.handlers != nil && m.handlers.onRawIncomingEvent != nil {
go m.handlers.onRawIncomingEvent(updateBytes)
}
clientID := m.getClientID(updateBytes)
if clientID != nil {
if clientChan, ok := m.getClientChannel(*clientID); ok {
go m.writeClientEvent(clientChan, updateBytes)
continue
} else {
// TODO: Log Received Update For Unknown Client
fmt.Printf("Received update for unknown client: %d\n", *clientID)
}
}
// TODO: Log General Received Update, Doesn't Belong To Any Client
fmt.Printf("Received update: %s\n", string(updateBytes))
}
}
func (m *Manager) getClientChannel(clientID int) (chan []byte, bool) {
clientValue, ok := m.clientUpdateChannels.Load(clientID)
if ok {
clientUpdateChannel, ok := clientValue.(chan []byte)
if ok {
return clientUpdateChannel, true
}
}
return nil, false
}
func (m *Manager) writeClientEvent(updateChan chan<- []byte, update []byte) {
updateChan <- update
}
func (m *Manager) getClientID(update []byte) *int {
type ClientID struct {
ID int `json:"@client_id"`
}
var clientID ClientID
err := json.Unmarshal(update, &clientID)
if err != nil {
return nil
}
return &clientID.ID
}