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
|
package proxy
import (
"errors"
"log"
"net"
"sync"
"time"
"github.com/HimbeerserverDE/mt"
"github.com/HimbeerserverDE/mt/rudp"
)
// A ServerConn is a connection to a minetest server.
type ServerConn struct {
mt.Peer
clt *ClientConn
mu sync.RWMutex
logger *log.Logger
cstate clientState
cstateMu sync.RWMutex
name string
initCh chan struct{}
auth struct {
method mt.AuthMethods
salt, srpA, a, srpK []byte
}
mediaPool string
dynMedia map[string]struct {
token uint32
cache bool
}
inv mt.Inv
detachedInvs []string
aos map[mt.AOID]struct{}
particleSpawners map[mt.ParticleSpawnerID]struct{}
sounds map[mt.SoundID]struct{}
huds map[mt.HUDID]mt.HUDType
playerList map[string]struct{}
}
func (sc *ServerConn) client() *ClientConn {
sc.mu.RLock()
defer sc.mu.RUnlock()
return sc.clt
}
func (sc *ServerConn) state() clientState {
sc.cstateMu.RLock()
defer sc.cstateMu.RUnlock()
return sc.cstate
}
func (sc *ServerConn) setState(state clientState) {
sc.cstateMu.Lock()
defer sc.cstateMu.Unlock()
sc.cstate = state
}
// Init returns a channel that is closed
// when the ServerConn enters the csActive state.
func (sc *ServerConn) Init() <-chan struct{} { return sc.initCh }
// Log logs an interaction with the ServerConn.
// dir indicates the direction of the interaction.
func (sc *ServerConn) Log(dir string, v ...interface{}) {
sc.logger.Println(append([]interface{}{dir}, v...)...)
}
func handleSrv(sc *ServerConn) {
go func() {
init := make(chan struct{})
defer close(init)
go func(init <-chan struct{}) {
select {
case <-init:
case <-time.After(10 * time.Second):
sc.Log("->", "timeout")
sc.Close()
}
}(init)
for sc.state() == csCreated && sc.client() != nil {
sc.SendCmd(&mt.ToSrvInit{
SerializeVer: serializeVer,
MinProtoVer: protoVer,
MaxProtoVer: protoVer,
PlayerName: sc.client().Name(),
})
time.Sleep(500 * time.Millisecond)
}
}()
RecvLoop:
for {
pkt, err := sc.Recv()
if err != nil {
if errors.Is(err, net.ErrClosed) {
if errors.Is(sc.WhyClosed(), rudp.ErrTimedOut) {
sc.Log("<->", "timeout")
} else {
sc.Log("<->", "disconnect")
}
if sc.client() != nil {
if errors.Is(sc.WhyClosed(), rudp.ErrTimedOut) {
sc.client().SendChatMsg("Server connection timed out, triggering fallback.")
} else {
sc.client().SendChatMsg("Server connection lost, triggering fallback.")
}
for _, srvName := range FallbackServers(sc.name) {
if err := sc.client().HopRaw(srvName); err != nil {
sc.client().Log("<-", err)
sc.client().SendChatMsg("Could not connect to "+srvName+", continuing fallback. Error:", err.Error())
}
break RecvLoop
}
ack, _ := sc.client().SendCmd(&mt.ToCltKick{
Reason: mt.Custom,
Custom: "Server connection closed unexpectedly.",
})
select {
case <-sc.client().Closed():
case <-ack:
sc.client().Close()
sc.client().mu.Lock()
sc.client().srv = nil
sc.client().mu.Unlock()
sc.mu.Lock()
sc.clt = nil
sc.mu.Unlock()
}
}
break
}
sc.Log("<-", err)
continue
}
sc.process(pkt)
}
}
|