nmdcs support

This commit is contained in:
mappu 2016-04-02 12:51:23 +13:00
parent 4fad66857a
commit 59ca9f8251

View File

@ -2,17 +2,30 @@
package libnmdc package libnmdc
import ( import (
"crypto/tls"
"fmt" "fmt"
"net" "net"
"net/url"
"regexp" "regexp"
"strings" "strings"
"time" "time"
) )
type ConnectionState int type ConnectionState int
type HubAddress string
type HubEventType int type HubEventType int
type HubAddress string
func (this *HubAddress) IsSecure() bool {
parsed, _ := url.Parse(strings.ToLower(string(*this)))
return parsed.Scheme == "nmdcs" || parsed.Scheme == "dchubs"
}
func (this *HubAddress) GetHostOnly() string {
parsed, _ := url.Parse(strings.ToLower(string(*this)))
return parsed.Host
}
const ( const (
CONNECTIONSTATE_DISCONNECTED = 1 CONNECTIONSTATE_DISCONNECTED = 1
CONNECTIONSTATE_CONNECTING = 2 // Handshake in progress CONNECTIONSTATE_CONNECTING = 2 // Handshake in progress
@ -283,13 +296,19 @@ func (this *HubConnection) processProtocolMessage(message string) {
func (this *HubConnection) worker() { func (this *HubConnection) worker() {
var fullBuffer string var fullBuffer string
var err error = nil
for { for {
// If we're not connected, attempt reconnect // If we're not connected, attempt reconnect
if this.conn == nil { if this.conn == nil {
tmp, err := net.Dial("tcp", string(this.Hco.Address))
this.conn = tmp if this.Hco.Address.IsSecure() {
this.conn, err = tls.Dial("tcp", this.Hco.Address.GetHostOnly(), nil)
} else {
this.conn, err = net.Dial("tcp", this.Hco.Address.GetHostOnly())
}
if err == nil { if err == nil {
this.OnEvent <- HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTING} this.OnEvent <- HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTING}
} }