You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.3 KiB
Go
76 lines
1.3 KiB
Go
package core
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"net"
|
|
|
|
"github.com/EngoEngine/ecs"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Session struct {
|
|
c net.Conn
|
|
ctx context.Context
|
|
p Printer
|
|
l *slog.Logger
|
|
|
|
w *ecs.World
|
|
|
|
sessionID uuid.UUID
|
|
userID uint64
|
|
}
|
|
|
|
func NewSession(ctx context.Context, c net.Conn, p Printer, l *slog.Logger, w *ecs.World) *Session {
|
|
sid := uuid.New()
|
|
return &Session{
|
|
ctx: ctx,
|
|
|
|
l: l.With(slog.String("sid", sid.String())),
|
|
c: c,
|
|
w: w,
|
|
p: p,
|
|
|
|
sessionID: sid,
|
|
}
|
|
}
|
|
|
|
func (s *Session) bufferInput(commands chan string) {
|
|
scanner := bufio.NewScanner(s.c)
|
|
defer close(commands)
|
|
|
|
for scanner.Scan() {
|
|
err := scanner.Err()
|
|
switch err {
|
|
case nil:
|
|
commands <- scanner.Text()
|
|
default:
|
|
s.l.With(slog.Any("error", scanner.Err())).Error("error scanning input")
|
|
return
|
|
}
|
|
}
|
|
|
|
s.l.Debug("session terminated")
|
|
}
|
|
|
|
func (s *Session) Start() {
|
|
s.l.Debug("starting telnet session")
|
|
fmt.Fprint(s.c, "Welcome to Bing Bong.\n")
|
|
|
|
commands := make(chan string)
|
|
go s.bufferInput(commands)
|
|
|
|
for command := range commands {
|
|
s.l.With(slog.String("command", command)).Debug("command recieved")
|
|
_, err := fmt.Fprintf(s.c, "> %q\n", command)
|
|
if err != nil {
|
|
s.l.With(
|
|
slog.Any("error", err),
|
|
).Error("connection closed on write")
|
|
return
|
|
}
|
|
}
|
|
}
|