diff --git a/csgo/demo_loader.go b/csgo/demo_loader.go index 6cf2f5f..8c4654f 100644 --- a/csgo/demo_loader.go +++ b/csgo/demo_loader.go @@ -23,7 +23,9 @@ import ( ) const ( - APPID = 730 + APPID = 730 + LOGIN_FAILED = iota + LOGIN_SUCCESS ) type DemoMatchLoaderConfig struct { @@ -41,21 +43,22 @@ type DemoMatchLoaderConfig struct { } type DemoMatchLoader struct { - client *steam.Client - GCReady bool - steamLogin *steam.LogOnDetails - matchRecv chan *protobuf.CMsgGCCStrike15V2_MatchList - cmList []*netutil.PortAddr - sentryFile string - loginKey string - db *ent.Client - dp *DemoParser - parseDemo chan *Demo - parseMap map[string]bool - parseMapL *sync.RWMutex - cache *cache.Cache - connecting *sync.Mutex - connectionWait uint64 + client *steam.Client + GCReady bool + steamLogin *steam.LogOnDetails + matchRecv chan *protobuf.CMsgGCCStrike15V2_MatchList + cmList []*netutil.PortAddr + sentryFile string + loginKey string + db *ent.Client + dp *DemoParser + parseDemo chan *Demo + parseMap map[string]bool + parseMapL *sync.RWMutex + cache *cache.Cache + connectionWait uint64 + connectFeedback chan int + LoggedIn bool } func AccountId2SteamId(accId uint32) uint64 { @@ -185,7 +188,6 @@ func (dml *DemoMatchLoader) Setup(config *DemoMatchLoaderConfig) error { dml.parseMap = map[string]bool{} dml.parseMapL = new(sync.RWMutex) dml.cache = config.Cache - dml.connecting = new(sync.Mutex) err := dml.dp.Setup(config.Db, config.Worker, config.SprayTimeout) if err != nil { return err @@ -232,7 +234,7 @@ func (dml *DemoMatchLoader) Setup(config *DemoMatchLoaderConfig) error { return nil } -func (dml DemoMatchLoader) LoadDemo(demo *Demo) error { +func (dml *DemoMatchLoader) LoadDemo(demo *Demo) error { select { case dml.parseDemo <- demo: return nil @@ -241,21 +243,30 @@ func (dml DemoMatchLoader) LoadDemo(demo *Demo) error { } } -func (dml DemoMatchLoader) connectLoop() { - dml.connecting.Lock() - defer dml.connecting.Unlock() - if !dml.client.Connected() { - for dml.connectToSteam() != nil { - log.Infof("[DL] Retrying connecting to steam...") - if dml.connectionWait == 0 { - dml.connectionWait = 1 +func (dml *DemoMatchLoader) connectLoop() { + for { + select { + case res := <-dml.connectFeedback: + switch res { + case LOGIN_FAILED: + time.Sleep(time.Minute * time.Duration(dml.connectionWait)) + if !dml.LoggedIn { + log.Infof("[DL] Retrying connecting to steam...") + + err := dml.connectToSteam() + if err != nil { + log.Warningf("[DL] Error connecting to steam: %v", err) + } + + if dml.connectionWait == 0 { + dml.connectionWait = 1 + } + dml.connectionWait *= 2 + } + case LOGIN_SUCCESS: + log.Infof("[DL] Steam login successfully restored after %d minutes", dml.connectionWait) + dml.connectionWait = 0 } - time.Sleep(time.Minute * time.Duration(dml.connectionWait)) - dml.connectionWait *= 2 - } - if dml.client.Connected() { - log.Infof("[DL] Steam login successfully restored after %d minutes", dml.connectionWait) - dml.connectionWait = 1 } } } @@ -274,6 +285,8 @@ func (dml *DemoMatchLoader) steamEventHandler() { } case *steam.LoggedOnEvent: log.Debug("[DL] Steam login success!") + dml.LoggedIn = true + dml.connectFeedback <- LOGIN_SUCCESS dml.client.Social.SetPersonaState(steamlang.EPersonaState_Online) go dml.setPlaying() case *steam.LogOnFailedEvent: @@ -285,15 +298,16 @@ func (dml *DemoMatchLoader) steamEventHandler() { _ = os.Remove(dml.sentryFile) _ = os.Remove(dml.loginKey) log.Warningf("[DL] Steam login failed: InvalidPassword") - go dml.connectLoop() case steamlang.EResult_InvalidLoginAuthCode: log.Fatalf("[DL] Steam auth code wrong") default: - log.Warningf("[DL] Unhandled login fasiled event %+v", e) + log.Warningf("[DL] Unhandled login failed event %+v", e) } case *steam.DisconnectedEvent: log.Warningf("Steam disconnected, trying to reconnect...") - go dml.connectLoop() + dml.GCReady = false + dml.LoggedIn = false + dml.connectFeedback <- LOGIN_FAILED case *steam.LoginKeyEvent: log.Debug("Got login_key!") err := ioutil.WriteFile(dml.loginKey, []byte(e.LoginKey), os.ModePerm) @@ -302,8 +316,14 @@ func (dml *DemoMatchLoader) steamEventHandler() { } case *steam.FatalErrorEvent: log.Debugf("[DL] Got FatalError %+v", e) + dml.GCReady = false + dml.LoggedIn = false + dml.connectFeedback <- LOGIN_FAILED case error: log.Warningf("[DL] Error: %+v", e) + dml.GCReady = false + dml.LoggedIn = false + dml.connectFeedback <- LOGIN_FAILED default: log.Debugf("[DL] %T: %v", e, e) }