libnmdc: use extra variable instead of reflect package
This commit is contained in:
parent
da92afa51e
commit
27c21572b3
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user