2021-04-14 08:51:34 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-04-14 18:27:45 -04:00
|
|
|
"bufio"
|
2021-04-14 17:58:02 -04:00
|
|
|
"context"
|
2021-04-14 08:51:34 -04:00
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net"
|
2021-04-14 18:27:45 -04:00
|
|
|
"os/exec"
|
2021-04-14 08:51:34 -04:00
|
|
|
"strconv"
|
2021-04-14 11:25:41 -04:00
|
|
|
"strings"
|
2021-04-14 08:51:34 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/xtaci/gaio"
|
2021-04-14 17:58:02 -04:00
|
|
|
"versestudios.com/go-telnet-asyncio-test/openconn"
|
2021-04-14 08:51:34 -04:00
|
|
|
)
|
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
type InChanKey string
|
|
|
|
type OutChanKey string
|
|
|
|
|
|
|
|
func ioHandler(w *gaio.Watcher, WatcherControl *chan string) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-*WatcherControl:
|
|
|
|
switch msg {
|
|
|
|
case "":
|
|
|
|
log.Println("empty WatcherControl command")
|
|
|
|
case "stop":
|
|
|
|
log.Println("stopping ioHandler via WatcherControl stop command")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
log.Println("unknown WatcherControl command:", msg)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// loop wait for any IO events
|
|
|
|
results, err := w.WaitIO()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
IOLoop:
|
|
|
|
for _, res := range results {
|
|
|
|
if res.Context != nil && nil != res.Context.(context.Context) {
|
|
|
|
if inChan, _, ok := openconn.FromContext(res.Context.(context.Context)); ok {
|
|
|
|
switch res.Operation {
|
|
|
|
case gaio.OpRead: // read completion event
|
|
|
|
inChan <- res
|
|
|
|
// queue next read
|
|
|
|
w.Read(res.Context, res.Conn, nil)
|
|
|
|
default: // anything else (meaning write completions)
|
|
|
|
continue IOLoop
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("error getting inChan and outChan from context: %v\n", res.Context)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("nil context! res: %v\n", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 08:51:34 -04:00
|
|
|
func main() {
|
|
|
|
port := flag.Int("port", 3333, "Port to accept connections on.")
|
|
|
|
host := flag.String("host", "127.0.0.1", "Host or IP to bind to")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
w, err := gaio.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
l, err := net.Listen("tcp", *host+":"+strconv.Itoa(*port))
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
log.Println("Listening to connections at '"+*host+"' on port", strconv.Itoa(*port))
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
2021-04-14 11:25:41 -04:00
|
|
|
log.Println("new client: ", conn.RemoteAddr())
|
2021-04-14 17:58:02 -04:00
|
|
|
// chan to terminate ioHandler when neccessary
|
|
|
|
WatcherControl := make(chan string)
|
|
|
|
|
|
|
|
// get new context with inChan and outChan for this client connection
|
|
|
|
ctx := openconn.NewContext(context.Background())
|
|
|
|
log.Printf("new context for client %v: %v\n", conn.RemoteAddr(), ctx)
|
2021-04-14 08:51:34 -04:00
|
|
|
|
|
|
|
// submit the first async write IO request
|
2021-04-14 17:58:02 -04:00
|
|
|
err = w.Write(ctx, conn, welcomeHandler())
|
2021-04-14 08:51:34 -04:00
|
|
|
if err != nil {
|
2021-04-14 11:25:41 -04:00
|
|
|
log.Printf("err sending welcomeHandler: %v\n", err)
|
2021-04-14 08:51:34 -04:00
|
|
|
return
|
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
// now that a prompt is (or will be) displayed, go ahead and listen for input
|
|
|
|
err = w.Read(ctx, conn, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("err queueing w.Read: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2021-04-14 11:25:41 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
time.Sleep(time.Millisecond)
|
2021-04-14 11:25:41 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
// menu handler for this connection
|
|
|
|
go menuHandler(ctx, conn, w)
|
|
|
|
// io handler for this connection
|
|
|
|
go ioHandler(w, &WatcherControl)
|
2021-04-14 08:51:34 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
log.Println("main thread looping")
|
|
|
|
// wg.Wait()
|
|
|
|
// log.Printf("Goodbye %s!", conn.RemoteAddr())
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
2021-04-14 11:25:41 -04:00
|
|
|
}
|
2021-04-14 08:51:34 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
MenuControl := make(chan string)
|
2021-04-14 08:51:34 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
var inChan, outChan chan gaio.OpResult
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
if inChan, outChan, ok = openconn.FromContext(ctx); !ok {
|
|
|
|
log.Println("Could not get inChan/outChan from context!", ok, ctx)
|
|
|
|
}
|
2021-04-14 11:25:41 -04:00
|
|
|
|
2021-04-14 08:51:34 -04:00
|
|
|
wg.Add(1)
|
2021-04-14 17:58:02 -04:00
|
|
|
go func(inChan, outChan chan gaio.OpResult, MenuControl chan string) {
|
2021-04-14 08:51:34 -04:00
|
|
|
defer wg.Done()
|
2021-04-14 17:58:02 -04:00
|
|
|
log.Println("starting menu loop")
|
|
|
|
paused := false
|
2021-04-14 11:25:41 -04:00
|
|
|
ControlLoop:
|
|
|
|
for {
|
2021-04-14 17:58:02 -04:00
|
|
|
// log.Println("checking watcher waitio")
|
2021-04-14 11:25:41 -04:00
|
|
|
select {
|
2021-04-14 17:58:02 -04:00
|
|
|
case msg := <-MenuControl:
|
|
|
|
log.Println("msg on MenuControl:", msg)
|
|
|
|
switch string(msg) {
|
|
|
|
|
|
|
|
case "stop":
|
|
|
|
log.Println("closing menu loop")
|
|
|
|
paused = true
|
2021-04-14 11:25:41 -04:00
|
|
|
break ControlLoop
|
2021-04-14 17:58:02 -04:00
|
|
|
|
|
|
|
case "pause":
|
|
|
|
log.Println("pausing menu loop via menucontrol")
|
|
|
|
paused = true
|
|
|
|
|
|
|
|
case "unpause":
|
|
|
|
log.Println("unpausing menu loop via menucontrol")
|
|
|
|
paused = false
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Println("menu received unknown command: ", msg)
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
|
2021-04-14 08:51:34 -04:00
|
|
|
default:
|
2021-04-14 17:58:02 -04:00
|
|
|
// log.Println("no msg on MenuControl, continuing")
|
|
|
|
if !paused {
|
|
|
|
// log.Println("menu watchercontrol waiting for IO")
|
|
|
|
select {
|
|
|
|
case res := <-inChan:
|
|
|
|
if res.Error != nil {
|
|
|
|
log.Println("error on inChan: ", res.Error)
|
|
|
|
}
|
|
|
|
if res.Operation == gaio.OpRead && res.Size > 0 && res.Conn == conn {
|
|
|
|
log.Printf("received on inChan: conn: %v, buffer size: %v\n", res.Conn.RemoteAddr(), res.Size)
|
|
|
|
log.Println("menu receive: ", strings.TrimSpace(string(res.Buffer[:res.Size-2])))
|
|
|
|
switch string(strings.TrimSpace(string(res.Buffer[:res.Size-2]))) {
|
2021-04-14 11:25:41 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
case "welcome":
|
|
|
|
if err := w.Write(ctx, conn, welcomeHandler()); err != nil {
|
|
|
|
log.Printf("error sending welcomeHandler from cmd `welcome`: %v", err)
|
2021-04-14 11:25:41 -04:00
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
|
|
|
|
case "adventure":
|
|
|
|
// start the door and wait
|
2021-04-14 18:27:45 -04:00
|
|
|
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"
|
2021-04-14 17:58:02 -04:00
|
|
|
err := w.Write(ctx, conn, []byte("\n\n> "))
|
2021-04-14 11:25:41 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error writing to connection: %v", err)
|
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
|
|
|
|
case "exit":
|
|
|
|
if err := w.Write(ctx, conn, exitHandler()); err != nil {
|
2021-04-14 18:27:45 -04:00
|
|
|
log.Printf("error sending exitHandler from cmd `exit`: %v\n", err)
|
2021-04-14 17:58:02 -04:00
|
|
|
} 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...")
|
2021-04-14 18:27:45 -04:00
|
|
|
log.Printf("Goodbye %s!\n", conn.RemoteAddr())
|
2021-04-14 17:58:02 -04:00
|
|
|
err := w.Free(conn)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("err closing connection: %v\n", err)
|
|
|
|
}
|
|
|
|
MenuControl <- "stop"
|
|
|
|
break ControlLoop
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
|
|
|
|
err := w.Write(ctx, conn, []byte("huh?\n\n> "))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error writing to connection: %v", err)
|
2021-04-14 11:25:41 -04:00
|
|
|
}
|
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// noop
|
|
|
|
log.Printf("received on inChan: conn: %v, buffer: %v\n", res.Conn.RemoteAddr(), string(res.Buffer))
|
2021-04-14 11:25:41 -04:00
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
/*
|
|
|
|
case res := <-outChan:
|
|
|
|
// log.Println("OpResult on outChan:", res)
|
|
|
|
if res.Error != nil {
|
|
|
|
log.Println("error on outChan: ", res.Error)
|
|
|
|
}
|
|
|
|
if res.Operation == gaio.OpWrite && res.Size > 0 && res.Conn == conn {
|
|
|
|
// write confirmation received
|
|
|
|
//log.Printf("received on outChan: conn: %v, buffer: %v\n", res.Conn.RemoteAddr(), string(res.Buffer))
|
|
|
|
} else {
|
|
|
|
// noop
|
|
|
|
log.Printf("watcher sent unknown on outChan: %v\n", res)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
default:
|
|
|
|
// noop - wait a lil bit
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
if conn == nil {
|
|
|
|
log.Println("conn is nil!")
|
2021-04-14 11:25:41 -04:00
|
|
|
}
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
time.Sleep(time.Millisecond)
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
log.Println("menu controlloop closed!")
|
|
|
|
}(inChan, outChan, MenuControl)
|
2021-04-14 08:51:34 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
log.Println("menu waiting on waitgroup")
|
|
|
|
wg.Wait()
|
|
|
|
log.Println("terminating menucontrol for client")
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
|
|
|
|
2021-04-14 11:25:41 -04:00
|
|
|
func welcomeHandler() []byte {
|
2021-04-14 08:51:34 -04:00
|
|
|
const bannerText = "\x1b[2J\x1b[H\x1b[31m\x1b[J\r\n" + `
|
|
|
|
|
|
|
|
_ __ _ ` + "" + `
|
|
|
|
| | / /__ / /________ ____ ___ ___` + "" + `
|
|
|
|
| | /| / / _ \/ / ___/ __ \/ __ '__ \/ _ \` + "" + `
|
|
|
|
| |/ |/ / __/ / /__/ /_/ / / / / / / __/` + "" + `
|
|
|
|
|__/|__/\___/_/\___/\____/_/ /_/ /_/\___/` + "\r\n\x1b[33m" + `
|
|
|
|
__` + "" + `
|
|
|
|
/ /_____ ` + "" + `
|
|
|
|
/ __/ __ \` + "" + `
|
|
|
|
/ /_/ /_/ /` + "" + `
|
|
|
|
\__/\____/` + "\x1b[35m\r\n" + `
|
|
|
|
_ __` + "" + `
|
|
|
|
| | / /__ _____________` + "" + `
|
|
|
|
| | / / _ \/ ___/ ___/ _ \` + "" + `
|
|
|
|
| |/ / __/ / (__ ) __/` + "" + `
|
|
|
|
|___/\___/_/ /____/\___/` + "\x1b[37m\r\n" + `
|
|
|
|
_____ __ ___` + "" + `
|
|
|
|
/ ___// /___ ______/ (_)___ _____` + "" + `
|
|
|
|
\__ \/ __/ / / / __ / / __ \/ ___/` + "" + `
|
|
|
|
___/ / /_/ /_/ / /_/ / / /_/ (__ ) ` + "" + `
|
|
|
|
/____/\__/\__,_/\__,_/_/\____/____/ ` + "\x1b[32m\r\n\r\n\r\n" + `
|
|
|
|
Help is available: type help` + "\r\n\r\n> "
|
2021-04-14 11:25:41 -04:00
|
|
|
|
|
|
|
return []byte(bannerText)
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
func exitHandler() []byte {
|
2021-04-14 08:51:34 -04:00
|
|
|
const exitMessage = "\x1b[2J\x1b[H\x1b[31m\x1b[J\r\n" + `
|
|
|
|
|
2021-04-14 11:25:41 -04:00
|
|
|
___| | |
|
|
|
|
\___ \ _ \ _ \ | | _ \ | | __| __ \ _' | __| _ \ __| _ \\ \ \ / __ \ _ \ | | |
|
|
|
|
| __/ __/ | | ( | | | \__ \ | | ( | ( __/ ( ( |\ \ \ / | | ( | | |_|
|
|
|
|
_____/ \___|\___| \__, |\___/ \__,_| ) ____/ .__/ \__,_|\___|\___| \___|\___/ \_/\_/ _.__/ \___/ \__, |_)
|
|
|
|
____/ / _| ____/
|
2021-04-14 08:51:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
`
|
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
return []byte(exitMessage)
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
|
|
|
|
2021-04-14 18:27:45 -04:00
|
|
|
func doorHandler(ctx context.Context, c net.Conn, w *gaio.Watcher, menuwg *sync.WaitGroup) error {
|
|
|
|
defer menuwg.Done()
|
|
|
|
|
2021-04-14 08:51:34 -04:00
|
|
|
const bannerText = "\r\nCOLOSSAL CAVE\r\n\r\n"
|
2021-04-14 18:27:45 -04:00
|
|
|
err := w.Write(ctx, c, []byte(bannerText))
|
2021-04-14 08:51:34 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error writing to connection: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:27:45 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-04-14 08:51:34 -04:00
|
|
|
cmd := exec.Command("/usr/games/adventure")
|
|
|
|
|
|
|
|
stdout, _ := cmd.StdoutPipe()
|
|
|
|
stdin, _ := cmd.StdinPipe()
|
|
|
|
|
|
|
|
cmd.Start()
|
2021-04-14 18:27:45 -04:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
2021-04-14 08:51:34 -04:00
|
|
|
wg.Add(1)
|
2021-04-14 18:27:45 -04:00
|
|
|
Terminator := make(chan bool)
|
|
|
|
go func(Terminator *chan bool, wg *sync.WaitGroup) {
|
2021-04-14 08:51:34 -04:00
|
|
|
log.Println("starting stdin reader goroutine")
|
|
|
|
defer wg.Done()
|
|
|
|
Check:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-*Terminator:
|
|
|
|
break Check
|
2021-04-14 18:27:45 -04:00
|
|
|
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])
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
2021-04-14 18:27:45 -04:00
|
|
|
} else {
|
|
|
|
log.Println("yikes, problem getting a result from inChan in doorHandler!")
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
2021-04-14 18:27:45 -04:00
|
|
|
default:
|
|
|
|
time.Sleep(time.Millisecond)
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Println("exiting stdin reader goroutine")
|
2021-04-14 18:27:45 -04:00
|
|
|
}(&Terminator, &wg)
|
2021-04-14 08:51:34 -04:00
|
|
|
|
|
|
|
log.Println("starting stdout reader goroutine")
|
|
|
|
scanner := bufio.NewScanner(stdout)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
m := scanner.Bytes()
|
2021-04-14 18:27:45 -04:00
|
|
|
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))
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
|
|
|
log.Println("exiting stdout reader goroutine")
|
|
|
|
Terminator <- true
|
|
|
|
|
|
|
|
log.Println("waiting for wg")
|
|
|
|
wg.Wait()
|
|
|
|
log.Println("waiting for cmd")
|
|
|
|
exitErr := cmd.Wait()
|
|
|
|
if exitErr != nil {
|
|
|
|
log.Printf("cmd exited with err: %v\n", exitErr)
|
|
|
|
}
|
2021-04-14 18:27:45 -04:00
|
|
|
log.Println("leaving door handler")
|
2021-04-14 08:51:34 -04:00
|
|
|
return nil
|
|
|
|
}
|