Whenever I attempt to retrieve a value from a ConcurrentDictionary there is no items within the dictionary. Take the following code (shortened to make the question easier to answer):
public void AddSession(EndPoint remoteEndPoint, IPlayerSession session)
{
this.logger.LogDebug($"Adding player session to {nameof(WorldContext)}...");
if (!this.endPointToSessionMap.TryAdd(remoteEndPoint, session))
{
this.logger.LogCritical($"The player session could not been added to the {nameof(WorldContext)}!");
}
}
Here, we can see that a critical log will be displayed if there was an issue. My log level is set to Debug so I should have no issues seeing the log if it occurs.
Now, take note of the following function used to retrieve a player session by it's remoteEndPoint:
public IPlayerSession? FindSessionByRemoteEndPoint(EndPoint remoteEndPoint)
{
if (!this.endPointToSessionMap.TryGetValue(remoteEndPoint, out var session))
{
return null;
}
return session;
}
This function is then used later on to find a session and then remove it. I use the function here like so:
public void RemoveConnection(EndPoint remoteEndPoint)
{
this.logger.LogDebug($"Removing TCP client connection from the {nameof(ServerContext)}...");
// Find the session.
var session = this.worldContext.FindSessionByRemoteEndPoint(remoteEndPoint);
// If it's not null, remove it.
if (session != null)
{
// This function is never invoked.
this.worldContext.RemoveSession(remoteEndPoint);
}
if (!this.endPointToConnectionMap.TryRemove(remoteEndPoint, out _))
{
this.logger.LogCritical($"The {nameof(ServerContext)} failed to removed a TCP client connection!");
}
}
However, for some reason the RemoveSession function is never invoked as endPointToSessionMap has no items.