use the common nonblocking socket read

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-09 21:48:35 -07:00
parent 562b20cbb4
commit 183ff58f8e
1 changed files with 4 additions and 31 deletions

View File

@ -11,6 +11,8 @@ import "github.com/svent/go-nbreader"
import "git.wit.com/jcarr/dnssecsocket"
import "git.wit.com/wit/shell"
// import "github.com/gookit/config"
// import "git.wit.com/wit/gui"
@ -60,7 +62,8 @@ func socketToWIT(myhostname string) {
connStdin, _ := conn.File()
go nonBlockingReader(connStdin, os.Stdout)
newreader := bufio.NewReader(connStdin)
go shell.NonBlockingReader(newreader, os.Stdout)
// read in input from stdin
reader := bufio.NewReader(os.Stdin)
@ -89,33 +92,3 @@ func retrySocket() {
socketToWIT("librem15.lab.wit.com")
}
}
// pass in two file handles (1 read, 1 write)
func nonBlockingReader(readFileHandle *os.File, writeFileHandle *os.File) {
// create a nonblocking GO reader
newreader := bufio.NewReader(readFileHandle)
nbr := nbreader.NewNBReader(newreader, 1024)
for {
defer readFileHandle.Close()
defer writeFileHandle.Close()
totalCount := 0
for {
oneByte := make([]byte, 1024)
count, err := nbr.Read(oneByte)
if (err != nil) {
log.Println("count, err =", count, err)
return
}
totalCount += count
if (count == 0) {
time.Sleep(20 * time.Millisecond) // only check the buffer 500 times a second
if (totalCount != 0) {
log.Println("totalCount = ", totalCount)
totalCount = 0
}
}
writeFileHandle.Write([]byte(string(oneByte)))
}
}
}