Skip to content

Commit

Permalink
Make sessions not findable if their host is not connected
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloMK7 committed Apr 16, 2024
1 parent 5e98a94 commit 23bdf4b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
48 changes: 42 additions & 6 deletions globals/matchmaking_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,19 @@ func CreateSessionByMatchmakeSession(matchmakeSession *match_making_types.Matchm
return sessions[sessionIndex], nil
}

// isSessionHostConnected checks if the current session host is connected
func isSessionHostConnected(session *CommonMatchmakeSession, endpoint *nex.PRUDPEndPoint) bool {
return session.ConnectionIDs.Each(func(_ int, connectionID uint32) bool {
target := endpoint.FindConnectionByID(connectionID)
if target == nil {
return false
}
return session.GameMatchmakeSession.HostPID.Equals(target.PID())
})
}

// FindSessionByMatchmakeSession finds a gathering that matches with a MatchmakeSession
func FindSessionByMatchmakeSession(pid *types.PID, searchMatchmakeSession *match_making_types.MatchmakeSession) uint32 {
func FindSessionByMatchmakeSession(connection *nex.PRUDPConnection, searchMatchmakeSession *match_making_types.MatchmakeSession) uint32 {
sessionsMutex.RLock()
defer sessionsMutex.RUnlock()

Expand All @@ -348,9 +359,22 @@ func FindSessionByMatchmakeSession(pid *types.PID, searchMatchmakeSession *match
// * This is handled below
candidateSessionIndexes := make([]uint32, 0, len(sessions))
for index, session := range sessions {
if session.SearchMatchmakeSession.Equals(searchMatchmakeSession) {
candidateSessionIndexes = append(candidateSessionIndexes, index)
if !session.SearchMatchmakeSession.Equals(searchMatchmakeSession) {
continue
}
// * Do not find the session if the host is not currently connected
if !isSessionHostConnected(session, connection.Endpoint().(*nex.PRUDPEndPoint)) {
continue
}

// * Do not find the room if the requesting connection is the host. This means
// * the host was disconnected but the room host PID wasn't updated yet by the rest of
// * the clients. The host suddenly being available again causes issues.
if (session.GameMatchmakeSession.HostPID.Equals(connection.PID())) {
continue
}

candidateSessionIndexes = append(candidateSessionIndexes, index)
}

// TODO - This whole section assumes legacy clients. None of it will work on the Switch
Expand Down Expand Up @@ -378,7 +402,7 @@ func FindSessionByMatchmakeSession(pid *types.PID, searchMatchmakeSession *match
}

if len(friendList) == 0 {
friendList = GetUserFriendPIDsHandler(pid.LegacyValue()) // TODO - This grpc method needs to support the Switch
friendList = GetUserFriendPIDsHandler(connection.PID().LegacyValue()) // TODO - This grpc method needs to support the Switch
}

if !slices.Contains(friendList, sessionToCheck.GameMatchmakeSession.OwnerPID.LegacyValue()) {
Expand All @@ -393,7 +417,7 @@ func FindSessionByMatchmakeSession(pid *types.PID, searchMatchmakeSession *match
}

// FindSessionsByMatchmakeSessionSearchCriterias finds a gathering that matches with the given search criteria
func FindSessionsByMatchmakeSessionSearchCriterias(pid *types.PID, searchCriterias []*match_making_types.MatchmakeSessionSearchCriteria, gameSpecificChecks func(searchCriteria *match_making_types.MatchmakeSessionSearchCriteria, matchmakeSession *match_making_types.MatchmakeSession) bool) []*CommonMatchmakeSession {
func FindSessionsByMatchmakeSessionSearchCriterias(connection *nex.PRUDPConnection, searchCriterias []*match_making_types.MatchmakeSessionSearchCriteria, gameSpecificChecks func(searchCriteria *match_making_types.MatchmakeSessionSearchCriteria, matchmakeSession *match_making_types.MatchmakeSession) bool) []*CommonMatchmakeSession {
sessionsMutex.RLock()
defer sessionsMutex.RUnlock()

Expand All @@ -402,6 +426,18 @@ func FindSessionsByMatchmakeSessionSearchCriterias(pid *types.PID, searchCriteri
// TODO - This whole section assumes legacy clients. None of it will work on the Switch
var friendList []uint32
for _, session := range sessions {
// * Do not find the session if the host is not currently connected
if !isSessionHostConnected(session, connection.Endpoint().(*nex.PRUDPEndPoint)) {
continue
}

// * Do not find the room if the requesting connection is the host. This means
// * the host was disconnected but the room host PID wasn't updated yet by the rest of
// * the clients. The host suddenly being available again causes issues.
if (session.GameMatchmakeSession.HostPID.Equals(connection.PID())) {
continue
}

for _, criteria := range searchCriterias {
// * Check things like game specific attributes
if gameSpecificChecks != nil {
Expand Down Expand Up @@ -447,7 +483,7 @@ func FindSessionsByMatchmakeSessionSearchCriterias(pid *types.PID, searchCriteri
}

if len(friendList) == 0 {
friendList = GetUserFriendPIDsHandler(pid.LegacyValue()) // TODO - Support the Switch
friendList = GetUserFriendPIDsHandler(connection.PID().LegacyValue()) // TODO - Support the Switch
}

if !slices.Contains(friendList, session.GameMatchmakeSession.OwnerPID.LegacyValue()) {
Expand Down
2 changes: 1 addition & 1 deletion matchmake-extension/auto_matchmake_postpone.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (commonProtocol *CommonProtocol) autoMatchmakePostpone(err error, packet ne

searchMatchmakeSession := matchmakeSession.Copy().(*match_making_types.MatchmakeSession)
commonProtocol.CleanupSearchMatchmakeSession(searchMatchmakeSession)
sessionIndex := common_globals.FindSessionByMatchmakeSession(connection.PID(), searchMatchmakeSession)
sessionIndex := common_globals.FindSessionByMatchmakeSession(connection, searchMatchmakeSession)
var session *common_globals.CommonMatchmakeSession

if sessionIndex == 0 {
Expand Down
2 changes: 1 addition & 1 deletion matchmake-extension/auto_matchmake_with_param_postpone.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (commonProtocol *CommonProtocol) autoMatchmakeWithParamPostpone(err error,

matchmakeSession := autoMatchmakeParam.SourceMatchmakeSession

sessions := common_globals.FindSessionsByMatchmakeSessionSearchCriterias(connection.PID(), autoMatchmakeParam.LstSearchCriteria.Slice(), commonProtocol.GameSpecificMatchmakeSessionSearchCriteriaChecks)
sessions := common_globals.FindSessionsByMatchmakeSessionSearchCriterias(connection, autoMatchmakeParam.LstSearchCriteria.Slice(), commonProtocol.GameSpecificMatchmakeSessionSearchCriteriaChecks)
var session *common_globals.CommonMatchmakeSession

if len(sessions) == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (commonProtocol *CommonProtocol) autoMatchmakeWithSearchCriteriaPostpone(er
return nil, nex.NewError(nex.ResultCodes.Core.InvalidArgument, "change_error")
}

sessions := common_globals.FindSessionsByMatchmakeSessionSearchCriterias(connection.PID(), lstSearchCriteria.Slice(), commonProtocol.GameSpecificMatchmakeSessionSearchCriteriaChecks)
sessions := common_globals.FindSessionsByMatchmakeSessionSearchCriterias(connection, lstSearchCriteria.Slice(), commonProtocol.GameSpecificMatchmakeSessionSearchCriteriaChecks)
var session *common_globals.CommonMatchmakeSession

if len(sessions) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion matchmake-extension/browse_matchmake_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (commonProtocol *CommonProtocol) browseMatchmakeSession(err error, packet n

searchCriterias := []*match_making_types.MatchmakeSessionSearchCriteria{searchCriteria}

sessions := common_globals.FindSessionsByMatchmakeSessionSearchCriterias(connection.PID(), searchCriterias, commonProtocol.GameSpecificMatchmakeSessionSearchCriteriaChecks)
sessions := common_globals.FindSessionsByMatchmakeSessionSearchCriterias(connection, searchCriterias, commonProtocol.GameSpecificMatchmakeSessionSearchCriteriaChecks)

// TODO - Is this right?
if resultRange.Offset.Value != math.MaxUint32 {
Expand Down

0 comments on commit 23bdf4b

Please sign in to comment.