Selaa lähdekoodia

use a Scanner instead of ReadString

tags/v0.1.0
Jeremy Latt 10 vuotta sitten
vanhempi
commit
2dc69c7e3d
1 muutettua tiedostoa jossa 7 lisäystä ja 9 poistoa
  1. 7
    9
      irc/socket.go

+ 7
- 9
irc/socket.go Näytä tiedosto

@@ -4,7 +4,6 @@ import (
4 4
 	"bufio"
5 5
 	"io"
6 6
 	"net"
7
-	"strings"
8 7
 )
9 8
 
10 9
 const (
@@ -15,14 +14,12 @@ const (
15 14
 
16 15
 type Socket struct {
17 16
 	conn   net.Conn
18
-	reader *bufio.Reader
19 17
 	writer *bufio.Writer
20 18
 }
21 19
 
22 20
 func NewSocket(conn net.Conn, commands chan<- Command) *Socket {
23 21
 	socket := &Socket{
24 22
 		conn:   conn,
25
-		reader: bufio.NewReader(conn),
26 23
 		writer: bufio.NewWriter(conn),
27 24
 	}
28 25
 
@@ -41,12 +38,9 @@ func (socket *Socket) Close() {
41 38
 }
42 39
 
43 40
 func (socket *Socket) readLines(commands chan<- Command) {
44
-	for {
45
-		line, err := socket.reader.ReadString('\n')
46
-		if socket.isError(err, R) {
47
-			break
48
-		}
49
-		line = strings.TrimRight(line, CRLF)
41
+	scanner := bufio.NewScanner(socket.conn)
42
+	for scanner.Scan() {
43
+		line := scanner.Text()
50 44
 		if len(line) == 0 {
51 45
 			continue
52 46
 		}
@@ -60,6 +54,10 @@ func (socket *Socket) readLines(commands chan<- Command) {
60 54
 		commands <- msg
61 55
 	}
62 56
 
57
+	if err := scanner.Err(); err != nil {
58
+		Log.debug.Printf("%s error: %s", socket, err)
59
+	}
60
+
63 61
 	close(commands)
64 62
 }
65 63
 

Loading…
Peruuta
Tallenna