diff --git a/gdxsv/lbs.go b/gdxsv/lbs.go index 3932cdf..f8f5e45 100644 --- a/gdxsv/lbs.go +++ b/gdxsv/lbs.go @@ -189,9 +189,8 @@ func (lbs *Lbs) ListenAndServe(addr string) { logger.Fatal("net.ListenTCP", zap.Error(err)) } - go lbs.serveUDP(addr) go lbs.eventLoop() - + go lbs.serveUDP(tcpAddr.Port) for { tcpConn, err := listener.AcceptTCP() if err != nil { @@ -204,14 +203,8 @@ func (lbs *Lbs) ListenAndServe(addr string) { } } -func (lbs *Lbs) serveUDP(addr string) { - logger.Info("lbs.ServeUDP", zap.String("addr", addr)) - - udpAddr, err := net.ResolveUDPAddr("udp4", addr) - if err != nil { - logger.Fatal("net.ResolveUDPAddr", zap.Error(err)) - } - udpConn, err := net.ListenUDP("udp4", udpAddr) +func (lbs *Lbs) serveUDP(port int) { + udpConn, err := net.ListenUDP("udp", &net.UDPAddr{Port: port}) if err != nil { logger.Fatal("net.ListenUDP", zap.Error(err)) } @@ -223,7 +216,7 @@ func (lbs *Lbs) serveUDP(addr string) { if err != nil { logger.Error("udpConn.ReadFromUDP", zap.Error(err)) if err == net.ErrClosed { - go lbs.serveUDP(addr) + go lbs.serveUDP(port) return } } @@ -354,11 +347,12 @@ func (lbs *Lbs) cleanPeer(p *LbsPeer) { if mcsAddr == McsAddrP2PGame { if !isOldFlycastVersion(p.PlatformInfo["flycast"], "v1.2.0") { sharedData.UpdateMcsGameState(p.Battle.BattleCode, McsGameStateClosed) - sharedData.UpdateMcsUserState(p.Battle.BattleCode, McsUserStateLeft) + sharedData.UpdateMcsUserState(p.SessionID, McsUserStateLeft) } } } if p.Battle != nil { + lbs.BroadcastBattleUserCount() p.Battle = nil } delete(lbs.userPeers, p.UserID) @@ -395,7 +389,6 @@ func (lbs *Lbs) eventLoop() { args.peer.lastRecvTime = time.Now() peers[args.peer.Address()] = args.peer StartLoginFlow(args.peer) - lbs.BroadcastBattleUserCount() case eventPeerMessage: if ce := args.peer.logger.Check(zap.DebugLevel, ""); ce != nil { fmt.Println(args.msg) @@ -423,7 +416,6 @@ func (lbs *Lbs) eventLoop() { args.peer.logger.Info("eventPeerLeave") lbs.cleanPeer(args.peer) delete(peers, args.peer.Address()) - lbs.BroadcastBattleUserCount() case eventFunc: func() { defer func() { @@ -644,10 +636,10 @@ func (lbs *Lbs) BroadcastRoomState(room *LbsRoom) { } func (lbs *Lbs) BroadcastBattleUserCount() { + cnt := sharedData.GetMcsUserCount() for userID := range lbs.userPeers { - var mcsUsersCount uint32 = uint32(len(sharedData.mcsUsers)) if p := lbs.FindPeer(userID); p != nil { - p.SendMessage(NewServerNotice(lbsBattleUserCount).Writer().Write32(mcsUsersCount).Msg()) + p.SendMessage(NewServerNotice(lbsBattleUserCount).Writer().Write32(uint32(cnt)).Msg()) } } } diff --git a/gdxsv/lbs_handler.go b/gdxsv/lbs_handler.go index 89ae5d9..fcef752 100644 --- a/gdxsv/lbs_handler.go +++ b/gdxsv/lbs_handler.go @@ -188,7 +188,6 @@ var _ = register(lbsLogout, func(p *LbsPeer, m *LbsMessage) { p.app.BroadcastLobbyUserCount(p.Lobby) p.Lobby = nil } - p.logout = true }) @@ -784,8 +783,7 @@ var _ = register(lbsDeviceData, func(p *LbsPeer, m *LbsMessage) { var _ = register(lbsServerMoney, func(p *LbsPeer, m *LbsMessage) { p.SendMessage(NewServerAnswer(m).Writer(). Write16(0).Write16(0).Write16(0).Write16(0).Msg()) - var mcsUsersCount uint32 = uint32(len(sharedData.GetMcsUsers())) - p.SendMessage(NewServerNotice(lbsBattleUserCount).Writer().Write32(mcsUsersCount).Msg()) + p.SendMessage(NewServerNotice(lbsBattleUserCount).Writer().Write32(uint32(sharedData.GetMcsUserCount())).Msg()) }) var _ = register(lbsStartLobby, func(p *LbsPeer, m *LbsMessage) { @@ -1600,10 +1598,20 @@ var _ = register(lbsExtSyncSharedData, func(p *LbsPeer, m *LbsMessage) { sharedData.SyncMcsToLbs(&mcsStatus) }) +func unzipIfCompressed(input []byte) []byte { + gr, err := zlib.NewReader(bytes.NewReader(input)) + if err != nil { + return input + } + defer gr.Close() + unzipped, _ := io.ReadAll(gr) + return unzipped +} + var _ = register(lbsPlatformInfo, func(p *LbsPeer, m *LbsMessage) { // patched client sends client-platform information r := m.Reader() - platformInfo := r.ReadString() + platformInfo := string(unzipIfCompressed(r.ReadBytes())) isFirstTime := len(p.PlatformInfo) == 0 for _, line := range strings.Split(strings.TrimSuffix(platformInfo, "\n"), "\n") { diff --git a/gdxsv/lbs_lobby.go b/gdxsv/lbs_lobby.go index 3c8bace..a4a6d99 100644 --- a/gdxsv/lbs_lobby.go +++ b/gdxsv/lbs_lobby.go @@ -658,8 +658,8 @@ func (l *LbsLobby) makeP2PMatchingMsg(b *LbsBattle, participants []*LbsPeer) ([] SessionId: int32(hash.Sum32()), PlayerCount: int32(len(participants)), PeerId: 0, - TimeoutMinMs: 6500, - TimeoutMaxMs: 10000, + TimeoutMinMs: 7500, + TimeoutMaxMs: 7500, Candidates: nil, RuleBin: SerializeRule(b.Rule), Users: nil, @@ -695,22 +695,29 @@ func (l *LbsLobby) makeP2PMatchingMsg(b *LbsBattle, participants []*LbsPeer) ([] ips["127.0.0.1"] = true ips[p.IP()] = true ips[p.PlatformInfo["local_ip"]] = true + ips[p.PlatformInfo["public_ipv4"]] = true + ips[p.PlatformInfo["public_ipv6"]] = true ports[p.PlatformInfo["udp_port"]] = true - ports[fmt.Sprint(p.udpAddr.Port)] = true for ip := range ips { for port := range ports { if ip == "" || port == "" || port == "0" { continue } - addrs[ip+":"+port] = true + addrs[net.JoinHostPort(ip, port)] = true } } + if p.udpAddr.IP != nil { + addrs[p.udpAddr.String()] = true + } + + p.udpAddr.IP.IsLoopback() + for addr := range addrs { ip, port, err := net.SplitHostPort(addr) if err != nil { - logger.Warn("SplitHostPort error", zap.Error(err)) + logger.Warn("net.SplitHostPort error", zap.Error(err)) continue } @@ -873,6 +880,7 @@ func (l *LbsLobby) checkLobbyBattleStart(force bool) { l.app.BroadcastLobbyUserCount(l) l.app.BroadcastLobbyMatchEntryUserCount(l) + l.app.BroadcastBattleUserCount() } func (l *LbsLobby) checkRoomBattleStart() { @@ -1034,6 +1042,8 @@ func (l *LbsLobby) checkRoomBattleStart() { if mcsPeer != nil { sharedData.NotifyLatestLbsStatus(mcsPeer) } + + l.app.BroadcastBattleUserCount() } func (l *LbsLobby) prepareMcs(mcsRegion string) (newMcsRegion string, mcsPeer *LbsPeer, mcsAddr string, canStart bool, alloc bool) { diff --git a/gdxsv/shareddata.go b/gdxsv/shareddata.go index 6b879df..4eda802 100644 --- a/gdxsv/shareddata.go +++ b/gdxsv/shareddata.go @@ -151,6 +151,12 @@ func (s *SharedData) SyncLbsToMcs(status *LbsStatus) { } } +func (s *SharedData) GetMcsUserCount() int { + s.Lock() + defer s.Unlock() + return len(s.mcsUsers) +} + func (s *SharedData) GetMcsUsers() []*McsUser { s.Lock() defer s.Unlock()