Refactor reader/writer/pinger

The old code relied on a global bool for reconnect synchronization; this
would cause the reader/writer to stop (and not restart) on every other
`IRCConnection` when one was `Reconnect`d. This patch renames the
methods to indicate they loop, makes them methods on `IRCConnection`,
and uses a member of `IRCConnection` for synchronization rather than a
global.
This commit is contained in:
lye 2012-03-21 23:08:21 -05:00
parent b06c2dfa4a
commit f2faa10846
2 changed files with 21 additions and 15 deletions

View File

@ -17,12 +17,10 @@ const (
VERSION = "cleanirc v1.0" VERSION = "cleanirc v1.0"
) )
var error_ bool func (irc *IRCConnection) readLoop() {
func reader(irc *IRCConnection) {
br := bufio.NewReader(irc.socket) br := bufio.NewReader(irc.socket)
for !error_ { for !irc.reconnecting {
msg, err := br.ReadString('\n') msg, err := br.ReadString('\n')
if err != nil { if err != nil {
irc.Error <-err irc.Error <-err
@ -37,9 +35,11 @@ func reader(irc *IRCConnection) {
if i := strings.Index(msg, " "); i > -1 { if i := strings.Index(msg, " "); i > -1 {
event.Source = msg[1:i] event.Source = msg[1:i]
msg = msg[i+1 : len(msg)] msg = msg[i+1 : len(msg)]
} else { } else {
irc.log(fmt.Sprintf("Misformed msg from server: %#s\n", msg)) irc.log(fmt.Sprintf("Misformed msg from server: %#s\n", msg))
} }
if i, j := strings.Index(event.Source, "!"), strings.Index(event.Source, "@"); i > -1 && j > -1 { if i, j := strings.Index(event.Source, "!"), strings.Index(event.Source, "@"); i > -1 && j > -1 {
event.Nick = event.Source[0:i] event.Nick = event.Source[0:i]
event.User = event.Source[i+1 : j] event.User = event.Source[i+1 : j]
@ -54,9 +54,11 @@ func reader(irc *IRCConnection) {
args = strings.Split(args[0], " ") args = strings.Split(args[0], " ")
event.Code = strings.ToUpper(args[0]) event.Code = strings.ToUpper(args[0])
if len(args) > 1 { if len(args) > 1 {
event.Arguments = args[1:len(args)] event.Arguments = args[1:len(args)]
} }
/* XXX: len(args) == 0: args should be empty */
irc.RunCallbacks(event) irc.RunCallbacks(event)
} }
@ -64,10 +66,10 @@ func reader(irc *IRCConnection) {
irc.syncreader <-true irc.syncreader <-true
} }
func writer(irc *IRCConnection) { func (irc *IRCConnection) writeLoop() {
b, ok := <-irc.pwrite b, ok := <-irc.pwrite
for !error_ && ok { for !irc.reconnecting && ok {
if b == "" || irc.socket == nil { if b == "" || irc.socket == nil {
break break
} }
@ -84,14 +86,14 @@ func writer(irc *IRCConnection) {
} }
//Pings the server if we have not recived any messages for 5 minutes //Pings the server if we have not recived any messages for 5 minutes
func pinger(irc *IRCConnection) { func (irc *IRCConnection) pingLoop() {
irc.ticker = time.Tick(1 * time.Minute) //Tick every minute. irc.ticker = time.Tick(1 * time.Minute) //Tick every minute.
irc.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes. irc.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes.
for { for {
select { select {
case <-irc.ticker: case <-irc.ticker:
//Ping if we haven't recived anything from the server within 4 minutes //Ping if we haven't received anything from the server within 4 minutes
if time.Since(irc.lastMessage) >= (4 * time.Minute) { if time.Since(irc.lastMessage) >= (4 * time.Minute) {
irc.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano())) irc.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
} }
@ -141,6 +143,8 @@ func (irc *IRCConnection) SendRaw(message string) {
} }
func (irc *IRCConnection) Reconnect() error { func (irc *IRCConnection) Reconnect() error {
irc.reconnecting = true
close(irc.pwrite) close(irc.pwrite)
close(irc.pread) close(irc.pread)
@ -159,12 +163,12 @@ func (irc *IRCConnection) Reconnect() error {
irc.log(fmt.Sprintf("Error: %s\n", err)) irc.log(fmt.Sprintf("Error: %s\n", err))
} }
error_ = false irc.reconnecting = false
irc.log(fmt.Sprintf("Connected to %s (%s)\n", irc.server, irc.socket.RemoteAddr())) irc.log(fmt.Sprintf("Connected to %s (%s)\n", irc.server, irc.socket.RemoteAddr()))
go reader(irc) go irc.readLoop()
go writer(irc) go irc.writeLoop()
irc.pwrite <-fmt.Sprintf("NICK %s\r\n", irc.nick) irc.pwrite <-fmt.Sprintf("NICK %s\r\n", irc.nick)
irc.pwrite <-fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", irc.user, irc.user) irc.pwrite <-fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", irc.user, irc.user)
@ -181,7 +185,6 @@ func (irc *IRCConnection) Loop() {
} }
irc.log(fmt.Sprintf("Error: %s\n", e)) irc.log(fmt.Sprintf("Error: %s\n", e))
error_ = true
irc.Reconnect() irc.Reconnect()
} }
@ -199,9 +202,9 @@ func (irc *IRCConnection) postConnect() error {
irc.syncreader = make(chan bool) irc.syncreader = make(chan bool)
irc.syncwriter = make(chan bool) irc.syncwriter = make(chan bool)
go reader(irc) go irc.readLoop()
go writer(irc) go irc.writeLoop()
go pinger(irc) go irc.pingLoop()
if len(irc.Password) > 0 { if len(irc.Password) > 0 {
irc.pwrite <-fmt.Sprintf("PASS %s\r\n", irc.Password) irc.pwrite <-fmt.Sprintf("PASS %s\r\n", irc.Password)
@ -248,6 +251,7 @@ func (irc *IRCConnection) log(msg string) {
} }
} }
/* XXX: Change ctor name */
func IRC(nick, user string) *IRCConnection { func IRC(nick, user string) *IRCConnection {
irc := new(IRCConnection) irc := new(IRCConnection)
irc.registered = false irc.registered = false

View File

@ -19,6 +19,8 @@ type IRCConnection struct {
socket net.Conn socket net.Conn
pread, pwrite chan string pread, pwrite chan string
syncreader, syncwriter chan bool syncreader, syncwriter chan bool
reconnecting bool
nick string //The nickname we want. nick string //The nickname we want.
nickcurrent string //The nickname we currently have. nickcurrent string //The nickname we currently have.
user string user string