-
Notifications
You must be signed in to change notification settings - Fork 3
/
listener.go
52 lines (44 loc) · 1.19 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
package spectrum
import (
"github.com/cooldogedev/spectrum-df/internal"
tr "github.com/cooldogedev/spectrum-df/transport"
"github.com/cooldogedev/spectrum-df/util"
"github.com/df-mc/dragonfly/server/session"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
)
type Listener struct {
authentication util.Authentication
transport tr.Transport
}
func NewListener(addr string, authentication util.Authentication, transport tr.Transport) (*Listener, error) {
if transport == nil {
transport = tr.NewSpectral()
}
if err := transport.Listen(addr); err != nil {
return nil, err
}
return &Listener{
authentication: authentication,
transport: transport,
}, nil
}
// Accept ...
func (l *Listener) Accept() (session.Conn, error) {
c, err := l.transport.Accept()
if err != nil {
return nil, err
}
var authenticator internal.Authenticator
if l.authentication != nil {
authenticator = l.authentication.Authenticate
}
return internal.NewConn(c, authenticator, packet.NewClientPool())
}
// Disconnect ...
func (l *Listener) Disconnect(conn session.Conn, _ string) error {
return conn.Close()
}
// Close ...
func (l *Listener) Close() error {
return l.transport.Close()
}