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,6 +248,9 @@ func (conn *Conn) rateLimit(chars int64) {
} }
func (conn *Conn) shutdown() { func (conn *Conn) shutdown() {
// Guard against double-call of shutdown() if we get an error in send()
// as calling sock.Close() will cause recv() to recieve EOF in readstring()
if conn.connected {
conn.connected = false conn.connected = false
conn.sock.Close() conn.sock.Close()
conn.cSend <- true conn.cSend <- true
@ -255,6 +259,7 @@ func (conn *Conn) shutdown() {
// reinit datastructures ready for next connection // reinit datastructures ready for next connection
// do this here rather than after runLoop()'s for due to race // do this here rather than after runLoop()'s for due to race
conn.initialise() 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] + "_")
} }
} }