3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00

simplify Socket.Read

This commit is contained in:
Shivaram Lingamneni 2020-06-23 03:18:49 -04:00
parent be138e4d71
commit 28a0ec86b5

View File

@ -7,7 +7,6 @@ package irc
import ( import (
"errors" "errors"
"io" "io"
"strings"
"sync" "sync"
"github.com/oragono/oragono/irc/utils" "github.com/oragono/oragono/irc/utils"
@ -59,30 +58,24 @@ func (socket *Socket) Close() {
// Read returns a single IRC line from a Socket. // Read returns a single IRC line from a Socket.
func (socket *Socket) Read() (string, error) { 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() { if socket.IsClosed() {
return "", io.EOF return "", io.EOF
} }
lineBytes, err := socket.conn.ReadLine() lineBytes, err := socket.conn.ReadLine()
// convert bytes to string
line := string(lineBytes) line := string(lineBytes)
// read last message properly (such as ERROR/QUIT/etc), just fail next reads/writes
if err == io.EOF { if err == io.EOF {
socket.Close() 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 return line, err
} else if err != nil {
return "", err
}
return line, nil
} }
// Write sends the given string out of Socket. Requirements: // Write sends the given string out of Socket. Requirements: