Skip to content

Commit

Permalink
fix mcs vars
Browse files Browse the repository at this point in the history
  • Loading branch information
inada-s committed May 2, 2020
1 parent 2045664 commit 8b986ac
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 50 deletions.
8 changes: 4 additions & 4 deletions gdxsv/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
86 changes: 50 additions & 36 deletions gdxsv/lbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,19 @@ 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{}
}

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{}),
}
Expand Down Expand Up @@ -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()
}
}
Expand All @@ -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
}
Expand All @@ -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)
}
})
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -221,32 +245,19 @@ 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)
args.c <- struct{}{}
}
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)
}
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -484,6 +495,9 @@ type LbsPeer struct {

mInbuf sync.Mutex
inbuf []byte

// used only mcs peer
mcsStatus *McsStatus
}

func (p *LbsPeer) InLobbyChat() bool {
Expand Down
11 changes: 5 additions & 6 deletions gdxsv/lbs_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

Expand Down Expand Up @@ -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))
})
Expand Down Expand Up @@ -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("<LF=6><BODY><CENTER>THE USER IS NOT IN LOBBY<END>").Msg())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
})
2 changes: 1 addition & 1 deletion gdxsv/lbs_mcsfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion gdxsv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()
Expand Down
4 changes: 2 additions & 2 deletions gdxsv/shareddata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions infra/mcsfunc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,25 @@ 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
cat << 'EOF' > /home/ubuntu/launch-mcs.sh
#!/bin/bash -eux
function finish {
echo "finish"
sleep 1
Expand Down Expand Up @@ -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"
`
}

Expand Down

0 comments on commit 8b986ac

Please sign in to comment.