Merge pull request #33 from lucron/master

Fix for reconnect and connection errors
This commit is contained in:
Thomas Jager 2014-06-24 10:08:50 +02:00
commit a4ab35198c

10
irc.go
View File

@ -171,11 +171,10 @@ func (irc *Connection) Loop() {
if irc.stopped { if irc.stopped {
break break
} }
irc.Log.Printf("Error: %s\n", err) irc.Log.Printf("Error, disconnected: %s\n", err)
irc.Disconnect()
for !irc.stopped { for !irc.stopped {
if err = irc.Connect(irc.server); err != nil { if err = irc.Reconnect(); err != nil {
irc.Log.Printf("Error: %s\n", err) irc.Log.Printf("Error while reconnecting: %s\n", err)
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} else { } else {
break break
@ -296,6 +295,7 @@ func (irc *Connection) Disconnect() {
// Reconnect to a server using the current connection. // Reconnect to a server using the current connection.
func (irc *Connection) Reconnect() error { func (irc *Connection) Reconnect() error {
irc.end = make(chan struct{})
return irc.Connect(irc.server) return irc.Connect(irc.server)
} }
@ -353,12 +353,10 @@ func (irc *Connection) Connect(server string) error {
irc.pread = make(chan string, 10) irc.pread = make(chan string, 10)
irc.pwrite = make(chan string, 10) irc.pwrite = make(chan string, 10)
irc.Error = make(chan error, 2) irc.Error = make(chan error, 2)
irc.Add(3) irc.Add(3)
go irc.readLoop() go irc.readLoop()
go irc.writeLoop() go irc.writeLoop()
go irc.pingLoop() 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)
} }