2016-02-12 04:03:42 +00:00
|
|
|
// libnmdc project libnmdc.go
|
|
|
|
package libnmdc
|
|
|
|
|
|
|
|
import (
|
2016-04-01 23:51:23 +00:00
|
|
|
"crypto/tls"
|
2016-04-03 01:15:57 +00:00
|
|
|
"errors"
|
2016-02-12 04:03:42 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2016-04-01 23:51:23 +00:00
|
|
|
"net/url"
|
2016-02-12 04:03:42 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-04-03 06:53:14 +00:00
|
|
|
const (
|
2016-04-04 07:01:47 +00:00
|
|
|
DEFAULT_CLIENT_TAG string = "libnmdc.go"
|
|
|
|
DEFAULT_HUB_NAME string = "(unknown)"
|
|
|
|
SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second
|
2016-04-03 06:53:14 +00:00
|
|
|
)
|
|
|
|
|
2016-02-12 04:03:42 +00:00
|
|
|
type ConnectionState int
|
|
|
|
type HubEventType int
|
|
|
|
|
2016-04-01 23:51:23 +00:00
|
|
|
type HubAddress string
|
|
|
|
|
2016-04-02 00:49:24 +00:00
|
|
|
func (this *HubAddress) parse() url.URL {
|
|
|
|
parsed, err := url.Parse(strings.ToLower(string(*this)))
|
|
|
|
if err != nil || len(parsed.Host) == 0 {
|
|
|
|
parsed = &url.URL{
|
|
|
|
Scheme: "nmdc",
|
|
|
|
Host: string(*this),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add default port if not specified
|
|
|
|
if !strings.ContainsRune(parsed.Host, ':') {
|
|
|
|
parsed.Host = parsed.Host + ":411"
|
|
|
|
}
|
|
|
|
|
|
|
|
return *parsed
|
|
|
|
}
|
|
|
|
|
2016-04-01 23:51:23 +00:00
|
|
|
func (this *HubAddress) IsSecure() bool {
|
2016-04-02 00:49:24 +00:00
|
|
|
parsed := this.parse()
|
|
|
|
|
2016-04-01 23:51:23 +00:00
|
|
|
return parsed.Scheme == "nmdcs" || parsed.Scheme == "dchubs"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HubAddress) GetHostOnly() string {
|
2016-04-02 00:49:24 +00:00
|
|
|
return this.parse().Host
|
2016-04-01 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 04:03:42 +00:00
|
|
|
const (
|
|
|
|
CONNECTIONSTATE_DISCONNECTED = 1
|
|
|
|
CONNECTIONSTATE_CONNECTING = 2 // Handshake in progress
|
|
|
|
CONNECTIONSTATE_CONNECTED = 3
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
EVENT_PUBLIC = 1
|
|
|
|
EVENT_PRIVATE = 2
|
|
|
|
EVENT_SYSTEM_MESSAGE_FROM_HUB = 3
|
|
|
|
EVENT_SYSTEM_MESSAGE_FROM_CONN = 4
|
|
|
|
EVENT_USER_JOINED = 5
|
|
|
|
EVENT_USER_PART = 6
|
|
|
|
EVENT_USER_UPDATED_INFO = 7
|
|
|
|
EVENT_CONNECTION_STATE_CHANGED = 8
|
|
|
|
EVENT_HUBNAME_CHANGED = 9
|
|
|
|
EVENT_DEBUG_MESSAGE = 10
|
|
|
|
)
|
|
|
|
|
|
|
|
var rx_protocolMessage *regexp.Regexp
|
|
|
|
var rx_publicChat *regexp.Regexp
|
|
|
|
var rx_incomingTo *regexp.Regexp
|
2016-04-03 01:15:57 +00:00
|
|
|
var ErrNotConnected error = errors.New("Not connected")
|
2016-02-12 04:03:42 +00:00
|
|
|
|
|
|
|
func init() {
|
2016-04-03 07:20:58 +00:00
|
|
|
rx_protocolMessage = regexp.MustCompile("(?ms)^[^|]*\\|")
|
2016-02-12 04:03:42 +00:00
|
|
|
rx_publicChat = regexp.MustCompile("(?ms)^<([^>]*)> (.*)$")
|
2016-04-02 01:01:58 +00:00
|
|
|
rx_incomingTo = regexp.MustCompile("(?ms)^([^ ]+) From: ([^ ]+) \\$<([^>]*)> (.*)")
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HubConnectionOptions struct {
|
2016-04-03 06:52:19 +00:00
|
|
|
Address HubAddress
|
|
|
|
SkipVerifyTLS bool // using a negative verb, because bools default to false
|
|
|
|
Self UserInfo
|
|
|
|
NickPassword string
|
|
|
|
|
|
|
|
// Returning messages in async mode
|
2016-02-12 04:03:42 +00:00
|
|
|
NumEventsToBuffer uint
|
2016-04-03 06:52:19 +00:00
|
|
|
|
|
|
|
// Returning messages in sync mode
|
|
|
|
OnEventSync func(HubEvent)
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HubConnection struct {
|
|
|
|
// Supplied parameters
|
|
|
|
Hco *HubConnectionOptions
|
|
|
|
|
|
|
|
// Current remote status
|
|
|
|
HubName string
|
|
|
|
State ConnectionState
|
|
|
|
Users map[string]UserInfo
|
|
|
|
|
|
|
|
// Streamed events
|
2016-04-03 06:52:19 +00:00
|
|
|
processEvent func(HubEvent)
|
2016-04-03 06:59:11 +00:00
|
|
|
OnEvent chan HubEvent
|
2016-02-12 04:03:42 +00:00
|
|
|
|
|
|
|
// Private state
|
2016-04-03 01:15:57 +00:00
|
|
|
conn net.Conn // this is an interface
|
2016-04-03 01:22:09 +00:00
|
|
|
connValid bool
|
2016-02-12 04:03:42 +00:00
|
|
|
sentOurHello bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type HubEvent struct {
|
|
|
|
EventType HubEventType
|
|
|
|
Nick string
|
|
|
|
Message string
|
|
|
|
StateChange ConnectionState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs ConnectionState) Format() string {
|
|
|
|
switch cs {
|
|
|
|
case CONNECTIONSTATE_DISCONNECTED:
|
|
|
|
return "Disconnected"
|
|
|
|
case CONNECTIONSTATE_CONNECTING:
|
|
|
|
return "Connecting"
|
|
|
|
case CONNECTIONSTATE_CONNECTED:
|
|
|
|
return "Connected"
|
|
|
|
default:
|
|
|
|
return "?"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NMDCUnescape(encoded string) string {
|
|
|
|
v1 := strings.Replace(encoded, "$", "$", -1)
|
|
|
|
v2 := strings.Replace(v1, "|", "|", -1)
|
|
|
|
return strings.Replace(v2, "&", "&", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NMDCEscape(plaintext string) string {
|
|
|
|
v1 := strings.Replace(plaintext, "&", "&", -1)
|
|
|
|
v2 := strings.Replace(v1, "|", "|", -1)
|
|
|
|
return strings.Replace(v2, "$", "$", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HubConnection) SayPublic(message string) {
|
|
|
|
this.SayRaw("<" + this.Hco.Self.Nick + "> " + NMDCEscape(message) + "|")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HubConnection) SayPrivate(recipient string, message string) {
|
|
|
|
this.SayRaw("$To: " + recipient + " From: " + this.Hco.Self.Nick + " $<" + this.Hco.Self.Nick + "> " + NMDCEscape(message) + "|")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HubConnection) SayInfo() {
|
|
|
|
this.SayRaw(this.Hco.Self.toMyINFO() + "|")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HubConnection) userJoined_NameOnly(nick string) {
|
|
|
|
_, already_existed := this.Users[nick]
|
|
|
|
if !already_existed {
|
|
|
|
this.Users[nick] = *NewUserInfo(nick)
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_USER_JOINED, Nick: nick})
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HubConnection) userJoined_Full(uinf *UserInfo) {
|
|
|
|
_, already_existed := this.Users[uinf.Nick]
|
|
|
|
if !already_existed {
|
|
|
|
this.Users[uinf.Nick] = *uinf
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_USER_JOINED, Nick: uinf.Nick})
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that protocol messages are transmitted on the caller thread, not from
|
|
|
|
// any internal libnmdc thread.
|
|
|
|
func (this *HubConnection) SayRaw(protocolCommand string) error {
|
2016-04-03 01:22:09 +00:00
|
|
|
if this.connValid {
|
2016-04-03 01:15:57 +00:00
|
|
|
_, err := this.conn.Write([]byte(protocolCommand))
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
return ErrNotConnected
|
|
|
|
}
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseLock(lock []byte) string {
|
|
|
|
|
|
|
|
nibble_swap := func(b byte) byte {
|
|
|
|
return ((b << 4) & 0xF0) | ((b >> 4) & 0x0F)
|
|
|
|
}
|
|
|
|
|
|
|
|
chr := func(b byte) string {
|
|
|
|
if b == 0 || b == 5 || b == 36 || b == 96 || b == 124 || b == 126 {
|
|
|
|
return fmt.Sprintf("/%%DCN%04d%%/", b)
|
|
|
|
} else {
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
key := chr(nibble_swap(lock[0] ^ lock[len(lock)-2] ^ lock[len(lock)-3] ^ 5))
|
|
|
|
for i := 1; i < len(lock); i += 1 {
|
|
|
|
key += chr(nibble_swap(lock[i] ^ lock[i-1]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HubConnection) processProtocolMessage(message string) {
|
|
|
|
|
|
|
|
// Zero-length protocol message
|
|
|
|
// ````````````````````````````
|
|
|
|
if len(message) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public chat
|
|
|
|
// ```````````
|
|
|
|
if rx_publicChat.MatchString(message) {
|
|
|
|
pubchat_parts := rx_publicChat.FindStringSubmatch(message)
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_PUBLIC, Nick: pubchat_parts[1], Message: NMDCUnescape(pubchat_parts[2])})
|
2016-02-12 04:03:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// System messages
|
|
|
|
// ```````````````
|
|
|
|
if message[0] != '$' {
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_HUB, Nick: this.HubName, Message: NMDCUnescape(message)})
|
2016-02-12 04:03:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Protocol messages
|
|
|
|
// `````````````````
|
|
|
|
|
|
|
|
commandParts := strings.SplitN(message, " ", 2)
|
|
|
|
switch commandParts[0] {
|
|
|
|
|
|
|
|
case "$Lock":
|
|
|
|
this.SayRaw("$Supports NoGetINFO UserCommand UserIP2|" +
|
|
|
|
"$Key " + parseLock([]byte(commandParts[1])) + "|" +
|
|
|
|
"$ValidateNick " + NMDCEscape(this.Hco.Self.Nick) + "|")
|
|
|
|
this.sentOurHello = false
|
|
|
|
|
|
|
|
case "$Hello":
|
|
|
|
if commandParts[1] == this.Hco.Self.Nick && !this.sentOurHello {
|
|
|
|
this.SayRaw("$Version 1,0091|")
|
|
|
|
this.SayRaw("$GetNickList|")
|
|
|
|
this.SayInfo()
|
|
|
|
this.sentOurHello = true
|
|
|
|
|
|
|
|
} else {
|
|
|
|
this.userJoined_NameOnly(commandParts[1])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case "$HubName":
|
|
|
|
this.HubName = commandParts[1]
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_HUBNAME_CHANGED, Nick: commandParts[1]})
|
2016-02-12 04:03:42 +00:00
|
|
|
|
|
|
|
case "$ValidateDenide": // sic
|
|
|
|
if len(this.Hco.NickPassword) > 0 {
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "Incorrect password."})
|
2016-02-12 04:03:42 +00:00
|
|
|
} else {
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "Nick already in use."})
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "$HubIsFull":
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "Hub is full."})
|
2016-02-12 04:03:42 +00:00
|
|
|
|
|
|
|
case "$BadPass":
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_SYSTEM_MESSAGE_FROM_CONN, Message: "Incorrect password."})
|
2016-02-12 04:03:42 +00:00
|
|
|
|
|
|
|
case "$GetPass":
|
|
|
|
this.SayRaw("$MyPass " + NMDCEscape(this.Hco.NickPassword) + "|")
|
|
|
|
|
|
|
|
case "$Quit":
|
|
|
|
delete(this.Users, commandParts[1])
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_USER_PART, Nick: commandParts[1]})
|
2016-02-12 04:03:42 +00:00
|
|
|
|
|
|
|
case "$MyINFO":
|
|
|
|
u := UserInfo{}
|
|
|
|
err := u.fromMyINFO(commandParts[1])
|
|
|
|
if err == nil {
|
|
|
|
this.userJoined_Full(&u)
|
|
|
|
} else {
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: err.Error()})
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "$NickList":
|
|
|
|
nicklist := strings.Split(commandParts[1], "$$")
|
|
|
|
for _, nick := range nicklist {
|
|
|
|
if len(nick) > 0 {
|
|
|
|
this.userJoined_NameOnly(nick)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 01:01:58 +00:00
|
|
|
case "$To:":
|
|
|
|
valid := false
|
2016-02-12 04:03:42 +00:00
|
|
|
if rx_incomingTo.MatchString(commandParts[1]) {
|
|
|
|
txparts := rx_incomingTo.FindStringSubmatch(commandParts[1])
|
2016-04-02 01:01:58 +00:00
|
|
|
if txparts[1] == this.Hco.Self.Nick && txparts[2] == txparts[3] {
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_PRIVATE, Nick: txparts[2], Message: txparts[4]})
|
2016-04-02 01:01:58 +00:00
|
|
|
valid = true
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-02 01:01:58 +00:00
|
|
|
|
|
|
|
if !valid {
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Malformed private message '" + commandParts[1] + "'"})
|
2016-04-02 01:01:58 +00:00
|
|
|
}
|
2016-02-12 04:03:42 +00:00
|
|
|
|
|
|
|
case "$UserIP":
|
|
|
|
// Final message in PtokaX connection handshake - trigger connection callback.
|
|
|
|
// This might not be the case for other hubsofts, though
|
|
|
|
if this.State != CONNECTIONSTATE_CONNECTED {
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTED})
|
2016-02-12 04:03:42 +00:00
|
|
|
this.State = CONNECTIONSTATE_CONNECTED
|
|
|
|
}
|
|
|
|
|
|
|
|
case "$ForceMove":
|
2016-04-04 07:01:55 +00:00
|
|
|
this.Hco.Address = HubAddress(commandParts[1])
|
|
|
|
this.conn.Close() // we'll reconnect onto the new address
|
2016-02-12 04:03:42 +00:00
|
|
|
|
|
|
|
// IGNORABLE COMMANDS
|
|
|
|
case "$Supports":
|
2016-04-04 07:01:55 +00:00
|
|
|
case "$UserCommand": // TODO $UserCommand 1 1 Group chat\New group chat$<%[mynick]> !groupchat_new||
|
2016-02-12 04:03:42 +00:00
|
|
|
case "$UserList":
|
|
|
|
case "$OpList":
|
|
|
|
case "$HubTopic":
|
|
|
|
case "$Search":
|
|
|
|
case "$ConnectToMe":
|
|
|
|
|
|
|
|
default:
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Unhandled protocol command '" + commandParts[0] + "'"})
|
2016-02-12 04:03:42 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-16 06:27:14 +00:00
|
|
|
func CheckIsNetTimeout(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch err.(type) {
|
|
|
|
case net.Error:
|
|
|
|
return err.(net.Error).Timeout()
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 04:03:42 +00:00
|
|
|
func (this *HubConnection) worker() {
|
|
|
|
var fullBuffer string
|
2016-04-01 23:51:23 +00:00
|
|
|
var err error = nil
|
2016-04-02 00:49:38 +00:00
|
|
|
var nbytes int = 0
|
2016-02-12 04:03:42 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
// If we're not connected, attempt reconnect
|
|
|
|
if this.conn == nil {
|
2016-04-01 23:51:23 +00:00
|
|
|
|
2016-04-03 06:36:46 +00:00
|
|
|
fullBuffer = "" // clear
|
|
|
|
|
2016-04-01 23:51:23 +00:00
|
|
|
if this.Hco.Address.IsSecure() {
|
2016-04-03 01:17:29 +00:00
|
|
|
this.conn, err = tls.Dial("tcp", this.Hco.Address.GetHostOnly(), &tls.Config{
|
|
|
|
InsecureSkipVerify: this.Hco.SkipVerifyTLS,
|
|
|
|
})
|
2016-04-01 23:51:23 +00:00
|
|
|
} else {
|
|
|
|
this.conn, err = net.Dial("tcp", this.Hco.Address.GetHostOnly())
|
|
|
|
}
|
|
|
|
|
2016-04-03 01:29:34 +00:00
|
|
|
if err != nil {
|
2016-04-03 06:35:05 +00:00
|
|
|
this.State = CONNECTIONSTATE_DISCONNECTED
|
2016-04-03 01:22:09 +00:00
|
|
|
this.connValid = false
|
2016-04-03 06:35:05 +00:00
|
|
|
|
2016-04-03 01:22:09 +00:00
|
|
|
} else {
|
2016-04-03 06:35:05 +00:00
|
|
|
this.State = CONNECTIONSTATE_CONNECTING
|
2016-04-03 01:22:09 +00:00
|
|
|
this.connValid = true
|
2016-04-03 06:52:19 +00:00
|
|
|
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTING})
|
2016-04-03 06:35:05 +00:00
|
|
|
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read from socket into our local buffer (blocking)
|
2016-04-03 01:22:09 +00:00
|
|
|
if this.connValid {
|
2016-04-03 07:20:58 +00:00
|
|
|
readBuff := make([]byte, 1024)
|
2016-04-04 07:01:47 +00:00
|
|
|
this.conn.SetReadDeadline(time.Now().Add(SEND_KEEPALIVE_EVERY))
|
2016-04-02 00:49:38 +00:00
|
|
|
nbytes, err = this.conn.Read(readBuff)
|
2016-04-04 06:57:50 +00:00
|
|
|
|
2016-04-16 06:27:14 +00:00
|
|
|
if CheckIsNetTimeout(err) {
|
2016-04-04 06:57:50 +00:00
|
|
|
// No data before read deadline
|
|
|
|
err = nil
|
|
|
|
|
|
|
|
// Send KA packet
|
|
|
|
_, err = this.conn.Write([]byte("|"))
|
|
|
|
}
|
|
|
|
|
2016-04-02 00:49:38 +00:00
|
|
|
if nbytes > 0 {
|
|
|
|
fullBuffer += string(readBuff[0:nbytes])
|
|
|
|
}
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to parse a message block
|
|
|
|
for len(fullBuffer) > 0 {
|
|
|
|
for len(fullBuffer) > 0 && fullBuffer[0] == '|' {
|
|
|
|
fullBuffer = fullBuffer[1:]
|
|
|
|
}
|
|
|
|
protocolMessage := rx_protocolMessage.FindString(fullBuffer)
|
|
|
|
if len(protocolMessage) > 0 {
|
2016-04-03 07:20:58 +00:00
|
|
|
this.processProtocolMessage(protocolMessage[:len(protocolMessage)-1])
|
2016-02-12 04:03:42 +00:00
|
|
|
fullBuffer = fullBuffer[len(protocolMessage):]
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 06:54:56 +00:00
|
|
|
// Maybe we disconnected
|
|
|
|
// Perform this check *last*, to ensure we've had a final shot at
|
|
|
|
// clearing out any queued messages
|
|
|
|
if err != nil {
|
|
|
|
this.State = CONNECTIONSTATE_DISCONNECTED
|
|
|
|
this.conn = nil
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-03 06:52:19 +00:00
|
|
|
func (this *HubConnectionOptions) prepareConnection() *HubConnection {
|
2016-02-12 04:03:42 +00:00
|
|
|
if this.Self.ClientTag == "" {
|
2016-04-03 06:53:14 +00:00
|
|
|
this.Self.ClientTag = DEFAULT_CLIENT_TAG
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hc := HubConnection{
|
2016-04-03 06:38:43 +00:00
|
|
|
Hco: this,
|
2016-04-03 06:53:14 +00:00
|
|
|
HubName: DEFAULT_HUB_NAME,
|
2016-04-03 06:38:43 +00:00
|
|
|
State: CONNECTIONSTATE_DISCONNECTED,
|
|
|
|
Users: make(map[string]UserInfo),
|
2016-04-03 06:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2016-04-03 06:59:11 +00:00
|
|
|
hc.OnEvent = make(chan HubEvent, this.NumEventsToBuffer)
|
2016-04-03 06:52:19 +00:00
|
|
|
hc.processEvent = func(ev HubEvent) {
|
2016-04-03 06:59:11 +00:00
|
|
|
hc.OnEvent <- ev
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go hc.worker()
|
2016-04-03 06:52:19 +00:00
|
|
|
return hc
|
|
|
|
}
|
2016-02-12 04:03:42 +00:00
|
|
|
|
2016-04-03 06:52:19 +00:00
|
|
|
// 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()
|
2016-02-12 04:03:42 +00:00
|
|
|
}
|