From 7ad36a0321801cba642917ffb9ff5721d4703f03 Mon Sep 17 00:00:00 2001 From: chteufleur Date: Wed, 28 Jun 2017 21:07:59 +0200 Subject: [PATCH] Fixe concurrent map read and map write for XMPP client connected. --- gateway/gateway.go | 29 ++++++++++++++++++++++++++++- gateway/xmpp.go | 6 +++--- xmpp/xmpp.go | 2 +- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/gateway/gateway.go b/gateway/gateway.go index 364eb25..415963a 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -32,7 +32,7 @@ type GatewayInfo struct { // XMPP XMPP_JID_Client string XMPP_Out chan interface{} - XMPP_Connected_Client map[string]bool + xmpp_Connected_Client *XmppConnectedClient DebugMessage bool XMPP_IQ_RemoteRoster_Request map[string]string AllowEditRoster bool @@ -44,6 +44,11 @@ type FriendSteam struct { sync.RWMutex } +type XmppConnectedClient struct { + client map[string]bool + sync.RWMutex +} + type StatusSteamFriend struct { XMPP_Status string XMPP_Type string @@ -110,3 +115,25 @@ func (s *GatewayInfo) RemoveFriendSteamId(steamId string) { delete(s.friendSteamId.steamId, steamId) s.friendSteamId.Unlock() } + +func (s *GatewayInfo) CreateXmppConnectedClient() { + s.xmpp_Connected_Client = &XmppConnectedClient{client: make(map[string]bool)} +} + +func (s *GatewayInfo) SetXmppConnectedClient(jid string) { + s.xmpp_Connected_Client.Lock() + s.xmpp_Connected_Client.client[jid] = true + s.xmpp_Connected_Client.Unlock() +} + +func (s *GatewayInfo) RemoveXmppConnectedClient(jid string) { + s.xmpp_Connected_Client.Lock() + delete(s.xmpp_Connected_Client.client, jid) + s.xmpp_Connected_Client.Unlock() +} + +func (s *GatewayInfo) GetLenXmppConnectedClient() int { + s.xmpp_Connected_Client.RLock() + defer s.xmpp_Connected_Client.RUnlock() + return len(s.xmpp_Connected_Client.client) +} diff --git a/gateway/xmpp.go b/gateway/xmpp.go index 8fdc01e..8650f1c 100644 --- a/gateway/xmpp.go +++ b/gateway/xmpp.go @@ -56,9 +56,9 @@ func (g *GatewayInfo) ReceivedXMPP_Presence(presence *xmpp.Presence) { if len(jid) == 2 { // Resource exist —> client speaking if presence.Type == Type_available { - g.XMPP_Connected_Client[presence.From] = true + g.SetXmppConnectedClient(presence.From) } else if presence.Type == Type_unavailable { - delete(g.XMPP_Connected_Client, presence.From) + g.RemoveXmppConnectedClient(presence.From) } } @@ -79,7 +79,7 @@ func (g *GatewayInfo) ReceivedXMPP_Presence(presence *xmpp.Presence) { // Destination is gateway itself if presence.Type == Type_unavailable { // Disconnect - if len(g.XMPP_Connected_Client) <= 0 { + if g.GetLenXmppConnectedClient() <= 0 { g.Disconnect() } } else { diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index d06810e..3c5ff3c 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -281,7 +281,7 @@ func AddNewUser(jidUser, steamLogin, steamPwd string, debugMessage bool) { g.Deleting = false g.XMPP_Out = comp.Out - g.XMPP_Connected_Client = make(map[string]bool) + g.CreateXmppConnectedClient() g.DebugMessage = debugMessage g.XMPP_IQ_RemoteRoster_Request = make(map[string]string) g.AllowEditRoster = false