aboutsummaryrefslogtreecommitdiff
path: root/conn.go
blob: ac3787c5ae11fc2a381d7aad846be68bbf6f727e (plain) (blame)
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

package irc

import (
	"bufio"
	"net"
	"slices"
)

type Conn struct {
	netConn   net.Conn
	bufReader *bufio.Reader
}

func NewConn(netConn net.Conn) Conn {
	return Conn{
		netConn:   netConn,
		bufReader: bufio.NewReader(netConn),
	}
}

func (c *Conn) ReadMessage() (msg Message, err error) {
	raw, err := c.bufReader.ReadSlice('\n')
	if err != nil {
		return
	}

	if raw[len(raw) - 1] == '\r' {
		raw = raw[:len(raw) - 1]
	}

	msg, err = Parse(slices.Clone(raw))

	return
}

func (c *Conn) Write(p []byte) (n int, err error) {
	return c.netConn.Write(p)
}

func (c *Conn) WriteString(s string) (n int, err error) {
	return c.netConn.Write(stringToBytes(s))
}