diff options
Diffstat (limited to 'conn.go')
-rw-r--r-- | conn.go | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -0,0 +1,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)) +} |