-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
64 lines (53 loc) · 1.2 KB
/
session.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
package pool
import (
"sync"
"github.com/KoNekoD/td/crypto"
"github.com/KoNekoD/td/mtproto"
)
// Session represents DC session.
type Session struct {
DC int
AuthKey crypto.AuthKey
Salt int64
}
// SyncSession is synchronization helper for Session.
type SyncSession struct {
data Session
mux sync.RWMutex
}
// NewSyncSession creates new SyncSession.
func NewSyncSession(data Session) *SyncSession {
return &SyncSession{
data: data,
}
}
// Store saves given Session.
func (s *SyncSession) Store(data Session) {
s.mux.Lock()
s.data = data
s.mux.Unlock()
}
// Migrate changes current DC and its addr, zeroes AuthKey and Salt.
func (s *SyncSession) Migrate(dc int) {
s.mux.Lock()
s.data.DC = dc
s.data.AuthKey = crypto.AuthKey{}
s.data.Salt = 0
s.mux.Unlock()
}
// Options fills Key and Salt field of given Options using stored session and returns it.
func (s *SyncSession) Options(opts mtproto.Options) (mtproto.Options, Session) {
s.mux.RLock()
data := s.data
s.mux.RUnlock()
opts.Key = data.AuthKey
opts.Salt = data.Salt
return opts, data
}
// Load gets session and returns it.
func (s *SyncSession) Load() (data Session) {
s.mux.RLock()
data = s.data
s.mux.RUnlock()
return
}