/// <summary>
/// The connections we currently hold
/// </summary>
- ConcurrentDictionary<EndPoint, UdpServerConnection> allConnections = new ConcurrentDictionary<EndPoint, UdpServerConnection>();
+ private ConcurrentDictionary<EndPoint, UdpServerConnection> allConnections = new ConcurrentDictionary<EndPoint, UdpServerConnection>();
public int ConnectionCount { get { return this.allConnections.Count; } }
UdpServerConnection connection;
if (!this.allConnections.TryGetValue(remoteEndPoint, out connection))
{
- //Check for malformed connection attempts
- if (!isHello)
+ lock (this.allConnections)
{
- message.Recycle();
- Interlocked.Decrement(ref this.ActiveCallbacks);
- return;
- }
-
- if (AcceptConnection != null)
- {
- if (!AcceptConnection(out var response))
+ if (!this.allConnections.TryGetValue(remoteEndPoint, out connection))
{
- message.Recycle();
- SendData(response, response.Length, remoteEndPoint);
- Interlocked.Decrement(ref this.ActiveCallbacks);
- return;
+ //Check for malformed connection attempts
+ if (!isHello)
+ {
+ message.Recycle();
+ Interlocked.Decrement(ref this.ActiveCallbacks);
+ return;
+ }
+
+ if (AcceptConnection != null)
+ {
+ if (!AcceptConnection(out var response))
+ {
+ message.Recycle();
+ SendData(response, response.Length, remoteEndPoint);
+ Interlocked.Decrement(ref this.ActiveCallbacks);
+ return;
+ }
+ }
+
+ aware = false;
+ connection = new UdpServerConnection(this, (IPEndPoint)remoteEndPoint, this.IPMode);
+ if (!this.allConnections.TryAdd(remoteEndPoint, connection))
+ {
+ throw new HazelException("Failed to add a connection. This should never happen.");
+ }
}
}
-
- connection = this.allConnections.GetOrAdd(remoteEndPoint, (ep) =>
- {
- aware = false;
- return new UdpServerConnection(this, (IPEndPoint)ep, this.IPMode);
- });
}
//Inform the connection of the buffer (new connections need to send an ack back to client)
length,
SocketFlags.None,
endPoint,
- delegate (IAsyncResult result)
- {
- try
- {
- socket.EndSendTo(result);
- }
- catch { }
- },
+ SendCallback,
null
);
}
}
}
+ private void SendCallback(IAsyncResult result)
+ {
+ try
+ {
+ socket.EndSendTo(result);
+ }
+ catch { }
+ }
+
/// <summary>
/// Sends data from the listener socket.
/// </summary>
/// Represents a servers's connection to a client that uses the UDP protocol.
/// </summary>
/// <inheritdoc/>
- sealed class UdpServerConnection : UdpConnection
+ internal sealed class UdpServerConnection : UdpConnection
{
/// <summary>
/// The connection listener that we use the socket of.
/// created this connection and is hence the listener this conenction sends and receives via.
/// </remarks>
public UdpConnectionListener Listener { get; private set; }
-
+
/// <summary>
/// Creates a UdpConnection for the virtual connection to the endpoint.
/// </summary>