Skip to content
Open
Changes from all 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
19 changes: 10 additions & 9 deletions src/core/SIP/SIPParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//-----------------------------------------------------------------------------

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -55,7 +56,7 @@ public class SIPParameters
public char TagDelimiter = DEFAULT_PARAMETER_DELIMITER;

//[DataMember]
private Dictionary<string, string> m_dictionary;
private ConcurrentDictionary<string, string> m_dictionary;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the other comments, m_dictionary can be read-only:

Suggested change
private ConcurrentDictionary<string, string> m_dictionary;
private readonly ConcurrentDictionary<string, string> m_dictionary;


[IgnoreDataMember]
public int Count
Expand All @@ -65,7 +66,7 @@ public int Count

internal SIPParameters()
{
m_dictionary = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
m_dictionary = new ConcurrentDictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
}

/// <summary>
Expand Down Expand Up @@ -186,7 +187,7 @@ public static string[] GetKeyValuePairsFromQuoted(string quotedString, char deli
}
}

private void AddKeyValuePair(string keyValuePair, Dictionary<string, string> dictionary)
private void AddKeyValuePair(string keyValuePair, ConcurrentDictionary<string, string> dictionary)
{
if (keyValuePair != null && keyValuePair.Trim().Length > 0)
{
Expand All @@ -198,15 +199,15 @@ private void AddKeyValuePair(string keyValuePair, Dictionary<string, string> dic
// If this is not the parameter that is being removed put it back on.
if (!dictionary.ContainsKey(keyName))
{
dictionary.Add(keyName, keyValuePair.Substring(seperatorPosn + 1).Trim());
dictionary.TryAdd(keyName, keyValuePair.Substring(seperatorPosn + 1).Trim());
}
}
else
{
// Keys with no values are valid in SIP so they get added to the collection with a null value.
if (!dictionary.ContainsKey(keyValuePair))
{
dictionary.Add(keyValuePair, null);
dictionary.TryAdd(keyValuePair, null);
}
}
}
Expand All @@ -220,7 +221,7 @@ public void Set(string name, string value)
}
else
{
m_dictionary.Add(name, value);
m_dictionary.TryAdd(name, value);
}
}

Expand Down Expand Up @@ -259,13 +260,13 @@ public void Remove(string name)
{
if (name != null)
{
m_dictionary.Remove(name);
m_dictionary.TryRemove(name, out string ignore);
}
}

public void RemoveAll()
{
m_dictionary = new Dictionary<string, string>();
m_dictionary = new ConcurrentDictionary<string, string>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of instantiating a new instance keep the existing one and clear it:

Suggested change
m_dictionary = new ConcurrentDictionary<string, string>();
m_dictionary.Clear();

}

public string[] GetKeys()
Expand Down Expand Up @@ -318,7 +319,7 @@ public SIPParameters CopyOf()
{
SIPParameters copy = new SIPParameters();
copy.TagDelimiter = this.TagDelimiter;
copy.m_dictionary = (this.m_dictionary != null) ? new Dictionary<string, string>(this.m_dictionary) : new Dictionary<string, string>();
copy.m_dictionary = (this.m_dictionary != null) ? new ConcurrentDictionary<string, string>(this.m_dictionary) : new ConcurrentDictionary<string, string>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_dictionary is never null:

Suggested change
copy.m_dictionary = (this.m_dictionary != null) ? new ConcurrentDictionary<string, string>(this.m_dictionary) : new ConcurrentDictionary<string, string>();
copy.m_dictionary = (!this.m_dictionary.IsEmpty) ? new ConcurrentDictionary<string, string>(this.m_dictionary) : new ConcurrentDictionary<string, string>();

return copy;
}

Expand Down