Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle multiple station URLs on Register and more #17

Merged
merged 7 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions globals/matchmaking_globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ type CommonMatchmakeSession struct {
}

var Sessions map[uint32]*CommonMatchmakeSession
var CurrentGatheringID uint32
DaniElectra marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 4 additions & 9 deletions globals/matchmaking_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@ import (
// GetAvailableGatheringID returns a gathering ID which doesn't belong to any session
// Returns 0 if no IDs are available (math.MaxUint32 has been reached)
func GetAvailableGatheringID() uint32 {
var gatheringID uint32 = 1
for gatheringID < math.MaxUint32 {
// * If the session does not exist, the gathering ID is free
if _, ok := Sessions[gatheringID]; !ok {
return gatheringID
}

gatheringID++
if CurrentGatheringID == math.MaxUint32 {
return 0
}

return 0
CurrentGatheringID++
return CurrentGatheringID
}

// FindOtherConnectionID searches a connection ID on the session that isn't the given one
Expand Down
9 changes: 5 additions & 4 deletions matchmake-extension/auto_matchmake_postpone.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import (
)

func autoMatchmake_Postpone(err error, client *nex.Client, callID uint32, anyGathering *nex.DataHolder, message string) uint32 {
if commonMatchmakeExtensionProtocol.cleanupSearchMatchmakeSessionHandler == nil {
logger.Warning("MatchmakeExtension::AutoMatchmake_Postpone missing CleanupSearchMatchmakeSessionHandler!")
return nex.Errors.Core.NotImplemented
}

if err != nil {
logger.Error(err.Error())
return nex.Errors.Core.InvalidArgument
}

server := commonMatchmakeExtensionProtocol.server
if commonMatchmakeExtensionProtocol.cleanupSearchMatchmakeSessionHandler == nil {
logger.Warning("MatchmakeExtension::AutoMatchmake_Postpone missing CleanupSearchMatchmakeSessionHandler!")
return nex.Errors.Core.Exception
}

// A client may disconnect from a session without leaving reliably,
// so let's make sure the client is removed from the session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func autoMatchmakeWithSearchCriteria_Postpone(err error, client *nex.Client, callID uint32, lstSearchCriteria []*match_making_types.MatchmakeSessionSearchCriteria, anyGathering *nex.DataHolder, message string) uint32 {
if commonMatchmakeExtensionProtocol.cleanupMatchmakeSessionSearchCriteriaHandler == nil {
logger.Warning("MatchmakeExtension::AutoMatchmake_Postpone missing CleanupMatchmakeSessionSearchCriteriaHandler!")
logger.Warning("MatchmakeExtension::AutoMatchmakeWithSearchCriteria_Postpone missing CleanupMatchmakeSessionSearchCriteriaHandler!")
return nex.Errors.Core.NotImplemented
}

Expand Down
55 changes: 55 additions & 0 deletions matchmake-extension/close_participation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package matchmake_extension

import (
nex "github.com/PretendoNetwork/nex-go"
common_globals "github.com/PretendoNetwork/nex-protocols-common-go/globals"
matchmake_extension "github.com/PretendoNetwork/nex-protocols-go/matchmake-extension"
)

func closeParticipation(err error, client *nex.Client, callID uint32, gid uint32) uint32 {
if err != nil {
logger.Error(err.Error())
return nex.Errors.Core.InvalidArgument
}

var session *common_globals.CommonMatchmakeSession
var ok bool
if session, ok = common_globals.Sessions[gid]; !ok {
return nex.Errors.RendezVous.SessionVoid
}

if session.GameMatchmakeSession.Gathering.OwnerPID != client.PID() {
return nex.Errors.RendezVous.PermissionDenied
}

session.GameMatchmakeSession.OpenParticipation = false

server := commonMatchmakeExtensionProtocol.server

rmcResponse := nex.NewRMCResponse(matchmake_extension.ProtocolID, callID)
rmcResponse.SetSuccess(matchmake_extension.MethodCloseParticipation, nil)

rmcResponseBytes := rmcResponse.Bytes()

var responsePacket nex.PacketInterface

if server.PRUDPVersion() == 0 {
responsePacket, _ = nex.NewPacketV0(client, nil)
responsePacket.SetVersion(0)
} else {
responsePacket, _ = nex.NewPacketV1(client, nil)
responsePacket.SetVersion(1)
}

responsePacket.SetSource(0xA1)
responsePacket.SetDestination(0xAF)
responsePacket.SetType(nex.DataPacket)
responsePacket.SetPayload(rmcResponseBytes)

responsePacket.AddFlag(nex.FlagNeedsAck)
responsePacket.AddFlag(nex.FlagReliable)

server.Send(responsePacket)

return 0
}
4 changes: 4 additions & 0 deletions matchmake-extension/modify_current_game_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func modifyCurrentGameAttribute(err error, client *nex.Client, callID uint32, gi
return nex.Errors.RendezVous.SessionVoid
}

if session.GameMatchmakeSession.Gathering.OwnerPID != client.PID() {
return nex.Errors.RendezVous.PermissionDenied
}

if int(attribIndex) > len(session.GameMatchmakeSession.Attributes) {
return nex.Errors.Core.InvalidIndex
}
Expand Down
2 changes: 2 additions & 0 deletions matchmake-extension/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func initDefault(c *CommonMatchmakeExtensionProtocol) {
// TODO - Organize by method ID
c.DefaultProtocol = matchmake_extension.NewProtocol(c.server)
c.DefaultProtocol.OpenParticipation(openParticipation)
c.DefaultProtocol.CloseParticipation(closeParticipation)
c.DefaultProtocol.CreateMatchmakeSession(createMatchmakeSession)
c.DefaultProtocol.GetSimplePlayingSession(getSimplePlayingSession)
c.DefaultProtocol.AutoMatchmakePostpone(autoMatchmake_Postpone)
Expand All @@ -58,6 +59,7 @@ func initMarioKart8(c *CommonMatchmakeExtensionProtocol) {
// TODO - Organize by method ID
c.MarioKart8Protocol = matchmake_extension_mario_kart_8.NewProtocol(c.server)
c.MarioKart8Protocol.OpenParticipation(openParticipation)
c.MarioKart8Protocol.CloseParticipation(closeParticipation)
c.MarioKart8Protocol.CreateMatchmakeSession(createMatchmakeSession)
c.MarioKart8Protocol.GetSimplePlayingSession(getSimplePlayingSession)
c.MarioKart8Protocol.AutoMatchmakePostpone(autoMatchmake_Postpone)
Expand Down
24 changes: 18 additions & 6 deletions secure-connection/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,27 @@ func register(err error, client *nex.Client, callID uint32, stationUrls []*nex.S
client.SetConnectionID(nextConnectionID)

localStation := stationUrls[0]
publicStation := localStation.Copy()

publicStation.SetAddress(client.Address().IP.String())
publicStation.SetPort(uint32(client.Address().Port))
publicStation.SetNatf(0)
publicStation.SetNatm(0)
publicStation.SetType(3)
// * A NEX client can set the public station URL by setting two URLs on the array
var publicStation *nex.StationURL
if len(stationUrls) > 1 {
publicStation = stationUrls[1]
} else {
DaniElectra marked this conversation as resolved.
Show resolved Hide resolved
publicStation = localStation.Copy()

publicStation.SetAddress(client.Address().IP.String())
publicStation.SetPort(uint32(client.Address().Port))
publicStation.SetNatf(0)
publicStation.SetNatm(0)
publicStation.SetType(3)
}

localStation.SetPID(client.PID())
publicStation.SetPID(client.PID())

localStation.SetRVCID(client.ConnectionID())
publicStation.SetRVCID(client.ConnectionID())

jonbarrow marked this conversation as resolved.
Show resolved Hide resolved
localStation.SetLocal()
publicStation.SetPublic()

Expand Down