Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions server/game/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type Room struct {
masterOrder []ClientID
watchers map[ClientID]*Client

lastMsg binary.Dict // map[clientID]unixtime_millisec
muLastMsg sync.RWMutex
lastMsg binary.Dict // map[clientID]unixtime_millisec

logger log.Logger

Expand Down Expand Up @@ -166,18 +167,24 @@ func (r *Room) Done() <-chan struct{} {

func (r *Room) writeLastMsg(cid ClientID) {
millisec := uint64(time.Now().UnixNano()) / 1000000
r.muLastMsg.Lock()
defer r.muLastMsg.Unlock()
r.lastMsg[string(cid)] = binary.MarshalULong(millisec)
}

func (r *Room) removeLastMsg(cid ClientID) {
r.muLastMsg.Lock()
defer r.muLastMsg.Unlock()
delete(r.lastMsg, string(cid))
}

// UpdateLastMsg : PlayerがMsgを受信したとき更新する.
// 既に登録されているPlayerのみ書き込み (watcherを含めないため)
func (r *Room) updateLastMsg(cid ClientID) {
id := string(cid)
if _, ok := r.lastMsg[id]; ok {
r.muLastMsg.Lock()
_, ok := r.lastMsg[string(cid)]
r.muLastMsg.Unlock()
if ok {
r.writeLastMsg(cid)
}
}
Expand Down Expand Up @@ -515,7 +522,9 @@ func (r *Room) msgPing(msg *MsgPing) {
}
}
msg.Sender.logger.Debugf("ping %v: %v", msg.Sender.Id, msg.Timestamp)
r.muLastMsg.RLock()
ev := binary.NewEvPong(msg.Timestamp, r.RoomInfo.Watchers, r.lastMsg)
r.muLastMsg.RUnlock()
msg.Sender.SendSystemEvent(ev)
}

Expand Down Expand Up @@ -783,13 +792,15 @@ func (r *Room) msgGetRoomInfo(msg *MsgGetRoomInfo) {
cis = append(cis, r.players[id].ClientInfo.Clone())
}
lmt := make(map[string]uint64)
r.muLastMsg.RLock()
for p, d := range r.lastMsg {
t, _, err := binary.UnmarshalAs(d, binary.TypeULong)
if err != nil {
r.logger.Errorf("Unmarshal LastMsg[%s]: %w", p, err)
}
lmt[p] = t.(uint64)
}
r.muLastMsg.RUnlock()

msg.Res <- &pb.GetRoomInfoRes{
RoomInfo: ri,
Expand Down
Loading