Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for connect to finish sending all data #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions telsh/telnet_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,27 +229,35 @@ func (telnetHandler *ShellHandler) ServeTELNET(ctx telnet.Context, writer telnet

//@TODO: Wire up the stdin, stdout, stderr of the handler.

var stdoutDone chan bool
if stdoutPipe, err := handler.StdoutPipe(); nil != err {
//@TODO:
} else if nil == stdoutPipe {
//@TODO:
} else {
connect(ctx, writer, stdoutPipe)
stdoutDone = connect(ctx, writer, stdoutPipe)
}


var stderrDone chan bool
if stderrPipe, err := handler.StderrPipe(); nil != err {
//@TODO:
} else if nil == stderrPipe {
//@TODO:
} else {
connect(ctx, writer, stderrPipe)
stderrDone = connect(ctx, writer, stderrPipe)
}


if err := handler.Run(); nil != err {
//@TODO:
}
if stdoutDone != nil {
<-stdoutDone
}
if stderrDone != nil {
<-stderrDone
}
line.Reset()
if _, err := oi.LongWrite(writer, promptBytes); nil != err {
return
Expand All @@ -270,10 +278,13 @@ func (telnetHandler *ShellHandler) ServeTELNET(ctx telnet.Context, writer telnet



func connect(ctx telnet.Context, writer io.Writer, reader io.Reader) {
func connect(ctx telnet.Context, writer io.Writer, reader io.Reader) chan bool {

logger := ctx.Logger()

// Create a buffer to avoid the goroutine to be blocked.
// The goroutine may leave whenever its job is done.
done := make(chan bool, 1)
go func(logger telnet.Logger){

var buffer [1]byte // Seems like the length of the buffer needs to be small, otherwise will have to wait for buffer to fill up.
Expand All @@ -293,5 +304,7 @@ func connect(ctx telnet.Context, writer io.Writer, reader io.Reader) {
oi.LongWrite(writer, p)
//logger.Tracef("Sent: %q.", p)
}
done <- true
}(logger)
return done
}