simplify Socket.Read

This commit is contained in:
Shivaram Lingamneni 2020-06-23 03:18:49 -04:00
parent be138e4d71
commit 28a0ec86b5
1 changed files with 7 additions and 14 deletions

View File

@ -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: