Moving session code into its own file
parent
7a4d66bcf8
commit
d22b4a893c
@ -0,0 +1,66 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Session struct {
|
||||||
|
c net.Conn
|
||||||
|
ctx context.Context
|
||||||
|
p Printer
|
||||||
|
l *slog.Logger
|
||||||
|
|
||||||
|
userID uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSession(ctx context.Context, c net.Conn, p Printer, l *slog.Logger) *Session {
|
||||||
|
sid := uuid.New()
|
||||||
|
return &Session{
|
||||||
|
ctx: ctx,
|
||||||
|
l: l.With(slog.String("sid", sid.String())),
|
||||||
|
c: c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue