Skip to content

Commit

Permalink
Merge pull request #256 from inada-s/rbk-ipv6
Browse files Browse the repository at this point in the history
Update for ipv6 ggpo support
  • Loading branch information
inada-s authored Jul 14, 2023
2 parents 226a6f4 + bf30b74 commit 85591f4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
24 changes: 8 additions & 16 deletions gdxsv/lbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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())
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions gdxsv/lbs_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ var _ = register(lbsLogout, func(p *LbsPeer, m *LbsMessage) {
p.app.BroadcastLobbyUserCount(p.Lobby)
p.Lobby = nil
}

p.logout = true
})

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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") {
Expand Down
20 changes: 15 additions & 5 deletions gdxsv/lbs_lobby.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions gdxsv/shareddata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 85591f4

Please sign in to comment.