diff --git a/AdcProtocol.go b/AdcProtocol.go index 156ef83..18668c9 100644 --- a/AdcProtocol.go +++ b/AdcProtocol.go @@ -8,6 +8,8 @@ func NewAdcProtocol(hc *HubConnection) *AdcProtocol { proto := AdcProtocol{} proto.hc = hc + // TODO send stuff immediately + return &proto } diff --git a/HubConnection.go b/HubConnection.go index 85b670d..a4b7f81 100644 --- a/HubConnection.go +++ b/HubConnection.go @@ -140,27 +140,56 @@ func (this *HubConnection) worker() { if err != nil { this.State = CONNECTIONSTATE_DISCONNECTED this.connValid = false + this.proto = nil } else { this.State = CONNECTIONSTATE_CONNECTING this.connValid = true this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTING}) + if this.Hco.Address.GetProtocol() == HubProtocolNmdc { + this.proto = NewNmdcProtocol(this) + } else if this.Hco.Address.GetProtocol() == HubProtocolAdc { + this.proto = NewAdcProtocol(this) + } else { + this.proto = nil + } + } } // Read from socket into our local buffer (blocking) if this.connValid { + readBuff := make([]byte, 1024) - this.conn.SetReadDeadline(time.Now().Add(SEND_KEEPALIVE_EVERY)) + + if this.proto == nil { + // Haven't determined if this is ADC or NMDC + // If we get data in this interval, it's NMDC; otherwise it's ADC + this.conn.SetReadDeadline(time.Now().Add(AUTODETECT_ADC_NMDC_TIMEOUT)) + } else { + // Normal + this.conn.SetReadDeadline(time.Now().Add(SEND_KEEPALIVE_EVERY)) + } + nbytes, err = this.conn.Read(readBuff) if checkIsNetTimeout(err) { // No data before read deadline err = nil - // Send KA packet - err = this.SayKeepalive() + if this.proto == nil { + // Autodetect: switch to ADC + this.proto = NewAdcProtocol(this) + } else { + // Normal + // Send KA packet + err = this.SayKeepalive() + } + } + + if this.proto == nil { + this.proto = NewNmdcProtocol(this) } if nbytes > 0 { @@ -197,6 +226,7 @@ func (this *HubConnection) worker() { this.State = CONNECTIONSTATE_DISCONNECTED this.conn = nil this.connValid = false + this.proto = nil this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()}) if this.autoReconnect { diff --git a/libnmdc.go b/libnmdc.go index 972f63f..34c98fb 100644 --- a/libnmdc.go +++ b/libnmdc.go @@ -8,11 +8,12 @@ import ( const ( DEFAULT_CLIENT_TAG string = "libnmdc.go" - DEFAULT_CLIENT_VERSION string = "0.15" + DEFAULT_CLIENT_VERSION string = "0.16" DEFAULT_HUB_NAME string = "(unknown)" SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second AUTO_RECONNECT_AFTER time.Duration = 30 * time.Second RECONNECT_IF_NO_DATA_RECIEVED_IN time.Duration = 24 * time.Hour // we expect keepalives wayyyy more frequently than this + AUTODETECT_ADC_NMDC_TIMEOUT time.Duration = 3 * time.Second ) var ErrNotConnected error = errors.New("Not connected")