Fixe concurrent map read and map write.

This commit is contained in:
chteufleur 2017-06-28 19:44:57 +02:00
parent 4a3fdd3da1
commit 4961ad8c9e
4 changed files with 51 additions and 11 deletions

View File

@ -1,6 +1,8 @@
package gateway
import (
"sync"
"github.com/Philipp15b/go-steam"
)
@ -23,7 +25,7 @@ type GatewayInfo struct {
SteamLoginInfo *steam.LogOnDetails
SteamClient *steam.Client
SentryFile string
FriendSteamId map[string]*StatusSteamFriend
friendSteamId map[string]*StatusSteamFriend
SteamConnecting bool
Deleting bool
@ -35,6 +37,8 @@ type GatewayInfo struct {
XMPP_IQ_RemoteRoster_Request map[string]string
AllowEditRoster bool
ChatstateNotificationData chan string
sync.RWMutex
}
type StatusSteamFriend struct {
@ -68,3 +72,38 @@ func (g *GatewayInfo) Delete() {
g.Disconnect()
}
func (s *GatewayInfo) CreateSteamIds() {
s.friendSteamId = make(map[string]*StatusSteamFriend)
}
func (s *GatewayInfo) GetFriendSteamId(steamId string) *StatusSteamFriend {
s.RLock()
defer s.RUnlock()
return s.friendSteamId[steamId]
}
func (s *GatewayInfo) GetAllFriendSteamId() []string {
s.RLock()
defer s.RUnlock()
allSteamIds := make([]string, len(s.friendSteamId))
i := 0
for steamId := range s.friendSteamId {
allSteamIds[i] = steamId
i++
}
return allSteamIds
}
func (s *GatewayInfo) SetFriendSteamId(steamId string, status *StatusSteamFriend) {
s.Lock()
s.friendSteamId[steamId] = status
s.Unlock()
}
func (s *GatewayInfo) RemoveFriendSteamId(steamId string) {
s.Lock()
delete(s.friendSteamId, steamId)
s.Unlock()
}

View File

@ -127,15 +127,16 @@ func (g *GatewayInfo) mainSteam() {
status = Status_extended_away
tpye = Type_available
}
if _, ok := g.FriendSteamId[steamId]; !ok {
steamFriendId := g.GetFriendSteamId(steamId)
if steamFriendId == nil {
// Send subscribsion
g.SendXmppPresence(status, Type_subscribe, "", steamId+"@"+XmppJidComponent, gameName, name)
g.FriendSteamId[steamId] = &StatusSteamFriend{XMPP_Status: status, XMPP_Type: tpye}
g.SetFriendSteamId(steamId, &StatusSteamFriend{XMPP_Status: status, XMPP_Type: tpye})
} else {
g.FriendSteamId[steamId].XMPP_Status = status
g.FriendSteamId[steamId].XMPP_Type = tpye
g.FriendSteamId[steamId].SteamGameName = gameName
g.FriendSteamId[steamId].SteamName = name
steamFriendId.XMPP_Status = status
steamFriendId.XMPP_Type = tpye
steamFriendId.SteamGameName = gameName
steamFriendId.SteamName = name
}
g.SendXmppPresence(status, tpye, "", steamId+"@"+XmppJidComponent, gameName, name)
@ -226,9 +227,9 @@ func (g *GatewayInfo) SteamDisconnect() {
func (g *GatewayInfo) DisconnectAllSteamFriend() {
logger.Debug.Printf("[%s] Disconnect all Steam friend", g.XMPP_JID_Client)
for sid := range g.FriendSteamId {
for _, sid := range g.GetAllFriendSteamId() {
g.SendXmppPresence(Status_offline, Type_unavailable, "", sid+"@"+XmppJidComponent, "", "")
delete(g.FriendSteamId, sid)
g.RemoveFriendSteamId(sid)
}
}

View File

@ -63,7 +63,7 @@ func (g *GatewayInfo) ReceivedXMPP_Presence(presence *xmpp.Presence) {
}
if presence.Type == Type_probe {
steamFriendStatus := g.FriendSteamId[steamJid[0]]
steamFriendStatus := g.GetFriendSteamId(steamJid[0])
if steamFriendStatus != nil {
g.SendXmppPresence(steamFriendStatus.XMPP_Status, steamFriendStatus.XMPP_Type, "", steamJid[0]+"@"+XmppJidComponent, steamFriendStatus.SteamGameName, steamFriendStatus.SteamName)
}

View File

@ -277,7 +277,7 @@ func AddNewUser(jidUser, steamLogin, steamPwd string, debugMessage bool) {
g.SteamPassword = steamPwd
g.XMPP_JID_Client = jidUser
g.SentryFile = gateway.SentryDirectory + jidUser
g.FriendSteamId = make(map[string]*gateway.StatusSteamFriend)
g.CreateSteamIds()
g.Deleting = false
g.XMPP_Out = comp.Out