-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
create.go
60 lines (52 loc) · 1.21 KB
/
create.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
package manager
import (
"context"
"github.com/gotd/td/internal/mtproto"
"github.com/gotd/td/internal/tdsync"
"github.com/gotd/td/tg"
)
// SetupCallback is a optional setup connection callback.
type SetupCallback = func(ctx context.Context, invoker tg.Invoker) error
// ConnOptions is a Telegram client connection options.
type ConnOptions struct {
DC int
Device DeviceConfig
Handler Handler
Setup SetupCallback
}
// SetDefaults sets default values.
func (c *ConnOptions) SetDefaults() {
if c.DC == 0 {
c.DC = 2
}
c.Device.SetDefaults()
if c.Handler == nil {
c.Handler = NoopHandler{}
}
}
// CreateConn creates new connection.
func CreateConn(
create mtproto.Dialer,
mode ConnMode,
appID int,
opts mtproto.Options,
connOpts ConnOptions,
) *Conn {
connOpts.SetDefaults()
conn := &Conn{
mode: mode,
appID: appID,
device: connOpts.Device,
clock: opts.Clock,
handler: connOpts.Handler,
sessionInit: tdsync.NewReady(),
gotConfig: tdsync.NewReady(),
dead: tdsync.NewReady(),
setup: connOpts.Setup,
}
conn.log = opts.Logger
opts.Handler = conn
opts.Logger = conn.log.Named("mtproto")
conn.proto = mtproto.New(create, opts)
return conn
}