Browse Source

I handle hangup properly.

Steve Thielemann 3 years ago
parent
commit
f74d460266
3 changed files with 33 additions and 7 deletions
  1. 10 5
      door/door.go
  2. 16 1
      door/input.go
  3. 7 1
      testdoor/testdoor.go

+ 10 - 5
door/door.go

@@ -53,11 +53,12 @@ type DropfileConfig struct {
 }
 
 type Door struct {
-	config   DropfileConfig
-	READFD   int
-	WRITEFD  int
-	TimeOut  time.Time // Fixed point in time, when time expires
-	pushback *list.List
+	config       DropfileConfig
+	READFD       int
+	WRITEFD      int
+	Disconnected bool
+	TimeOut      time.Time // Fixed point in time, when time expires
+	pushback     *list.List
 }
 
 // Return the amount of time left as time.Duration
@@ -197,10 +198,14 @@ func (d *Door) Init() {
 
 // Write string to client.
 func (d *Door) Write(output string) {
+	if d.Disconnected {
+		return
+	}
 	buffer := []byte(output)
 	n, err := syscall.Write(d.WRITEFD, buffer)
 	if err != nil {
 		fmt.Println("Write error/HANGUP?", n)
+		d.Disconnected = true
 	}
 	// No, this isn't it.  The # of bytes in buffer == bytes written.
 	if n != len(buffer) {

+ 16 - 1
door/input.go

@@ -54,6 +54,10 @@ func clearAll(fdSetPtr *syscall.FdSet) {
 // Is there a key waiting?
 // Returns true on key, or if hungup/time out
 func (d *Door) HasKey() bool {
+	if d.Disconnected {
+		return true
+	}
+
 	if d.TimeLeft() < 0 {
 		return true
 	}
@@ -96,8 +100,10 @@ func (d *Door) getch() int {
 	buffer := make([]byte, 1)
 	r, err := syscall.Read(d.READFD, buffer)
 	if r != 1 {
+		// I'm getting write error here... (when disconnected)
 		fmt.Printf("Read said ready, but didn't read a character %d %v.", r, err)
 		// hangup
+		d.Disconnected = true
 		return -2
 	}
 	return int(buffer[0])
@@ -119,10 +125,13 @@ func (d *Door) getkey_or_pushback() int {
 func (d *Door) GetKey() int {
 	var c, c2 int
 
+	if d.Disconnected {
+		return -2
+	}
+
 	if d.TimeLeft() < 0 {
 		return -3
 	}
-
 	c = d.getkey_or_pushback()
 
 	if c < 0 {
@@ -296,6 +305,10 @@ func (d *Door) GetKey() int {
 }
 
 func (d *Door) WaitKey(secs int64, usecs int64) int {
+	if d.Disconnected {
+		return -2
+	}
+
 	// var fdsetRead, fdsetWrite, fdsete syscall.FdSet
 	var fdsetRead syscall.FdSet
 	// fdsetWrite := syscall.FdSet
@@ -331,8 +344,10 @@ func (d *Door) WaitKey(secs int64, usecs int64) int {
 	buffer := make([]byte, 1)
 	r, err := syscall.Read(d.READFD, buffer)
 	if r != 1 {
+		// I'm getting write error here... (when disconnected)
 		fmt.Printf("Read said ready, but didn't read a character %d %v.", r, err)
 		// hangup
+		d.Disconnected = true
 		return -2
 	}
 	return int(buffer[0])

+ 7 - 1
testdoor/testdoor.go

@@ -3,6 +3,7 @@ package main
 import (
 	"fmt"
 	"red-green/door"
+	"time"
 )
 
 func pctUpdate(pct *int64) func() int64 {
@@ -79,7 +80,12 @@ func main() {
 		bar3.Update()
 		d.Write(bar3.Output())
 
-		d.WaitKey(0, 55000)
+		if d.Disconnected {
+			break
+		}
+
+		time.Sleep(time.Millisecond * 100)
+		// d.WaitKey(0, 55000)
 	}
 
 	d.Write(door.CRNL)