diff --git a/src/libnmdc/libnmdc.go b/src/libnmdc/libnmdc.go index 029c4d2..c74f907 100644 --- a/src/libnmdc/libnmdc.go +++ b/src/libnmdc/libnmdc.go @@ -7,7 +7,6 @@ import ( "fmt" "net" "net/url" - "reflect" "regexp" "strings" "time" @@ -97,20 +96,10 @@ type HubConnection struct { // Private state conn net.Conn // this is an interface + connValid bool sentOurHello bool } -func isNil(a interface{}) bool { - // Workaround to catch the potential for storing nil in an interface. - // @ref http://stackoverflow.com/q/13476349 - defer func() { recover() }() - return a == nil || reflect.ValueOf(a).IsNil() -} - -func (this *HubConnection) isConnValid() bool { - return isNil(this.conn) -} - type HubEvent struct { EventType HubEventType Nick string @@ -174,7 +163,7 @@ func (this *HubConnection) userJoined_Full(uinf *UserInfo) { // Note that protocol messages are transmitted on the caller thread, not from // any internal libnmdc thread. func (this *HubConnection) SayRaw(protocolCommand string) error { - if this.isConnValid() { + if this.connValid { _, err := this.conn.Write([]byte(protocolCommand)) return err } else { @@ -354,12 +343,14 @@ func (this *HubConnection) worker() { if err == nil { this.OnEvent <- HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTING} - this.conn = nil // WARNING: storing nil in an interface(!) + this.connValid = false + } else { + this.connValid = true } } // Read from socket into our local buffer (blocking) - if this.isConnValid() { + if this.connValid { readBuff := make([]byte, 4096) nbytes, err = this.conn.Read(readBuff) if nbytes > 0 {