From 4961ad8c9ecb54f5d2b8e8a069df7167d35eefe1 Mon Sep 17 00:00:00 2001 From: chteufleur Date: Wed, 28 Jun 2017 19:44:57 +0200 Subject: [PATCH] Fixe concurrent map read and map write. --- gateway/gateway.go | 41 ++++++++++++++++++++++++++++++++++++++++- gateway/steam.go | 17 +++++++++-------- gateway/xmpp.go | 2 +- xmpp/xmpp.go | 2 +- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/gateway/gateway.go b/gateway/gateway.go index bb90a8b..406b614 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -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() +} diff --git a/gateway/steam.go b/gateway/steam.go index 3d67a14..c053e94 100644 --- a/gateway/steam.go +++ b/gateway/steam.go @@ -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) } } diff --git a/gateway/xmpp.go b/gateway/xmpp.go index 50805a4..8fdc01e 100644 --- a/gateway/xmpp.go +++ b/gateway/xmpp.go @@ -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) } diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index 94151e8..d06810e 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -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