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

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

View File

@ -3,6 +3,7 @@ package libnmdc
type HubConnectionOptions struct { type HubConnectionOptions struct {
Address HubAddress Address HubAddress
SkipVerifyTLS bool // using a negative verb, because bools default to false SkipVerifyTLS bool // using a negative verb, because bools default to false
SkipAutoReconnect bool // as above
Self UserInfo Self UserInfo
NickPassword string NickPassword string
@ -23,13 +24,16 @@ func (this *HubConnectionOptions) prepareConnection() *HubConnection {
HubName: DEFAULT_HUB_NAME, HubName: DEFAULT_HUB_NAME,
State: CONNECTIONSTATE_DISCONNECTED, State: CONNECTIONSTATE_DISCONNECTED,
Users: make(map[string]UserInfo), Users: make(map[string]UserInfo),
autoReconnect: !this.SkipAutoReconnect,
} }
return &hc return &hc
} }
// Connects to an NMDC server, and spawns a background goroutine to handle // 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 { func (this *HubConnectionOptions) Connect() *HubConnection {
if this.NumEventsToBuffer < 1 { 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. // 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() { func (this *HubConnectionOptions) ConnectSync() {
hc := this.prepareConnection() hc := this.prepareConnection()
hc.worker() hc.worker()

View File

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