3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

use a Scanner instead of ReadString

This commit is contained in:
Jeremy Latt 2014-03-27 17:51:29 -07:00
parent d696f2313e
commit 2dc69c7e3d

View File

@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"io" "io"
"net" "net"
"strings"
) )
const ( const (
@ -15,14 +14,12 @@ const (
type Socket struct { type Socket struct {
conn net.Conn conn net.Conn
reader *bufio.Reader
writer *bufio.Writer writer *bufio.Writer
} }
func NewSocket(conn net.Conn, commands chan<- Command) *Socket { func NewSocket(conn net.Conn, commands chan<- Command) *Socket {
socket := &Socket{ socket := &Socket{
conn: conn, conn: conn,
reader: bufio.NewReader(conn),
writer: bufio.NewWriter(conn), writer: bufio.NewWriter(conn),
} }
@ -41,12 +38,9 @@ func (socket *Socket) Close() {
} }
func (socket *Socket) readLines(commands chan<- Command) { func (socket *Socket) readLines(commands chan<- Command) {
for { scanner := bufio.NewScanner(socket.conn)
line, err := socket.reader.ReadString('\n') for scanner.Scan() {
if socket.isError(err, R) { line := scanner.Text()
break
}
line = strings.TrimRight(line, CRLF)
if len(line) == 0 { if len(line) == 0 {
continue continue
} }
@ -60,6 +54,10 @@ func (socket *Socket) readLines(commands chan<- Command) {
commands <- msg commands <- msg
} }
if err := scanner.Err(); err != nil {
Log.debug.Printf("%s error: %s", socket, err)
}
close(commands) close(commands)
} }