3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-11 06:29:29 +01:00
ergo/src/irc/net.go

47 lines
711 B
Go
Raw Normal View History

2012-04-07 20:44:59 +02:00
package irc
import (
"bufio"
"log"
"net"
)
// Adapt `net.Conn` to a `chan string`.
func NewStringChan(conn net.Conn) chan string {
ch := make(chan string)
rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
done := make(chan bool)
go func() {
<- done
close(ch)
}()
// conn -> ch
go func() {
for {
line, err := rw.ReadString('\n')
if err != nil {
log.Print("StringChan[read]: %v", err)
break
}
ch <- line
}
done <- true
}()
// ch -> conn
go func() {
for str := range ch {
if _, err := rw.WriteString(str + "\r\n"); err != nil {
log.Print("StringChan[write]: %v", err)
break
}
rw.Flush()
}
done <- true
}()
return ch
}