From 30980fcbaddeab5a109880e82b47158e8722e621 Mon Sep 17 00:00:00 2001 From: Chance Zibolski Date: Mon, 14 Jul 2014 15:54:53 -0700 Subject: [PATCH 1/3] Retrieve error chan from method. This allows using the error chan in an interface. Also moved the disconnect error into a named variable for checking specific errors. --- irc.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/irc.go b/irc.go index 1fc06fa..84cb6f0 100644 --- a/irc.go +++ b/irc.go @@ -35,11 +35,15 @@ const ( VERSION = "go-ircevent v2.1" ) +var ErrDisconnected = errors.New("Disconnect Called") + // Read data from a connection. To be used as a goroutine. func (irc *Connection) readLoop() { defer irc.Done() br := bufio.NewReaderSize(irc.socket, 512) + errChan := irc.ErrorChan() + for { select { case <-irc.end: @@ -61,7 +65,7 @@ func (irc *Connection) readLoop() { } if err != nil { - irc.Error <- err + errChan <- err break } @@ -103,6 +107,7 @@ func (irc *Connection) readLoop() { // Loop to write to a connection. To be used as a goroutine. func (irc *Connection) writeLoop() { defer irc.Done() + errChan := irc.ErrorChan() for { select { case <-irc.end: @@ -127,7 +132,7 @@ func (irc *Connection) writeLoop() { irc.socket.SetWriteDeadline(zero) if err != nil { - irc.Error <- err + errChan <- err return } } @@ -166,8 +171,9 @@ func (irc *Connection) pingLoop() { // Main loop to control the connection. func (irc *Connection) Loop() { + errChan := irc.ErrorChan() for !irc.stopped { - err := <-irc.Error + err := <-errChan if irc.stopped { break } @@ -276,6 +282,10 @@ func (irc *Connection) Mode(target string, modestring ...string) { irc.SendRawf("MODE %s", target) } +func (irc *Connection) ErrorChan() chan error { + return irc.Error +} + // A disconnect sends all buffered messages (if possible), // stops all goroutines and then closes the socket. func (irc *Connection) Disconnect() { @@ -290,7 +300,7 @@ func (irc *Connection) Disconnect() { irc.netsock.Close() irc.netsock = nil } - irc.Error <- errors.New("Disconnect Called") + irc.ErrorChan() <- ErrDisconnected } // Reconnect to a server using the current connection. From 846881130f18bc10e4c48edd847f3327780fff69 Mon Sep 17 00:00:00 2001 From: Chance Zibolski Date: Mon, 14 Jul 2014 17:05:06 -0700 Subject: [PATCH 2/3] Remove pread --- irc.go | 2 -- irc_struct.go | 8 ++++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/irc.go b/irc.go index 84cb6f0..b29fce8 100644 --- a/irc.go +++ b/irc.go @@ -291,7 +291,6 @@ func (irc *Connection) ErrorChan() chan error { func (irc *Connection) Disconnect() { close(irc.end) close(irc.pwrite) - close(irc.pread) irc.Wait() irc.socket.Close() @@ -360,7 +359,6 @@ func (irc *Connection) Connect(server string) error { } irc.Log.Printf("Connected to %s (%s)\n", irc.server, irc.socket.RemoteAddr()) - irc.pread = make(chan string, 10) irc.pwrite = make(chan string, 10) irc.Error = make(chan error, 2) irc.Add(3) diff --git a/irc_struct.go b/irc_struct.go index a630b43..1e582aa 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -24,10 +24,10 @@ type Connection struct { PingFreq time.Duration KeepAlive time.Duration - socket net.Conn - netsock net.Conn - pread, pwrite chan string - end chan struct{} + socket net.Conn + netsock net.Conn + pwrite chan string + end chan struct{} nick string //The nickname we want. nickcurrent string //The nickname we currently have. From 06fee2df7d48a003b45505f9fdae099252742579 Mon Sep 17 00:00:00 2001 From: Chance Zibolski Date: Mon, 14 Jul 2014 17:05:27 -0700 Subject: [PATCH 3/3] disconnect: Clear callbacks before closing channels --- irc.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/irc.go b/irc.go index b29fce8..306b061 100644 --- a/irc.go +++ b/irc.go @@ -289,6 +289,10 @@ func (irc *Connection) ErrorChan() chan error { // A disconnect sends all buffered messages (if possible), // stops all goroutines and then closes the socket. func (irc *Connection) Disconnect() { + for event := range irc.events { + irc.ClearCallback(event) + } + close(irc.end) close(irc.pwrite)