55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package libnmdc
|
|
|
|
type HubConnectionOptions struct {
|
|
Address HubAddress
|
|
SkipVerifyTLS bool // using a negative verb, because bools default to false
|
|
Self UserInfo
|
|
NickPassword string
|
|
|
|
// Returning messages in async mode
|
|
NumEventsToBuffer uint
|
|
|
|
// Returning messages in sync mode
|
|
OnEventSync func(HubEvent)
|
|
}
|
|
|
|
func (this *HubConnectionOptions) prepareConnection() *HubConnection {
|
|
if this.Self.ClientTag == "" {
|
|
this.Self.ClientTag = DEFAULT_CLIENT_TAG
|
|
}
|
|
|
|
hc := HubConnection{
|
|
Hco: this,
|
|
HubName: DEFAULT_HUB_NAME,
|
|
State: CONNECTIONSTATE_DISCONNECTED,
|
|
Users: make(map[string]UserInfo),
|
|
}
|
|
|
|
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.
|
|
func (this *HubConnectionOptions) Connect() *HubConnection {
|
|
|
|
if this.NumEventsToBuffer < 1 {
|
|
this.NumEventsToBuffer = 1
|
|
}
|
|
|
|
hc := this.prepareConnection()
|
|
hc.OnEvent = make(chan HubEvent, this.NumEventsToBuffer)
|
|
hc.processEvent = func(ev HubEvent) {
|
|
hc.OnEvent <- ev
|
|
}
|
|
|
|
go hc.worker()
|
|
return hc
|
|
}
|
|
|
|
// Connects to an NMDC server, and blocks forever to handle protocol messages.
|
|
// Client code should supply an event handling function.
|
|
func (this *HubConnectionOptions) ConnectSync() {
|
|
hc := this.prepareConnection()
|
|
hc.worker()
|
|
}
|