From 27c21572b38ee3cb4e326951a66d59268f632d6d Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 3 Apr 2016 13:22:09 +1200 Subject: [PATCH] libnmdc: use extra variable instead of reflect package --- src/libnmdc/libnmdc.go | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) 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 {