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
|
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)
}
}()
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, switching to fallback server.")
if sc.client().whyKicked == nil {
sc.client().whyKicked = &mt.ToCltKick{
Reason: mt.Custom,
Custom: "Server connection timed out.",
}
}
} else {
sc.client().SendChatMsg("Server connection lost, switching to fallback server.")
if sc.client().whyKicked == nil {
sc.client().whyKicked = &mt.ToCltKick{
Reason: mt.Custom,
Custom: "Server connection lost.",
}
}
}
sc.client().fallback()
break
}
break
}
sc.Log("<-", err)
continue
}
sc.process(pkt)
}
}
|