From 2dc69c7e3d32c9c99ed2dfc1b0c508d9e96e8b1c Mon Sep 17 00:00:00 2001 From: Jeremy Latt Date: Thu, 27 Mar 2014 17:51:29 -0700 Subject: [PATCH] use a Scanner instead of ReadString --- irc/socket.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/irc/socket.go b/irc/socket.go index 2b075ea0..aa9be493 100644 --- a/irc/socket.go +++ b/irc/socket.go @@ -4,7 +4,6 @@ import ( "bufio" "io" "net" - "strings" ) const ( @@ -15,14 +14,12 @@ const ( type Socket struct { conn net.Conn - reader *bufio.Reader writer *bufio.Writer } func NewSocket(conn net.Conn, commands chan<- Command) *Socket { socket := &Socket{ conn: conn, - reader: bufio.NewReader(conn), writer: bufio.NewWriter(conn), } @@ -41,12 +38,9 @@ func (socket *Socket) Close() { } func (socket *Socket) readLines(commands chan<- Command) { - for { - line, err := socket.reader.ReadString('\n') - if socket.isError(err, R) { - break - } - line = strings.TrimRight(line, CRLF) + scanner := bufio.NewScanner(socket.conn) + for scanner.Scan() { + line := scanner.Text() if len(line) == 0 { continue } @@ -60,6 +54,10 @@ func (socket *Socket) readLines(commands chan<- Command) { commands <- msg } + if err := scanner.Err(); err != nil { + Log.debug.Printf("%s error: %s", socket, err) + } + close(commands) }