From 28a0ec86b58ba0721962a0a4cdba06eb38f899dd Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Tue, 23 Jun 2020 03:18:49 -0400 Subject: [PATCH] simplify Socket.Read --- irc/socket.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/irc/socket.go b/irc/socket.go index 6304a1a4..f4d6844e 100644 --- a/irc/socket.go +++ b/irc/socket.go @@ -7,7 +7,6 @@ package irc import ( "errors" "io" - "strings" "sync" "github.com/oragono/oragono/irc/utils" @@ -59,30 +58,24 @@ func (socket *Socket) Close() { // Read returns a single IRC line from a Socket. func (socket *Socket) Read() (string, error) { + // immediately fail if Close() has been called, even if there's + // still data in a bufio.Reader or websocket buffer: if socket.IsClosed() { return "", io.EOF } lineBytes, err := socket.conn.ReadLine() - - // convert bytes to string line := string(lineBytes) - // read last message properly (such as ERROR/QUIT/etc), just fail next reads/writes if err == io.EOF { socket.Close() + // process last message properly (such as ERROR/QUIT/etc), just fail next reads/writes + if line != "" { + err = nil + } } - if err == io.EOF && strings.TrimSpace(line) != "" { - // don't do anything - } else if err == errInvalidUtf8 { - // pass the data through so we can parse the command at least - return line, err - } else if err != nil { - return "", err - } - - return line, nil + return line, err } // Write sends the given string out of Socket. Requirements: