ソースを参照

Fix windows input go routine.

It wasn't shutting down.
door32w.go: allows testing of door, greets and says goodbye.
Steve Thielemann 1 年間 前
コミット
3adddd4290
3 ファイル変更46 行追加6 行削除
  1. 16 1
      door/input_windows.go
  2. 25 5
      door32w.go
  3. 5 0
      runtest-win.bat

+ 16 - 1
door/input_windows.go

@@ -29,7 +29,9 @@ func extended_output(buffer []rune) string {
 
 func Reader(d *Door) {
 	// handle syscall.Handle, readerChannel *chan byte
-	C.init_wsa()
+	var WSA syscall.WSAData
+	syscall.WSAStartup(0x202, &WSA)
+	// C.init_wsa()
 
 	var runebuffer bytes.Buffer
 	var runeread = bufio.NewReaderSize(&runebuffer, 1)
@@ -74,8 +76,21 @@ func Reader(d *Door) {
 			// timeout
 			ReadRune(d, runeread, true)
 			// process(d, false)
+
+			d.readerMutex.Lock()
+			if d.ReaderEnd {
+				log.Println("Door closing...")
+				if !d.ReaderClosed {
+					d.ReaderClosed = true
+					close(d.readerChannel)
+				}
+				d.readerMutex.Unlock()
+				return
+			}
+			d.readerMutex.Unlock()
 			continue
 		}
+
 		gobytes := C.GoBytes(unsafe.Pointer(readerbuffer), read)
 
 		if DEBUG_INPUT {

+ 25 - 5
door32w.go

@@ -1,5 +1,10 @@
 package main
 
+/*
+A Windows Door tester.
+
+*/
+
 import (
 	"flag"
 	"fmt"
@@ -24,7 +29,7 @@ func main() {
 	flag.Parse()
 
 	if port == 0 && flag.NArg() != 1 {
-		fmt.Println("I need a Port and a commandline to execute.")
+		fmt.Println("I need a Port and a door command line to execute.")
 		flag.PrintDefaults()
 		os.Exit(2)
 	}
@@ -46,7 +51,7 @@ func main() {
 		fmt.Println("Client connected.")
 		fmt.Println("Client " + c.RemoteAddr().String() + " connected.")
 
-		// fork goes here
+		// Handle the connection in the background...
 		go handleConnection(c, flag.Arg(0))
 		c = nil
 	}
@@ -82,6 +87,8 @@ func handleConnection(conn net.Conn, cmd string) {
 
 	conn.Write([]byte("\xff\xfb\x01\xff\xfb\x03\xff\xfd\x10"))
 	drain(conn)
+	conn.Write([]byte("\n\rLaunching door\n\r"))
+	time.Sleep(time.Second)
 
 	fmt.Printf("%#v\n", conn)
 	tcp, _ := conn.(*net.TCPConn)
@@ -89,6 +96,8 @@ func handleConnection(conn net.Conn, cmd string) {
 
 	// https://github.com/golang/go/issues/10350
 
+	// Duplicate the handle for passing off to the door.
+
 	raw, err := tcp.SyscallConn()
 	fmt.Printf("%#v, err: %#v\n", raw, err)
 
@@ -96,7 +105,7 @@ func handleConnection(conn net.Conn, cmd string) {
 	raw.Control(func(fd uintptr) {
 		socket_fd = fd
 	})
-	fmt.Printf("Socket: %#v\n", socket_fd)
+	fmt.Printf("Socket FD: %#v\n", socket_fd)
 	proc, _ := syscall.GetCurrentProcess()
 	var dup_socket_fd syscall.Handle
 
@@ -106,7 +115,7 @@ func handleConnection(conn net.Conn, cmd string) {
 		fmt.Printf("ERR DuplicateHandle: %#v\n", err)
 	}
 
-	fmt.Printf("Dup: %#v\n", dup_socket_fd)
+	fmt.Printf("Dup FD: %#v\n", dup_socket_fd)
 
 	// windows:  tcp.File() failes.  net.OpError
 	/*
@@ -144,7 +153,7 @@ func handleConnection(conn net.Conn, cmd string) {
 
 	err = syscall.CreateProcess(nil, argv, nil, nil, true, 0, nil, nil, &si, &pi)
 
-	fmt.Printf("Return: %#v\n", err)
+	fmt.Printf("Door has launched.  Return: %#v\n", err)
 	raw = nil
 
 	if err == nil {
@@ -152,6 +161,11 @@ func handleConnection(conn net.Conn, cmd string) {
 		fmt.Printf("Event %#v, err: %#v\n", event, e)
 	}
 
+	fmt.Println("Close Duplicate.")
+	// Close the duplicate...
+	syscall.CloseHandle(dup_socket_fd)
+	fmt.Println("Close Done.")
+
 	/*
 		exec_cmd := exec.Command(parts[0], parts[1:]...)
 		fmt.Printf("exec_cmd: %#v\n", exec_cmd)
@@ -192,14 +206,20 @@ func handleConnection(conn net.Conn, cmd string) {
 	// fmt.Printf("Child started: %d\n", id)
 	// return
 
+	fmt.Println("Welcome caller back.")
+
 	conn.Write([]byte("Welcome back!\r\nSeeya!\r\n"))
 
+	fmt.Println("Ok, shut this connection down.")
+
 	// I can't seem to get this to close the connection.  :(
 	conn.Close()
 	syscall.CloseHandle(syscall.Handle(socket_fd))
 	tcp.SetLinger(0)
 	tcp.Close()
 
+	fmt.Println("Closed.")
+
 	/*
 		for {
 			buffer, err := bufio.NewReader(conn).ReadBytes('\r') // HMM.  \r ?

+ 5 - 0
runtest-win.bat

@@ -0,0 +1,5 @@
+cd testdoor
+go build
+cd ..
+go build door32w.go convert.go
+door32w -p 4044 "testdoor\testdoor.exe -d ..\door32.sys"