mirror of
https://github.com/ergochat/ergo.git
synced 2024-11-10 22:19:31 +01:00
get rid of channel abstraction for buffered socket io
This commit is contained in:
parent
aac0efebee
commit
887f12cb31
@ -1,9 +1,12 @@
|
|||||||
package irc
|
package irc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -12,34 +15,36 @@ type Client struct {
|
|||||||
channels ChannelSet
|
channels ChannelSet
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
hostname string
|
hostname string
|
||||||
|
idleTimer *time.Timer
|
||||||
invisible bool
|
invisible bool
|
||||||
nick string
|
nick string
|
||||||
operator bool
|
operator bool
|
||||||
|
quitTimer *time.Timer
|
||||||
realname string
|
realname string
|
||||||
|
recv *bufio.Reader
|
||||||
registered bool
|
registered bool
|
||||||
replies chan<- Reply
|
replies chan<- Reply
|
||||||
|
send *bufio.Writer
|
||||||
server *Server
|
server *Server
|
||||||
serverPass bool
|
serverPass bool
|
||||||
username string
|
username string
|
||||||
idleTimer *time.Timer
|
|
||||||
quitTimer *time.Timer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(server *Server, conn net.Conn) *Client {
|
func NewClient(server *Server, conn net.Conn) *Client {
|
||||||
read := StringReadChan(conn)
|
|
||||||
write := StringWriteChan(conn)
|
|
||||||
replies := make(chan Reply)
|
replies := make(chan Reply)
|
||||||
|
|
||||||
client := &Client{
|
client := &Client{
|
||||||
channels: make(ChannelSet),
|
channels: make(ChannelSet),
|
||||||
conn: conn,
|
conn: conn,
|
||||||
hostname: AddrLookupHostname(conn.RemoteAddr()),
|
hostname: AddrLookupHostname(conn.RemoteAddr()),
|
||||||
|
recv: bufio.NewReader(conn),
|
||||||
replies: replies,
|
replies: replies,
|
||||||
|
send: bufio.NewWriter(conn),
|
||||||
server: server,
|
server: server,
|
||||||
}
|
}
|
||||||
|
|
||||||
go client.readConn(read)
|
go client.readConn()
|
||||||
go client.writeConn(write, replies)
|
go client.writeConn(replies)
|
||||||
|
|
||||||
client.Touch()
|
client.Touch()
|
||||||
return client
|
return client
|
||||||
@ -73,14 +78,26 @@ func (client *Client) Quit() {
|
|||||||
client.server.commands <- msg
|
client.server.commands <- msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) readConn(recv <-chan string) {
|
func (c *Client) readConn() {
|
||||||
for str := range recv {
|
for {
|
||||||
m, err := ParseCommand(str)
|
line, err := c.recv.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
log.Printf("%s → %s error: %s", c.conn.RemoteAddr(), c.conn.LocalAddr(), err)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
if DEBUG_NET {
|
||||||
|
log.Printf("%s → %s %s", c.conn.RemoteAddr(), c.conn.LocalAddr(), line)
|
||||||
|
}
|
||||||
|
|
||||||
|
m, err := ParseCommand(line)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == NotEnoughArgsError {
|
if err == NotEnoughArgsError {
|
||||||
c.Reply(ErrNeedMoreParams(c.server, str))
|
c.Reply(ErrNeedMoreParams(c.server, line))
|
||||||
} else {
|
} else {
|
||||||
c.Reply(ErrUnknownCommand(c.server, str))
|
c.Reply(ErrUnknownCommand(c.server, line))
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -90,12 +107,35 @@ func (c *Client) readConn(recv <-chan string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) writeConn(write chan<- string, replies <-chan Reply) {
|
func (client *Client) maybeLogWriteError(err error) bool {
|
||||||
|
if err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
log.Printf("%s ← %s error: %s", client.conn.RemoteAddr(), client.conn.LocalAddr(), err)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *Client) writeConn(replies <-chan Reply) {
|
||||||
for reply := range replies {
|
for reply := range replies {
|
||||||
if DEBUG_CLIENT {
|
if DEBUG_CLIENT {
|
||||||
log.Printf("%s ← %s %s", c, reply.Source(), reply)
|
log.Printf("%s ← %s %s", client, reply.Source(), reply)
|
||||||
|
}
|
||||||
|
for _, str := range reply.Format(client) {
|
||||||
|
if DEBUG_NET {
|
||||||
|
log.Printf("%s ← %s %s", client.conn.RemoteAddr(), client.conn.LocalAddr(), str)
|
||||||
|
}
|
||||||
|
if _, err := client.send.WriteString(str); client.maybeLogWriteError(err) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if _, err := client.send.WriteString(CRLF); client.maybeLogWriteError(err) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err := client.send.Flush(); client.maybeLogWriteError(err) {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
reply.Format(c, write)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
58
irc/net.go
58
irc/net.go
@ -1,68 +1,10 @@
|
|||||||
package irc
|
package irc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Adapt `net.Conn` to a `chan string`.
|
|
||||||
func StringReadChan(conn net.Conn) <-chan string {
|
|
||||||
ch := make(chan string)
|
|
||||||
reader := bufio.NewReader(conn)
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
line, err := reader.ReadString('\n')
|
|
||||||
if err != nil {
|
|
||||||
if err != io.EOF {
|
|
||||||
log.Printf("%s → %s error: %s", conn.RemoteAddr(), conn.LocalAddr(), err)
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if DEBUG_NET {
|
|
||||||
log.Printf("%s → %s %s", conn.RemoteAddr(), conn.LocalAddr(), line)
|
|
||||||
}
|
|
||||||
|
|
||||||
ch <- strings.TrimSpace(line)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return ch
|
|
||||||
}
|
|
||||||
|
|
||||||
func maybeLogWriteError(conn net.Conn, err error) bool {
|
|
||||||
if err != nil {
|
|
||||||
if err != io.EOF {
|
|
||||||
log.Printf("%s ← %s error: %s", conn.RemoteAddr(), conn.LocalAddr(), err)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func StringWriteChan(conn net.Conn) chan<- string {
|
|
||||||
ch := make(chan string)
|
|
||||||
writer := bufio.NewWriter(conn)
|
|
||||||
go func() {
|
|
||||||
for str := range ch {
|
|
||||||
if DEBUG_NET {
|
|
||||||
log.Printf("%s ← %s %s", conn.RemoteAddr(), conn.LocalAddr(), str)
|
|
||||||
}
|
|
||||||
if _, err := writer.WriteString(str); maybeLogWriteError(conn, err) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if _, err := writer.WriteString(CRLF); maybeLogWriteError(conn, err) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if err := writer.Flush(); maybeLogWriteError(conn, err) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return ch
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddrLookupHostname(addr net.Addr) string {
|
func AddrLookupHostname(addr net.Addr) string {
|
||||||
addrStr := addr.String()
|
addrStr := addr.String()
|
||||||
ipaddr, _, err := net.SplitHostPort(addrStr)
|
ipaddr, _, err := net.SplitHostPort(addrStr)
|
||||||
|
18
irc/reply.go
18
irc/reply.go
@ -41,8 +41,8 @@ func NewStringReply(source Identifier, code string,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (reply *StringReply) Format(client *Client, write chan<- string) {
|
func (reply *StringReply) Format(client *Client) []string {
|
||||||
write <- reply.message
|
return []string{reply.message}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (reply *StringReply) String() string {
|
func (reply *StringReply) String() string {
|
||||||
@ -63,8 +63,8 @@ func NewNumericReply(source Identifier, code int, format string,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (reply *NumericReply) Format(client *Client, write chan<- string) {
|
func (reply *NumericReply) Format(client *Client) []string {
|
||||||
write <- reply.FormatString(client)
|
return []string{reply.FormatString(client)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (reply *NumericReply) FormatString(client *Client) string {
|
func (reply *NumericReply) FormatString(client *Client) string {
|
||||||
@ -93,7 +93,8 @@ func NewNamesReply(channel *Channel) Reply {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (reply *NamesReply) Format(client *Client, write chan<- string) {
|
func (reply *NamesReply) Format(client *Client) []string {
|
||||||
|
lines := make([]string, 0)
|
||||||
base := RplNamReply(reply.channel, []string{})
|
base := RplNamReply(reply.channel, []string{})
|
||||||
baseLen := len(base.FormatString(client))
|
baseLen := len(base.FormatString(client))
|
||||||
tooLong := func(names []string) bool {
|
tooLong := func(names []string) bool {
|
||||||
@ -103,16 +104,17 @@ func (reply *NamesReply) Format(client *Client, write chan<- string) {
|
|||||||
nicks := reply.channel.Nicks()
|
nicks := reply.channel.Nicks()
|
||||||
for to < len(nicks) {
|
for to < len(nicks) {
|
||||||
if (from < (to - 1)) && tooLong(nicks[from:to]) {
|
if (from < (to - 1)) && tooLong(nicks[from:to]) {
|
||||||
RplNamReply(reply.channel, nicks[from:to-1]).Format(client, write)
|
lines = append(lines, RplNamReply(reply.channel, nicks[from:to-1]).Format(client)...)
|
||||||
from, to = to-1, to
|
from, to = to-1, to
|
||||||
} else {
|
} else {
|
||||||
to += 1
|
to += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if from < len(nicks) {
|
if from < len(nicks) {
|
||||||
RplNamReply(reply.channel, nicks[from:]).Format(client, write)
|
lines = append(lines, RplNamReply(reply.channel, nicks[from:]).Format(client)...)
|
||||||
}
|
}
|
||||||
RplEndOfNames(reply.channel).Format(client, write)
|
lines = append(lines, RplEndOfNames(reply.channel).Format(client)...)
|
||||||
|
return lines
|
||||||
}
|
}
|
||||||
|
|
||||||
func (reply *NamesReply) String() string {
|
func (reply *NamesReply) String() string {
|
||||||
|
@ -112,7 +112,7 @@ type Replier interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Reply interface {
|
type Reply interface {
|
||||||
Format(*Client, chan<- string)
|
Format(*Client) []string
|
||||||
Source() Identifier
|
Source() Identifier
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user