chore: migrate ConnectedUser map to MutexMap

This commit is contained in:
Jonathan Barrow
2024-04-09 12:21:50 -04:00
parent d6fdde6161
commit 18e1a0fefa
25 changed files with 81 additions and 72 deletions

View File

@@ -13,8 +13,7 @@ func GetUserFriends(pid uint32) (*types.List[*friends_3ds_types.FriendRelationsh
friendRelationships := types.NewList[*friends_3ds_types.FriendRelationship]()
friendRelationships.Type = friends_3ds_types.NewFriendRelationship()
rows, err := database.Postgres.Query(`
SELECT user2_pid, type FROM "3ds".friendships WHERE user1_pid=$1 AND type=1 LIMIT 100`, pid)
rows, err := database.Postgres.Query("SELECT user2_pid, type FROM \"3ds\".friendships WHERE user1_pid=$1 AND type=1 LIMIT 100", pid)
if err != nil {
if err == sql.ErrNoRows {
return friendRelationships, database.ErrEmptyList

View File

@@ -59,10 +59,10 @@ func AcceptFriendRequestAndReturnFriendInfo(friendRequestID uint64) (*friends_wi
}
friendInfo := friends_wiiu_types.NewFriendInfo()
connectedUser := globals.ConnectedUsers[senderPID]
connectedUser, ok := globals.ConnectedUsers.Get(senderPID)
var lastOnline *types.DateTime
if connectedUser != nil {
if ok && connectedUser != nil {
// * Online
friendInfo.NNAInfo = connectedUser.NNAInfo
friendInfo.Presence = connectedUser.PresenceV2

View File

@@ -35,10 +35,10 @@ func GetUserFriendList(pid uint32) (*types.List[*friends_wiiu_types.FriendInfo],
}
friendInfo := friends_wiiu_types.NewFriendInfo()
connectedUser := globals.ConnectedUsers[friendPID]
connectedUser, ok := globals.ConnectedUsers.Get(friendPID)
var lastOnline *types.DateTime
if connectedUser != nil {
if ok && connectedUser != nil {
// * Online
friendInfo.NNAInfo = connectedUser.NNAInfo
friendInfo.Presence = connectedUser.PresenceV2

View File

@@ -16,7 +16,7 @@ var AuthenticationServer *nex.PRUDPServer
var AuthenticationEndpoint *nex.PRUDPEndPoint
var SecureServer *nex.PRUDPServer
var SecureEndpoint *nex.PRUDPEndPoint
var ConnectedUsers map[uint32]*types.ConnectedUser
var ConnectedUsers *nex.MutexMap[uint32, *types.ConnectedUser]
var AESKey []byte
var GRPCAccountClientConnection *grpc.ClientConn
var GRPCAccountClient pb.AccountClient

View File

@@ -13,9 +13,9 @@ import (
// SendUserNotificationWiiU implements helloworld.SendUserNotificationWiiU
func (s *gRPCFriendsServer) SendUserNotificationWiiU(ctx context.Context, in *pb.SendUserNotificationWiiURequest) (*empty.Empty, error) {
connectedUser := globals.ConnectedUsers[in.GetPid()]
connectedUser, ok := globals.ConnectedUsers.Get(in.GetPid())
if connectedUser != nil {
if ok && connectedUser != nil {
rmcRequest := nex.NewRMCRequest(globals.SecureEndpoint)
rmcRequest.ProtocolID = nintendo_notifications.ProtocolID
rmcRequest.CallID = 3810693103

View File

@@ -11,6 +11,7 @@ import (
"github.com/PretendoNetwork/friends/globals"
"github.com/PretendoNetwork/friends/types"
pb "github.com/PretendoNetwork/grpc-go/account"
"github.com/PretendoNetwork/nex-go/v2"
"github.com/PretendoNetwork/plogger-go"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
@@ -21,8 +22,9 @@ import (
func init() {
globals.Logger = plogger.NewLogger()
globals.ConnectedUsers = make(map[uint32]*types.ConnectedUser)
// Setup RSA private key for token parsing
globals.ConnectedUsers = nex.NewMutexMap[uint32, *types.ConnectedUser]()
// * Setup RSA private key for token parsing
var err error
err = godotenv.Load()

View File

@@ -23,8 +23,8 @@ func AddFriendByPrincipalID(err error, packet nex.PacketInterface, callID uint32
return nil, nex.NewError(nex.ResultCodes.FPD.Unknown, "") // TODO - Add error message
}
connectedUser := globals.ConnectedUsers[pid.LegacyValue()]
if connectedUser != nil {
connectedUser, ok := globals.ConnectedUsers.Get(pid.LegacyValue())
if ok && connectedUser != nil {
go notifications_3ds.SendFriendshipCompleted(connectedUser.Connection, pid.LegacyValue(), connection.PID())
}

View File

@@ -18,12 +18,12 @@ func GetFriendPresence(err error, packet nex.PacketInterface, callID uint32, pid
presenceList.Type = friends_3ds_types.NewFriendPresence()
pidList.Each(func(i int, pid *types.PID) bool {
connectedUser := globals.ConnectedUsers[pid.LegacyValue()]
connectedUser, ok := globals.ConnectedUsers.Get(pid.LegacyValue())
if connectedUser != nil && connectedUser.Presence != nil {
if ok && connectedUser != nil && connectedUser.Presence != nil {
friendPresence := friends_3ds_types.NewFriendPresence()
friendPresence.PID = pid.Copy().(*types.PID)
friendPresence.Presence = globals.ConnectedUsers[pid.LegacyValue()].Presence
friendPresence.Presence = connectedUser.Presence
presenceList.Append(friendPresence)
}

View File

@@ -64,8 +64,8 @@ func SyncFriend(err error, packet nex.PacketInterface, callID uint32, lfc *types
relationships = append(relationships, relationship)
// * Alert the other side, in case they weren't able to get our presence data
connectedUser := globals.ConnectedUsers[pid.LegacyValue()]
if connectedUser != nil {
connectedUser, ok := globals.ConnectedUsers.Get(pid.LegacyValue())
if ok && connectedUser != nil {
go notifications_3ds.SendFriendshipCompleted(connectedUser.Connection, pid.LegacyValue(), connection.PID())
}
}

View File

@@ -30,18 +30,19 @@ func UpdatePresence(err error, packet nex.PacketInterface, callID uint32, presen
go notifications_3ds.SendPresenceUpdate(connection, currentPresence)
pid := connection.PID().LegacyValue()
connectedUser, ok := globals.ConnectedUsers.Get(pid)
if globals.ConnectedUsers[pid] == nil {
if !ok || connectedUser == nil {
// TODO - Figure out why this is getting removed
connectedUser := friends_types.NewConnectedUser()
connectedUser = friends_types.NewConnectedUser()
connectedUser.PID = pid
connectedUser.Platform = friends_types.CTR
connectedUser.Connection = connection
globals.ConnectedUsers[pid] = connectedUser
globals.ConnectedUsers.Set(pid, connectedUser)
}
globals.ConnectedUsers[pid].Presence = currentPresence
connectedUser.Presence = currentPresence
rmcResponse := nex.NewRMCSuccess(globals.SecureEndpoint, nil)
rmcResponse.ProtocolID = friends_3ds.ProtocolID

View File

@@ -30,30 +30,32 @@ func AcceptFriendRequest(err error, packet nex.PacketInterface, callID uint32, i
}
friendPID := friendInfo.NNAInfo.PrincipalBasicInfo.PID.LegacyValue()
connectedUser := globals.ConnectedUsers[friendPID]
connectedUser, ok := globals.ConnectedUsers.Get(friendPID)
if connectedUser != nil {
if ok && connectedUser != nil {
senderPID := connection.PID().LegacyValue()
senderConnectedUser := globals.ConnectedUsers[senderPID]
senderConnectedUser, ok := globals.ConnectedUsers.Get(senderPID)
senderFriendInfo := friends_wiiu_types.NewFriendInfo()
if ok && senderConnectedUser != nil {
senderFriendInfo := friends_wiiu_types.NewFriendInfo()
senderFriendInfo.NNAInfo = senderConnectedUser.NNAInfo
senderFriendInfo.Presence = senderConnectedUser.PresenceV2
status, err := database_wiiu.GetUserComment(senderPID)
if err != nil {
globals.Logger.Critical(err.Error())
senderFriendInfo.Status = friends_wiiu_types.NewComment()
senderFriendInfo.Status.LastChanged = types.NewDateTime(0)
} else {
senderFriendInfo.Status = status
senderFriendInfo.NNAInfo = senderConnectedUser.NNAInfo
senderFriendInfo.Presence = senderConnectedUser.PresenceV2
status, err := database_wiiu.GetUserComment(senderPID)
if err != nil {
globals.Logger.Critical(err.Error())
senderFriendInfo.Status = friends_wiiu_types.NewComment()
senderFriendInfo.Status.LastChanged = types.NewDateTime(0)
} else {
senderFriendInfo.Status = status
}
senderFriendInfo.BecameFriend = friendInfo.BecameFriend
senderFriendInfo.LastOnline = friendInfo.LastOnline // TODO - Change this
senderFriendInfo.Unknown = types.NewPrimitiveU64(0)
go notifications_wiiu.SendFriendRequestAccepted(connectedUser.Connection, senderFriendInfo)
}
senderFriendInfo.BecameFriend = friendInfo.BecameFriend
senderFriendInfo.LastOnline = friendInfo.LastOnline // TODO - Change this
senderFriendInfo.Unknown = types.NewPrimitiveU64(0)
go notifications_wiiu.SendFriendRequestAccepted(connectedUser.Connection, senderFriendInfo)
}
rmcResponseStream := nex.NewByteStreamOut(globals.SecureEndpoint.LibraryVersions(), globals.SecureEndpoint.ByteStreamSettings())

View File

@@ -117,9 +117,9 @@ func AddFriendRequest(err error, packet nex.PacketInterface, callID uint32, pid
friendInfo.LastOnline = types.NewDateTime(0)
friendInfo.Unknown = types.NewPrimitiveU64(0)
recipientClient := globals.ConnectedUsers[recipientPID]
recipientClient, ok := globals.ConnectedUsers.Get(recipientPID)
if recipientClient != nil {
if ok && recipientClient != nil {
friendRequestNotificationData := friends_wiiu_types.NewFriendRequest()
friendRequestNotificationData.PrincipalInfo = senderPrincipalInfo

View File

@@ -28,8 +28,8 @@ func CancelFriendRequest(err error, packet nex.PacketInterface, callID uint32, i
}
}
connectedUser := globals.ConnectedUsers[pid]
if connectedUser != nil {
connectedUser, ok := globals.ConnectedUsers.Get(pid)
if ok && connectedUser != nil {
// * This may send the friend removed notification, but they are the same.
go notifications_wiiu.SendFriendshipRemoved(connectedUser.Connection, connection.PID())
}

View File

@@ -28,8 +28,8 @@ func RemoveFriend(err error, packet nex.PacketInterface, callID uint32, pid *typ
}
}
connectedUser := globals.ConnectedUsers[pid.LegacyValue()]
if connectedUser != nil {
connectedUser, ok := globals.ConnectedUsers.Get(pid.LegacyValue())
if ok && connectedUser != nil {
go notifications_wiiu.SendFriendshipRemoved(connectedUser.Connection, pid)
}

View File

@@ -24,19 +24,21 @@ func UpdateAndGetAllInformation(err error, packet nex.PacketInterface, callID ui
// * Get user information
pid := connection.PID().LegacyValue()
connectedUser, ok := globals.ConnectedUsers.Get(pid)
if globals.ConnectedUsers[pid] == nil {
if !ok || connectedUser == nil {
// TODO - Figure out why this is getting removed
connectedUser := friends_types.NewConnectedUser()
connectedUser.PID = pid
connectedUser.Platform = friends_types.WUP
connectedUser.Connection = connection
// TODO - Find a clean way to create a NNAInfo?
globals.ConnectedUsers[pid] = connectedUser
globals.ConnectedUsers.Set(pid, connectedUser)
}
globals.ConnectedUsers[pid].NNAInfo = nnaInfo
globals.ConnectedUsers[pid].PresenceV2 = presence
connectedUser.NNAInfo = nnaInfo
connectedUser.PresenceV2 = presence
principalPreference, err := database_wiiu.GetUserPrincipalPreference(pid)
if err != nil {

View File

@@ -23,7 +23,9 @@ func UpdatePresence(err error, packet nex.PacketInterface, callID uint32, presen
presence.Online = types.NewPrimitiveBool(true) // * Force online status. I have no idea why this is always false
presence.PID = connection.PID() // * WHY IS THIS SET TO 0 BY DEFAULT??
if globals.ConnectedUsers[pid] == nil {
connectedUser, ok := globals.ConnectedUsers.Get(pid)
if !ok || connectedUser == nil {
// TODO - Figure out why this is getting removed
connectedUser := friends_types.NewConnectedUser()
connectedUser.PID = pid
@@ -31,10 +33,10 @@ func UpdatePresence(err error, packet nex.PacketInterface, callID uint32, presen
connectedUser.Connection = connection
// TODO - Find a clean way to create a NNAInfo?
globals.ConnectedUsers[pid] = connectedUser
globals.ConnectedUsers.Set(pid, connectedUser)
}
globals.ConnectedUsers[pid].PresenceV2 = presence
connectedUser.PresenceV2 = presence
notifications_wiiu.SendPresenceUpdate(presence)

View File

@@ -58,7 +58,7 @@ func RegisterEx(err error, packet nex.PacketInterface, callID uint32, vecMyURLs
urlPublic := types.NewString("")
if retval.IsSuccess() {
globals.ConnectedUsers[pid] = user
globals.ConnectedUsers.Set(pid, user)
localStation, _ := vecMyURLs.Get(0)

View File

@@ -30,12 +30,13 @@ func StartSecureServer() {
globals.SecureEndpoint.OnConnectionEnded(func(connection *nex.PRUDPConnection) {
pid := connection.PID().LegacyValue()
user, ok := globals.ConnectedUsers.Get(pid)
if globals.ConnectedUsers[pid] == nil {
if !ok || user == nil {
return
}
platform := globals.ConnectedUsers[pid].Platform
platform := user.Platform
lastOnline := types.NewDateTime(0)
lastOnline.FromTimestamp(time.Now())
@@ -55,7 +56,7 @@ func StartSecureServer() {
notifications_3ds.SendUserWentOfflineGlobally(connection)
}
delete(globals.ConnectedUsers, pid)
globals.ConnectedUsers.Delete(pid)
})
registerCommonSecureServerProtocols()

View File

@@ -46,9 +46,9 @@ func SendCommentUpdate(connection *nex.PRUDPConnection, comment string) {
}
friendsList.Each(func(i int, friend *friends_3ds_types.FriendRelationship) bool {
connectedUser := globals.ConnectedUsers[friend.PID.LegacyValue()]
connectedUser, ok := globals.ConnectedUsers.Get(friend.PID.LegacyValue())
if connectedUser != nil {
if ok && connectedUser != nil {
requestPacket, _ := nex.NewPRUDPPacketV0(globals.SecureEndpoint.Server, connection, nil)
requestPacket.SetType(constants.DataPacket)

View File

@@ -43,9 +43,9 @@ func SendFavoriteUpdate(connection *nex.PRUDPConnection, gameKey *friends_3ds_ty
}
friendsList.Each(func(i int, friend *friends_3ds_types.FriendRelationship) bool {
connectedUser := globals.ConnectedUsers[friend.PID.LegacyValue()]
connectedUser, ok := globals.ConnectedUsers.Get(friend.PID.LegacyValue())
if connectedUser != nil {
if ok && connectedUser != nil {
requestPacket, _ := nex.NewPRUDPPacketV0(globals.SecureEndpoint.Server, connection, nil)
requestPacket.SetType(constants.DataPacket)

View File

@@ -45,9 +45,9 @@ func SendMiiUpdateNotification(connection *nex.PRUDPConnection) {
}
friendsList.Each(func(i int, friend *friends_3ds_types.FriendRelationship) bool {
connectedUser := globals.ConnectedUsers[friend.PID.LegacyValue()]
connectedUser, ok := globals.ConnectedUsers.Get(friend.PID.LegacyValue())
if connectedUser != nil {
if ok && connectedUser != nil {
requestPacket, _ := nex.NewPRUDPPacketV0(globals.SecureEndpoint.Server, connection, nil)
requestPacket.SetType(constants.DataPacket)

View File

@@ -43,9 +43,9 @@ func SendPresenceUpdate(connection *nex.PRUDPConnection, presence *friends_3ds_t
}
friendsList.Each(func(i int, friend *friends_3ds_types.FriendRelationship) bool {
connectedUser := globals.ConnectedUsers[friend.PID.LegacyValue()]
connectedUser, ok := globals.ConnectedUsers.Get(friend.PID.LegacyValue())
if connectedUser != nil {
if ok && connectedUser != nil {
requestPacket, _ := nex.NewPRUDPPacketV0(globals.SecureEndpoint.Server, connection, nil)
requestPacket.SetType(constants.DataPacket)

View File

@@ -52,9 +52,9 @@ func SendUserWentOffline(connection *nex.PRUDPConnection, pid *types.PID) {
notificationRequestBytes := notificationRequest.Bytes()
connectedUser := globals.ConnectedUsers[pid.LegacyValue()]
connectedUser, ok := globals.ConnectedUsers.Get(pid.LegacyValue())
if connectedUser != nil {
if ok && connectedUser != nil {
requestPacket, _ := nex.NewPRUDPPacketV0(globals.SecureEndpoint.Server, connection, nil)
requestPacket.SetType(constants.DataPacket)

View File

@@ -69,9 +69,9 @@ func SendPresenceUpdate(presence *friends_wiiu_types.NintendoPresenceV2) {
}
friendPID := friend.NNAInfo.PrincipalBasicInfo.PID
connectedUser := globals.ConnectedUsers[friendPID.LegacyValue()]
connectedUser, ok := globals.ConnectedUsers.Get(friendPID.LegacyValue())
if connectedUser != nil {
if ok && connectedUser != nil {
requestPacket, _ := nex.NewPRUDPPacketV0(globals.SecureEndpoint.Server, connectedUser.Connection, nil)
requestPacket.SetType(constants.DataPacket)

View File

@@ -57,9 +57,9 @@ func SendUserWentOffline(connection *nex.PRUDPConnection, pid *types.PID) {
notificationRequestBytes := notificationRequest.Bytes()
connectedUser := globals.ConnectedUsers[pid.LegacyValue()]
connectedUser, ok := globals.ConnectedUsers.Get(pid.LegacyValue())
if connectedUser != nil {
if ok && connectedUser != nil {
requestPacket, _ := nex.NewPRUDPPacketV0(globals.SecureEndpoint.Server, connectedUser.Connection, nil)
requestPacket.SetType(constants.DataPacket)