protocol autodetection

--HG--
branch : adc
This commit is contained in:
mappu 2017-11-22 19:22:13 +13:00
parent b022c9534a
commit 131ce0a63b
3 changed files with 37 additions and 4 deletions

View File

@ -8,6 +8,8 @@ func NewAdcProtocol(hc *HubConnection) *AdcProtocol {
proto := AdcProtocol{} proto := AdcProtocol{}
proto.hc = hc proto.hc = hc
// TODO send stuff immediately
return &proto return &proto
} }

View File

@ -140,28 +140,57 @@ func (this *HubConnection) worker() {
if err != nil { if err != nil {
this.State = CONNECTIONSTATE_DISCONNECTED this.State = CONNECTIONSTATE_DISCONNECTED
this.connValid = false this.connValid = false
this.proto = nil
} else { } else {
this.State = CONNECTIONSTATE_CONNECTING this.State = CONNECTIONSTATE_CONNECTING
this.connValid = true this.connValid = true
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTING}) 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) // Read from socket into our local buffer (blocking)
if this.connValid { if this.connValid {
readBuff := make([]byte, 1024) readBuff := make([]byte, 1024)
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)) this.conn.SetReadDeadline(time.Now().Add(SEND_KEEPALIVE_EVERY))
}
nbytes, err = this.conn.Read(readBuff) nbytes, err = this.conn.Read(readBuff)
if checkIsNetTimeout(err) { if checkIsNetTimeout(err) {
// No data before read deadline // No data before read deadline
err = nil err = nil
if this.proto == nil {
// Autodetect: switch to ADC
this.proto = NewAdcProtocol(this)
} else {
// Normal
// Send KA packet // Send KA packet
err = this.SayKeepalive() err = this.SayKeepalive()
} }
}
if this.proto == nil {
this.proto = NewNmdcProtocol(this)
}
if nbytes > 0 { if nbytes > 0 {
this.lastDataRecieved = time.Now() this.lastDataRecieved = time.Now()
@ -197,6 +226,7 @@ func (this *HubConnection) worker() {
this.State = CONNECTIONSTATE_DISCONNECTED this.State = CONNECTIONSTATE_DISCONNECTED
this.conn = nil this.conn = nil
this.connValid = false this.connValid = false
this.proto = nil
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()}) this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()})
if this.autoReconnect { if this.autoReconnect {

View File

@ -8,11 +8,12 @@ import (
const ( const (
DEFAULT_CLIENT_TAG string = "libnmdc.go" DEFAULT_CLIENT_TAG string = "libnmdc.go"
DEFAULT_CLIENT_VERSION string = "0.15" DEFAULT_CLIENT_VERSION string = "0.16"
DEFAULT_HUB_NAME string = "(unknown)" DEFAULT_HUB_NAME string = "(unknown)"
SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second
AUTO_RECONNECT_AFTER time.Duration = 30 * 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 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") var ErrNotConnected error = errors.New("Not connected")