nmdcs support

This commit is contained in:
mappu 2016-04-02 12:51:23 +13:00
parent 4fad66857a
commit 59ca9f8251
1 changed files with 22 additions and 3 deletions

View File

@ -2,17 +2,30 @@
package libnmdc
import (
"crypto/tls"
"fmt"
"net"
"net/url"
"regexp"
"strings"
"time"
)
type ConnectionState int
type HubAddress string
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 (
CONNECTIONSTATE_DISCONNECTED = 1
CONNECTIONSTATE_CONNECTING = 2 // Handshake in progress
@ -283,13 +296,19 @@ func (this *HubConnection) processProtocolMessage(message string) {
func (this *HubConnection) worker() {
var fullBuffer string
var err error = nil
for {
// If we're not connected, attempt reconnect
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 {
this.OnEvent <- HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTING}
}