This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
connect.go
101 lines (88 loc) · 2.66 KB
/
connect.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
package client
import (
"sync/atomic"
)
// connect will try to establish a connection to the configured webwire server
// and try to automatically restore the session if there is any.
// If the session restoration fails connect won't fail, instead it will reset the current session
// and return normally.
// Before establishing the connection - connect verifies protocol compatibility and returns an
// error if the protocol implemented by the server doesn't match the required protocol version
// of this client instance.
func (clt *Client) connect() error {
clt.connectLock.Lock()
defer clt.connectLock.Unlock()
if atomic.LoadInt32(&clt.status) == StatConnected {
return nil
}
if err := clt.verifyProtocolVersion(); err != nil {
return err
}
if err := clt.conn.Dial(clt.serverAddr); err != nil {
return err
}
// Setup reader thread
go func() {
defer func() {
// Set status
atomic.StoreInt32(&clt.status, StatDisconnected)
select {
case clt.readerClosing <- true:
default:
}
}()
for {
message, err := clt.conn.Read()
if err != nil {
if err.IsAbnormalCloseErr() {
// Error while reading message
clt.errorLog.Print("Abnormal closure error:", err)
}
atomic.StoreInt32(&clt.status, StatDisconnected)
// Call hook
clt.impl.OnDisconnected()
// Try to reconnect if autoconn wasn't disabled.
// reconnect in another goroutine to let this one die
// and free up the socket
go func() {
if atomic.LoadInt32(&clt.autoconnect) == autoconnectEnabled {
if err := clt.tryAutoconnect(0); err != nil {
clt.errorLog.Printf("Auto-reconnect failed after connection loss: %s", err)
return
}
}
}()
return
}
// Try to handle the message
if err := clt.handleMessage(message); err != nil {
clt.warningLog.Print("Failed handling message:", err)
}
}
}()
atomic.StoreInt32(&clt.status, StatConnected)
// Read the current sessions key if there is any
clt.sessionLock.RLock()
if clt.session == nil {
clt.sessionLock.RUnlock()
return nil
}
sessionKey := clt.session.Key
clt.sessionLock.RUnlock()
// Try to restore session if necessary
restoredSession, err := clt.requestSessionRestoration([]byte(sessionKey))
if err != nil {
// Just log a warning and still return nil, even if session restoration failed,
// because we only care about the connection establishment in this method
clt.warningLog.Printf("Couldn't restore session on reconnection: %s", err)
// Reset the session
clt.sessionLock.Lock()
clt.session = nil
clt.sessionLock.Unlock()
return nil
}
clt.sessionLock.Lock()
clt.session = restoredSession
clt.sessionLock.Unlock()
return nil
}