add doorHandler back in

This commit is contained in:
Sundog Jones 2021-04-14 15:27:45 -07:00
parent 1f3c0331e7
commit 2a295b43b9

79
main.go
View File

@ -1,10 +1,12 @@
package main
import (
"bufio"
"context"
"flag"
"log"
"net"
"os/exec"
"strconv"
"strings"
"sync"
@ -183,14 +185,15 @@ func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) {
case "adventure":
// start the door and wait
log.Printf("starting simulated door handler...")
MenuControl <- "pause"
// if err := doorHandler(w); err != nil {
// log.Printf("error from cmd `adventure`: %v", err)
// }
time.Sleep(5 * time.Second)
log.Printf("returning from door")
MenuControl <- "unpause"
log.Println("starting door handler...")
var wg sync.WaitGroup
wg.Add(1)
go doorHandler(ctx, conn, w, &wg)
log.Println("menu handler waiting for wg to return from door")
wg.Wait()
log.Println("returning from door")
//MenuControl <- "unpause"
err := w.Write(ctx, conn, []byte("\n\n> "))
if err != nil {
log.Printf("error writing to connection: %v", err)
@ -198,12 +201,12 @@ func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) {
case "exit":
if err := w.Write(ctx, conn, exitHandler()); err != nil {
log.Printf("error sending exitHandler from cmd `exit`: %v", err)
log.Printf("error sending exitHandler from cmd `exit`: %v\n", err)
} else {
// let's wait before we actually close the connection to try and ensure the write gets through
time.Sleep(time.Second)
log.Println("exiting...")
log.Printf("Goodbye %s!", conn.RemoteAddr())
log.Printf("Goodbye %s!\n", conn.RemoteAddr())
err := w.Free(conn)
if err != nil {
log.Printf("err closing connection: %v\n", err)
@ -299,25 +302,33 @@ func exitHandler() []byte {
return []byte(exitMessage)
}
// rework to mimic mainHandler
/*
func doorHandler(w *gaio.Watcher) error {
func doorHandler(ctx context.Context, c net.Conn, w *gaio.Watcher, menuwg *sync.WaitGroup) error {
defer menuwg.Done()
const bannerText = "\r\nCOLOSSAL CAVE\r\n\r\n"
err := sendClientText(*c, bannerText)
err := w.Write(ctx, c, []byte(bannerText))
if err != nil {
log.Printf("error writing to connection: %v\n", err)
}
var inChan, _ chan gaio.OpResult
var ok bool
if inChan, _, ok = openconn.FromContext(ctx); !ok {
log.Println("Could not get inChan/outChan from context!", ok, ctx)
}
cmd := exec.Command("/usr/games/adventure")
stdout, _ := cmd.StdoutPipe()
stdin, _ := cmd.StdinPipe()
cmd.Start()
Terminator := make(chan bool)
wg := new(sync.WaitGroup)
var wg sync.WaitGroup
wg.Add(1)
go func(c net.Conn, Terminator *chan bool, wg *sync.WaitGroup) {
Terminator := make(chan bool)
go func(Terminator *chan bool, wg *sync.WaitGroup) {
log.Println("starting stdin reader goroutine")
defer wg.Done()
Check:
@ -325,27 +336,21 @@ func doorHandler(w *gaio.Watcher) error {
select {
case <-*Terminator:
break Check
default:
time.Sleep(time.Second)
inBuf := make([]byte, 80)
var l int
if l, err = c.Read(inBuf); err != nil {
if err == io.EOF {
log.Println("connection closed?")
break Check
} else {
log.Printf("error reading conn: %v\n", err)
break Check
case result, ok := <-inChan:
if ok {
if result.Operation == gaio.OpRead {
// we asked for a read - send it to stdin
stdin.Write(result.Buffer[:result.Size])
}
} else {
log.Println("yikes, problem getting a result from inChan in doorHandler!")
}
if l > 0 {
log.Printf("writing %d bytes to stdin: %v\n", l, string(inBuf[:l]))
stdin.Write(inBuf[:l])
}
default:
time.Sleep(time.Millisecond)
}
}
log.Println("exiting stdin reader goroutine")
}(*c, &Terminator, wg)
}(&Terminator, &wg)
log.Println("starting stdout reader goroutine")
scanner := bufio.NewScanner(stdout)
@ -353,9 +358,9 @@ func doorHandler(w *gaio.Watcher) error {
for scanner.Scan() {
m := scanner.Bytes()
(*c).Write(m)
(*c).Write([]byte{'\n'})
log.Printf("wrote %d bytes to stdout: %v\n", len(m)+1, string(m))
m = append(m, '\n')
w.Write(ctx, c, m)
log.Printf("wrote %d bytes to watcher from door stdout: %v\n", len(m)+1, string(m))
}
log.Println("exiting stdout reader goroutine")
Terminator <- true
@ -367,6 +372,6 @@ func doorHandler(w *gaio.Watcher) error {
if exitErr != nil {
log.Printf("cmd exited with err: %v\n", exitErr)
}
log.Println("leaving door handler")
return nil
}
*/