github.com/df-mc/dragonfly@v0.9.13/server/listener.go (about) 1 package server 2 3 import ( 4 "fmt" 5 "github.com/df-mc/dragonfly/server/session" 6 "github.com/sandertv/gophertunnel/minecraft" 7 "github.com/sirupsen/logrus" 8 "io" 9 "log" 10 ) 11 12 // Listener is a source for connections that may be listened on by a Server using Server.listen. Proxies can use this to 13 // provide players from a different source. 14 type Listener interface { 15 // Accept blocks until the next connection is established and returns it. An error is returned if the Listener was 16 // closed using Close. 17 Accept() (session.Conn, error) 18 // Disconnect disconnects a connection from the Listener with a reason. 19 Disconnect(conn session.Conn, reason string) error 20 io.Closer 21 } 22 23 // listenerFunc may be used to return a *minecraft.Listener using a Config. It 24 // is the standard listener used when UserConfig.Config() is called. 25 func (uc UserConfig) listenerFunc(conf Config) (Listener, error) { 26 cfg := minecraft.ListenConfig{ 27 MaximumPlayers: conf.MaxPlayers, 28 StatusProvider: statusProvider{name: conf.Name}, 29 AuthenticationDisabled: conf.AuthDisabled, 30 ResourcePacks: conf.Resources, 31 Biomes: biomes(), 32 TexturePacksRequired: conf.ResourcesRequired, 33 } 34 if l, ok := conf.Log.(*logrus.Logger); ok { 35 cfg.ErrorLog = log.Default() 36 log.SetOutput(l.WithField("src", "gophertunnel").WriterLevel(logrus.DebugLevel)) 37 } 38 l, err := cfg.Listen("raknet", uc.Network.Address) 39 if err != nil { 40 return nil, fmt.Errorf("create minecraft listener: %w", err) 41 } 42 conf.Log.Infof("Server running on %v.\n", l.Addr()) 43 return listener{l}, nil 44 } 45 46 // listener is a Listener implementation that wraps around a minecraft.Listener so that it can be listened on by 47 // Server. 48 type listener struct { 49 *minecraft.Listener 50 } 51 52 // Accept blocks until the next connection is established and returns it. An error is returned if the Listener was 53 // closed using Close. 54 func (l listener) Accept() (session.Conn, error) { 55 conn, err := l.Listener.Accept() 56 if err != nil { 57 return nil, err 58 } 59 return conn.(session.Conn), err 60 } 61 62 // Disconnect disconnects a connection from the Listener with a reason. 63 func (l listener) Disconnect(conn session.Conn, reason string) error { 64 return l.Listener.Disconnect(conn.(*minecraft.Conn), reason) 65 }