forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.go
59 lines (53 loc) · 2 KB
/
listener.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
package server
import (
"fmt"
"github.com/df-mc/dragonfly/server/session"
"github.com/sandertv/gophertunnel/minecraft"
"io"
)
// Listener is a source for connections that may be listened on by a Server using Server.listen. Proxies can use this to
// provide players from a different source.
type Listener interface {
// Accept blocks until the next connection is established and returns it. An error is returned if the Listener was
// closed using Close.
Accept() (session.Conn, error)
// Disconnect disconnects a connection from the Listener with a reason.
Disconnect(conn session.Conn, reason string) error
io.Closer
}
// listenerFunc may be used to return a *minecraft.Listener using a Config. It
// is the standard listener used when UserConfig.Config() is called.
func (uc UserConfig) listenerFunc(conf Config) (Listener, error) {
cfg := minecraft.ListenConfig{
MaximumPlayers: conf.MaxPlayers,
StatusProvider: statusProvider{name: conf.Name},
AuthenticationDisabled: conf.AuthDisabled,
ResourcePacks: conf.Resources,
Biomes: biomes(),
TexturePacksRequired: conf.ResourcesRequired,
}
l, err := cfg.Listen("raknet", uc.Network.Address)
if err != nil {
return nil, fmt.Errorf("create minecraft listener: %w", err)
}
conf.Log.Infof("Server running on %v.\n", l.Addr())
return listener{l}, nil
}
// listener is a Listener implementation that wraps around a minecraft.Listener so that it can be listened on by
// Server.
type listener struct {
*minecraft.Listener
}
// Accept blocks until the next connection is established and returns it. An error is returned if the Listener was
// closed using Close.
func (l listener) Accept() (session.Conn, error) {
conn, err := l.Listener.Accept()
if err != nil {
return nil, err
}
return conn.(session.Conn), err
}
// Disconnect disconnects a connection from the Listener with a reason.
func (l listener) Disconnect(conn session.Conn, reason string) error {
return l.Listener.Disconnect(conn.(*minecraft.Conn), reason)
}