-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
conn_builder.go
95 lines (81 loc) · 1.91 KB
/
conn_builder.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
package telegram
import (
"context"
"go.uber.org/zap"
"github.com/gotd/td/bin"
"github.com/gotd/td/internal/mtproto"
"github.com/gotd/td/internal/pool"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/telegram/internal/manager"
"github.com/gotd/td/tg"
"github.com/gotd/td/transport"
)
type clientHandler struct {
client *Client
}
func (c clientHandler) OnSession(cfg tg.Config, s mtproto.Session) error {
return c.client.onSession(cfg, s)
}
func (c clientHandler) OnMessage(b *bin.Buffer) error {
return c.client.handleUpdates(b)
}
func (c *Client) asHandler() manager.Handler {
return clientHandler{
client: c,
}
}
type connConstructor func(
create mtproto.Dialer,
mode manager.ConnMode,
appID int,
opts mtproto.Options,
connOpts manager.ConnOptions,
) pool.Conn
func defaultConstructor() connConstructor {
return func(
create mtproto.Dialer,
mode manager.ConnMode,
appID int,
opts mtproto.Options,
connOpts manager.ConnOptions,
) pool.Conn {
return manager.CreateConn(create, mode, appID, opts, connOpts)
}
}
func (c *Client) dcList() dcs.List {
cfg := c.cfg.Load()
return dcs.List{
Options: cfg.DCOptions,
Domains: c.domains,
Test: c.testDC,
}
}
func (c *Client) primaryDC(dc int) mtproto.Dialer {
return func(ctx context.Context) (transport.Conn, error) {
return c.resolver.Primary(ctx, dc, c.dcList())
}
}
func (c *Client) createPrimaryConn(setup manager.SetupCallback) pool.Conn {
return c.createConn(0, c.defaultMode, setup)
}
func (c *Client) createConn(
id int64,
mode manager.ConnMode,
setup manager.SetupCallback,
) pool.Conn {
opts, s := c.session.Options(c.opts)
opts.Logger = c.log.Named("conn").With(
zap.Int64("conn_id", id),
zap.Int("dc_id", s.DC),
)
return c.create(
c.primaryDC(s.DC), mode, c.appID,
opts, manager.ConnOptions{
DC: s.DC,
Test: c.testDC,
Device: c.device,
Handler: c.asHandler(),
Setup: setup,
},
)
}