option to disable autoreconnect, move magic to constant

This commit is contained in:
mappu 2016-05-04 19:15:02 +12:00
parent 8e4a45a2b9
commit 43db3a0d64
3 changed files with 21 additions and 11 deletions

View File

@ -21,9 +21,10 @@ type HubConnection struct {
OnEvent chan HubEvent
// Private state
conn net.Conn // this is an interface
connValid bool
sentOurHello bool
conn net.Conn // this is an interface
connValid bool
sentOurHello bool
autoReconnect bool
}
func (this *HubConnection) SayPublic(message string) {
@ -268,8 +269,12 @@ func (this *HubConnection) worker() {
this.connValid = false
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()})
time.Sleep(30 * time.Second) // Wait before reconnect
continue
if this.autoReconnect {
time.Sleep(AUTO_RECONNECT_AFTER) // Wait before reconnect
continue
} else {
return // leave the worker for good
}
}
}

View File

@ -1,10 +1,11 @@
package libnmdc
type HubConnectionOptions struct {
Address HubAddress
SkipVerifyTLS bool // using a negative verb, because bools default to false
Self UserInfo
NickPassword string
Address HubAddress
SkipVerifyTLS bool // using a negative verb, because bools default to false
SkipAutoReconnect bool // as above
Self UserInfo
NickPassword string
// Returning messages in async mode
NumEventsToBuffer uint
@ -23,13 +24,16 @@ func (this *HubConnectionOptions) prepareConnection() *HubConnection {
HubName: DEFAULT_HUB_NAME,
State: CONNECTIONSTATE_DISCONNECTED,
Users: make(map[string]UserInfo),
autoReconnect: !this.SkipAutoReconnect,
}
return &hc
}
// Connects to an NMDC server, and spawns a background goroutine to handle
// protocol messages. Client code should select on all the interface channels.
// protocol messages. Events will be sent by channel to the returned hc.OnEvent,
// the client is responsible for selecting off this.
func (this *HubConnectionOptions) Connect() *HubConnection {
if this.NumEventsToBuffer < 1 {
@ -47,7 +51,7 @@ func (this *HubConnectionOptions) Connect() *HubConnection {
}
// Connects to an NMDC server, and blocks forever to handle protocol messages.
// Client code should supply an event handling function.
// Client code should supply an event handling function as hco.OnEventSync.
func (this *HubConnectionOptions) ConnectSync() {
hc := this.prepareConnection()
hc.worker()

View File

@ -13,6 +13,7 @@ const (
DEFAULT_CLIENT_TAG string = "libnmdc.go"
DEFAULT_HUB_NAME string = "(unknown)"
SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second
AUTO_RECONNECT_AFTER time.Duration = 30 * time.Second
)
var rx_protocolMessage *regexp.Regexp