Use connected bool properly to ensure shutdown() can't be called twice.

This commit is contained in:
Alex Bramley 2011-07-22 01:17:35 +01:00
parent 291132cab5
commit 6634869fe6
2 changed files with 14 additions and 10 deletions

View File

@ -139,6 +139,7 @@ func (conn *Conn) Connect(host string, pass ...string) os.Error {
bufio.NewReader(conn.sock), bufio.NewReader(conn.sock),
bufio.NewWriter(conn.sock)) bufio.NewWriter(conn.sock))
conn.sock.SetTimeout(conn.Timeout * 1e9) conn.sock.SetTimeout(conn.Timeout * 1e9)
conn.connected = true
go conn.send() go conn.send()
go conn.recv() go conn.recv()
@ -247,14 +248,18 @@ func (conn *Conn) rateLimit(chars int64) {
} }
func (conn *Conn) shutdown() { func (conn *Conn) shutdown() {
conn.connected = false // Guard against double-call of shutdown() if we get an error in send()
conn.sock.Close() // as calling sock.Close() will cause recv() to recieve EOF in readstring()
conn.cSend <- true if conn.connected {
conn.cLoop <- true conn.connected = false
conn.dispatchEvent(&Line{Cmd: "DISCONNECTED"}) conn.sock.Close()
// reinit datastructures ready for next connection conn.cSend <- true
// do this here rather than after runLoop()'s for due to race conn.cLoop <- true
conn.initialise() conn.dispatchEvent(&Line{Cmd: "DISCONNECTED"})
// reinit datastructures ready for next connection
// do this here rather than after runLoop()'s for due to race
conn.initialise()
}
} }
// Dumps a load of information about the current state of the connection to a // Dumps a load of information about the current state of the connection to a

View File

@ -76,7 +76,6 @@ func (conn *Conn) h_PING(line *Line) {
// Handler to trigger a "CONNECTED" event on receipt of numeric 001 // Handler to trigger a "CONNECTED" event on receipt of numeric 001
func (conn *Conn) h_001(line *Line) { func (conn *Conn) h_001(line *Line) {
// we're connected! // we're connected!
conn.connected = true
conn.dispatchEvent(&Line{Cmd: "CONNECTED"}) conn.dispatchEvent(&Line{Cmd: "CONNECTED"})
// and we're being given our hostname (from the server's perspective) // and we're being given our hostname (from the server's perspective)
t := line.Args[len(line.Args)-1] t := line.Args[len(line.Args)-1]
@ -103,7 +102,7 @@ func (conn *Conn) h_433(line *Line) {
// if this is happening before we're properly connected (i.e. the nick // if this is happening before we're properly connected (i.e. the nick
// we sent in the initial NICK command is in use) we will not receive // we sent in the initial NICK command is in use) we will not receive
// a NICK message to confirm our change of nick, so ReNick here... // a NICK message to confirm our change of nick, so ReNick here...
if !conn.connected && line.Args[1] == conn.Me.Nick { if line.Args[1] == conn.Me.Nick {
conn.Me.ReNick(line.Args[1] + "_") conn.Me.ReNick(line.Args[1] + "_")
} }
} }