Skip to content

Commit

Permalink
Wait for connect to finish sending all data
Browse files Browse the repository at this point in the history
Go function created in connect, transfer data from a handler to telnet
client. However, as there's no synchronization between the go function
and the telnet server, there's a case where the server sends a prompt
before the go funcs finishsed all transmissions.

This fix assure that the telnet server waits until all data are sent to
the client, before sending a prompt.
  • Loading branch information
thayama committed Dec 6, 2018
1 parent 9ff0b2a commit c8d60d6
Showing 1 changed file with 16 additions and 3 deletions.
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 go function to be blocked.
// The go function 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
}

0 comments on commit c8d60d6

Please sign in to comment.