/
base_conn.go
60 lines (51 loc) · 1.17 KB
/
base_conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package irc
import (
"bufio"
"io"
"net"
"sync"
)
// BaseConn is a simple IRC connection.
type BaseConn struct {
conn net.Conn
scanner *bufio.Scanner
mu sync.Mutex
}
var _ Conn = (*BaseConn)(nil)
// NewBaseConn creates a new BaseConn from a net.Conn.
func NewBaseConn(conn net.Conn) *BaseConn {
return &BaseConn{
conn: conn,
scanner: bufio.NewScanner(conn),
}
}
// BaseDial is shorthand for calling net.Dial("tcp", addr) and calling
// NewBaseConn on the returned net.Conn.
func BaseDial(addr string) (*BaseConn, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
return NewBaseConn(conn), nil
}
// Close closes the underlying connection.
func (b *BaseConn) Close() error {
return b.conn.Close()
}
// Encode encodes a message over the connection.
func (b *BaseConn) Encode(m *Message) error {
_, err := m.WriteToWithNewline(b.conn)
return err
}
// Decode decodes a message into the argument, which cannot be nil.
func (b *BaseConn) Decode(m *Message) error {
b.mu.Lock()
defer b.mu.Unlock()
if !b.scanner.Scan() {
if err := b.scanner.Err(); err != nil {
return err
}
return io.EOF
}
return m.Parse(b.scanner.Text())
}