diff options
Diffstat (limited to 'internal/net')
| -rw-r--r-- | internal/net/conn.go | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/internal/net/conn.go b/internal/net/conn.go index 2a250d8..42b7c41 100644 --- a/internal/net/conn.go +++ b/internal/net/conn.go @@ -9,6 +9,7 @@ import ( "io" "net" "strings" + "sync/atomic" "time" ) @@ -25,9 +26,11 @@ type Conn interface { } type tcpConn struct { - conn net.Conn - reader *bufio.Reader - echoOff bool + conn net.Conn + reader *bufio.Reader + // echoOff is written by the owner goroutine (SetEcho/SetEchoState) and + // read by this connection's reader goroutine, so it must be atomic. + echoOff atomic.Bool } func newTCPConn(conn net.Conn) *tcpConn { @@ -69,7 +72,7 @@ func (t *tcpConn) ReadMessage() (string, error) { } text := strings.TrimRight(string(line), "\r\n") text = stripIAC(text) - if t.echoOff { + if t.echoOff.Load() { t.conn.Write([]byte("\r\n")) } return text, nil @@ -88,7 +91,7 @@ func (t *tcpConn) Close() error { } func (t *tcpConn) SetEcho(on bool) error { - t.echoOff = !on + t.echoOff.Store(!on) if on { return t.writeTelnetCmd(wont, echo) } @@ -109,7 +112,7 @@ func (t *tcpConn) EchoSequence(on bool) []byte { // SetEchoState records the echo state without writing to the network; used // when the negotiation bytes are queued instead of written inline. func (t *tcpConn) SetEchoState(on bool) { - t.echoOff = !on + t.echoOff.Store(!on) } const ( |
