Break out unboxing code; pass conn/line objects for other events.

This commit is contained in:
Alex Bramley 2011-07-28 00:24:07 +01:00
parent 8e6de2f3c7
commit 303d3724e2
2 changed files with 9 additions and 5 deletions

View File

@ -266,7 +266,7 @@ func (conn *Conn) shutdown() {
conn.sock.Close() conn.sock.Close()
conn.cSend <- true conn.cSend <- true
conn.cLoop <- true conn.cLoop <- true
conn.Dispatcher.Dispatch("disconnected") conn.Dispatcher.Dispatch("disconnected", conn, nil)
// 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()

View File

@ -22,10 +22,14 @@ import (
// strings of digits like "332" (mainly because I really didn't feel like // strings of digits like "332" (mainly because I really didn't feel like
// putting massive constant tables in). // putting massive constant tables in).
func (conn *Conn) AddHandler(name string, f func(*Conn, *Line)) { func (conn *Conn) AddHandler(name string, f func(*Conn, *Line)) {
conn.Registry.AddHandler(name, IRCHandler(f))
}
// Wrap f in an anonymous unboxing function // Wrap f in an anonymous unboxing function
conn.Registry.AddHandler(name, event.NewHandler(func(ev ...interface{}) { func IRCHandler(f func(*Conn, *Line)) event.Handler {
return event.NewHandler(func(ev ...interface{}) {
f(ev[0].(*Conn), ev[1].(*Line)) f(ev[0].(*Conn), ev[1].(*Line))
})) })
} }
// Basic ping/pong handler // Basic ping/pong handler
@ -36,7 +40,7 @@ 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.Dispatcher.Dispatch("connected") conn.Dispatcher.Dispatch("connected", conn, line)
// 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]
if idx := strings.LastIndex(t, " "); idx != -1 { if idx := strings.LastIndex(t, " "); idx != -1 {