diff --git a/gdxsv/db.go b/gdxsv/db.go index 1dbd332..cf6c5e8 100644 --- a/gdxsv/db.go +++ b/gdxsv/db.go @@ -122,7 +122,7 @@ type DB interface { RegisterAccount(ip string) (*DBAccount, error) // RegisterAccountWithLoginKey creates new user account with specific login key. - // This function enables users to share login-key among different servers. + // This function enables userPeers to share login-key among different servers. RegisterAccountWithLoginKey(ip string, loginKey string) (*DBAccount, error) // GetAccountByLoginKey retrieves an account by login-key. @@ -135,7 +135,7 @@ type DB interface { LoginAccount(account *DBAccount, sessionID string) error // RegisterUser creates new user. - // An account can hold three users. + // An account can hold three userPeers. RegisterUser(loginKey string) (*DBUser, error) // GetUserList returns user list that the account holds. @@ -167,9 +167,9 @@ type DB interface { // CalculateUserDailyBattleCount calculates daily battle count of the user. CalculateUserDailyBattleCount(userID string) (ret BattleCountResult, err error) - // GetWinCountRanking returns top users of win count. + // GetWinCountRanking returns top userPeers of win count. GetWinCountRanking(side byte) (ret []*RankingRecord, err error) - // GetWinCountRanking returns top users of kill count. + // GetWinCountRanking returns top userPeers of kill count. GetKillCountRanking(side byte) (ret []*RankingRecord, err error) } diff --git a/gdxsv/lbs.go b/gdxsv/lbs.go index 13fc5cb..ffa1b9f 100644 --- a/gdxsv/lbs.go +++ b/gdxsv/lbs.go @@ -32,10 +32,9 @@ const ( type Lbs struct { handlers map[CmdID]LbsHandler - users map[string]*LbsPeer + userPeers map[string]*LbsPeer + mcsPeers map[string]*LbsPeer lobbies map[byte]map[uint16]*LbsLobby - mcsPeers map[string]*LbsPeer // publicAddr -> McsStatus - mcsStatus map[string]*McsStatus // region -> McsStatus chEvent chan interface{} chQuit chan interface{} } @@ -43,10 +42,9 @@ type Lbs struct { func NewLbs() *Lbs { app := &Lbs{ handlers: defaultLbsHandlers, - users: make(map[string]*LbsPeer), - lobbies: make(map[byte]map[uint16]*LbsLobby), + userPeers: make(map[string]*LbsPeer), mcsPeers: make(map[string]*LbsPeer), - mcsStatus: make(map[string]*McsStatus), + lobbies: make(map[byte]map[uint16]*LbsLobby), chEvent: make(chan interface{}, 64), chQuit: make(chan interface{}), } @@ -82,26 +80,26 @@ func (lbs *Lbs) GetLobby(platform uint8, lobbyID uint16) *LbsLobby { return lobby } -func (s *Lbs) ListenAndServe(addr string) error { +func (lbs *Lbs) ListenAndServe(addr string) error { glog.Info("ListenAndServe", addr) tcpAddr, err := net.ResolveTCPAddr("tcp", addr) if err != nil { return err } - listner, err := net.ListenTCP("tcp", tcpAddr) + listener, err := net.ListenTCP("tcp", tcpAddr) if err != nil { return err } - go s.eventLoop() + go lbs.eventLoop() for { - tcpConn, err := listner.AcceptTCP() + tcpConn, err := listener.AcceptTCP() if err != nil { glog.Errorln(err) continue } glog.Infoln("A new tcp connection open.", tcpConn.RemoteAddr()) - peer := s.NewPeer(tcpConn) + peer := lbs.NewPeer(tcpConn) go peer.serve() } } @@ -118,17 +116,19 @@ func (lbs *Lbs) NewPeer(conn *net.TCPConn) *LbsPeer { } func (lbs *Lbs) FindMcs(region string) *McsStatus { - for _, mcs := range lbs.mcsStatus { - if strings.HasPrefix(mcs.Region, region) && - mcs.PublicAddr != "" { - return mcs + for _, p := range lbs.mcsPeers { + if p.mcsStatus != nil { + if strings.HasPrefix(p.mcsStatus.Region, region) && + p.mcsStatus.PublicAddr != "" { + return p.mcsStatus + } } } return nil } func (lbs *Lbs) FindPeer(userID string) *LbsPeer { - p, ok := lbs.users[userID] + p, ok := lbs.userPeers[userID] if !ok { return nil } @@ -154,7 +154,7 @@ func (lbs *Lbs) Locked(f func(*Lbs)) { func (lbs *Lbs) Quit() { lbs.Locked(func(app *Lbs) { - for _, p := range app.users { + for _, p := range app.userPeers { SendServerShutDown(p) } }) @@ -188,6 +188,30 @@ type eventFunc struct { c chan<- interface{} } +func (lbs *Lbs) cleanPeer(p *LbsPeer) { + if p.UserID != "" { + if p.Room != nil { + p.Room.Exit(p.UserID) + lbs.BroadcastRoomState(p.Room) + p.Room = nil + } + if p.Lobby != nil { + p.Lobby.Exit(p.UserID) + lbs.BroadcastLobbyUserCount(p.Lobby) + lbs.BroadcastLobbyMatchEntryUserCount(p.Lobby) + p.Lobby = nil + } + delete(lbs.userPeers, p.UserID) + } + + if p.mcsStatus != nil { + delete(p.app.mcsPeers, p.mcsStatus.PublicAddr) + p.mcsStatus = nil + } + + p.conn.Close() +} + func (lbs *Lbs) eventLoop() { tick := time.Tick(1 * time.Second) peers := map[string]*LbsPeer{} @@ -221,7 +245,7 @@ func (lbs *Lbs) eventLoop() { } case eventPeerLeave: glog.Infoln("eventPeerLeave") - delete(lbs.users, args.peer.UserID) + lbs.cleanPeer(args.peer) delete(peers, args.peer.Address()) case eventFunc: args.f(lbs) @@ -229,24 +253,11 @@ func (lbs *Lbs) eventLoop() { } case <-tick: for _, p := range peers { - if 1 <= time.Since(p.lastRecvTime).Minutes() { - glog.Infoln("Kick", p.Address()) - if p.UserID != "" { - if p.Room != nil { - p.Room.Exit(p.UserID) - lbs.BroadcastRoomState(p.Room) - p.Room = nil - } - if p.Lobby != nil { - p.Lobby.Exit(p.UserID) - lbs.BroadcastLobbyUserCount(p.Lobby) - lbs.BroadcastLobbyMatchEntryUserCount(p.Lobby) - p.Lobby = nil - } - } - delete(peers, p.Address()) + lastRecvSince := time.Since(p.lastRecvTime) + if 1 <= lastRecvSince.Minutes() { + glog.Infoln("Kick peer", p.Address()) p.conn.Close() - } else { + } else if 10 <= lastRecvSince.Seconds() { RequestLineCheck(p) } } @@ -268,7 +279,7 @@ func (lbs *Lbs) BroadcastLobbyUserCount(lobby *LbsLobby) { // For lobby select scene. msg := NewServerNotice(lbsPlazaJoin).Writer(). Write16(lobby.ID).Write16(uint16(len(lobby.Users))).Msg() - for _, u := range lbs.users { + for _, u := range lbs.userPeers { if u.Platform == lobby.Platform { u.SendMessage(msg) } @@ -484,6 +495,9 @@ type LbsPeer struct { mInbuf sync.Mutex inbuf []byte + + // used only mcs peer + mcsStatus *McsStatus } func (p *LbsPeer) InLobbyChat() bool { diff --git a/gdxsv/lbs_handler.go b/gdxsv/lbs_handler.go index 29f370c..a071bfe 100644 --- a/gdxsv/lbs_handler.go +++ b/gdxsv/lbs_handler.go @@ -379,7 +379,7 @@ var _ = register(lbsUserRegist, func(p *LbsPeer, m *LbsMessage) { } p.DBUser = *u - p.app.users[p.UserID] = p + p.app.userPeers[p.UserID] = p p.SendMessage(NewServerAnswer(m).Writer().WriteString(userID).Msg()) }) @@ -410,7 +410,7 @@ var _ = register(lbsUserDecide, func(p *LbsPeer, m *LbsMessage) { } p.DBUser = *u - p.app.users[p.UserID] = p + p.app.userPeers[p.UserID] = p p.SendMessage(NewServerAnswer(m).Writer().WriteString(p.UserID).Msg()) p.SendMessage(NewServerQuestion(lbsAskGameCode)) }) @@ -962,7 +962,7 @@ var _ = register(lbsSendMail, func(p *LbsPeer, m *LbsMessage) { glog.Infoln("com1", comment1) glog.Infoln("com2", comment2) - u, ok := p.app.users[userID] + u, ok := p.app.userPeers[userID] if !ok { p.SendMessage(NewServerAnswer(m).SetErr().Writer(). WriteString("
THE USER IS NOT IN LOBBY").Msg()) @@ -1161,7 +1161,7 @@ var _ = register(lbsTopRankingTag, func(p *LbsPeer, m *LbsMessage) { }) var _ = register(lbsTopRankingSuu, func(p *LbsPeer, m *LbsMessage) { - // How many users there is in the ranking + // How many userPeers there is in the ranking // page: ranking kind? page := m.Reader().Read8() glog.Infoln("page", page) @@ -1362,8 +1362,7 @@ var _ = register(lbsExtSyncSharedData, func(p *LbsPeer, m *LbsMessage) { } glog.Infof("update mcs status: %+v", mcsStatus) - p.app.mcsStatus[mcsStatus.Region] = &mcsStatus p.app.mcsPeers[mcsStatus.PublicAddr] = p - + p.mcsStatus = &mcsStatus SyncSharedDataMcsToLbs(&mcsStatus) }) diff --git a/gdxsv/lbs_mcsfunc.go b/gdxsv/lbs_mcsfunc.go index 0f599bf..c075f95 100644 --- a/gdxsv/lbs_mcsfunc.go +++ b/gdxsv/lbs_mcsfunc.go @@ -31,7 +31,7 @@ var gcpLocationName = map[string]string{ "europe-west2": "London, England, UK", "europe-west3": "Frankfurt, Germany", "europe-west4": "Eemshaven, Netherlands", - "europe-west6": "Zürich, Switzerland", + "europe-west6": "Zurich, Switzerland", "northamerica-northeast1": "Montreal, Quebec, Canada", "southamerica-east1": "Osasco (Sao Paulo), Brazil", "us-central1": "Council Bluffs, Iowa, USA", diff --git a/gdxsv/main.go b/gdxsv/main.go index 8937cc8..c30f65f 100644 --- a/gdxsv/main.go +++ b/gdxsv/main.go @@ -141,7 +141,7 @@ func mainLbs() { dumper.DisablePointerAddresses = true go func() { for { - ioutil.WriteFile("dump.txt", []byte(dumper.Sdump(lbs.users)), 0644) + ioutil.WriteFile("dump.txt", []byte(dumper.Sdump(lbs.userPeers)), 0644) time.Sleep(time.Second) } }() diff --git a/gdxsv/shareddata.go b/gdxsv/shareddata.go index 67522c4..7ba2446 100644 --- a/gdxsv/shareddata.go +++ b/gdxsv/shareddata.go @@ -40,11 +40,11 @@ type McsStatus struct { Region string `json:"region,omitempty"` PublicAddr string `json:"public_addr,omitempty"` Updated time.Time `json:"updated,omitempty"` - Users []McsUser `json:"users,omitempty"` + Users []McsUser `json:"userPeers,omitempty"` } type LbsStatus struct { - Users []McsUser `json:"users,omitempty"` + Users []McsUser `json:"userPeers,omitempty"` } func AddUserWhoIsGoingToBattle(battleCode string, mcsRegion string, userID string, name string, side uint16, sessionID string) { diff --git a/infra/mcsfunc/index.js b/infra/mcsfunc/index.js index 0079541..c3ea096 100644 --- a/infra/mcsfunc/index.js +++ b/infra/mcsfunc/index.js @@ -32,10 +32,17 @@ const gcpRegions = { function getStartupScript(version) { return `\ #!/bin/bash +echo "startup-script" apt-get update apt-get install -y jq wget curl +curl -sSO https://dl.google.com/cloudagents/install-monitoring-agent.sh +bash install-monitoring-agent.sh + +curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh +bash install-logging-agent.sh + if grep -xqFe 'ubuntu ALL=NOPASSWD: /sbin/shutdown' /etc/sudoers; then echo 'ubuntu ALL=NOPASSWD: /sbin/shutdown' >> /etc/sudoers fi @@ -43,6 +50,7 @@ fi cat << 'EOF' > /home/ubuntu/launch-mcs.sh #!/bin/bash -eux + function finish { echo "finish" sleep 1 @@ -78,6 +86,8 @@ EOF chmod +x /home/ubuntu/launch-mcs.sh su ubuntu -c 'cd /home/ubuntu && nohup ./launch-mcs.sh 2>&1 | logger &' + +echo "startup-script done" ` }