private void EndPointOnOnDisconnected(SocketEndPoint endPoint, ConnectionId connectionId) { DisposeAndRemoveEndPoint(endPoint); // This should never be called from within a lock! EmitOnClientDisconnected(endPoint); }
private void DisposeAndRemoveEndPoint(SocketEndPoint endPoint) { endPoint?.TryDispose(); lock (_syncRoot) { _internalEndPoints.Remove(endPoint); _connectedEndPoints.Remove(endPoint); } }
private void OnIncomingConnection(IAsyncResult ar) { if (_isDisposed) { return; } SocketEndPoint endPoint = null; try { endPoint = CreateAndAddEndpoint(); endPoint.OnIncomingConnection(_serverSocket, ar); lock (_syncRoot) { // We need to check here again because if we've been // disposed of in the meantime, then we should close // this incoming connection again and shut down... if (_isDisposed) { DisposeAndRemoveEndPoint(endPoint); return; } _connectedEndPoints.Add(endPoint); } // This should never be called from within a lock! EmitOnClientConnected(endPoint); // DO NOT ADD ANY MORE CODE AFTER THIS LINE } catch (Exception e) { Log.ErrorFormat("{0}: Caught exception while accepting incoming connection: {1}", Name, e); DisposeAndRemoveEndPoint(endPoint); } finally { if (!_isDisposed) { BeginAccept(); } else { Log.DebugFormat("{0}: No longer calling BeginAccept(): We've been disposed of", Name); } } }
private void EndPointOnOnDisconnected(SocketEndPoint endPoint, ConnectionId connectionId) { lock (_syncRoot) { _internalEndPoints.Remove(endPoint); _connectedEndPoints.Remove(endPoint); } // This should never be called from within a lock! EmitOnClientDisconnected(endPoint); }
/// <summary> /// Creates a new endpoint, registers all currently known subjects /// with it and adds it to the <see cref="_internalEndPoints" /> list /// in one atomic operation. /// </summary> /// <returns></returns> private SocketEndPoint CreateAndAddEndpoint() { var endPoint = new SocketEndPoint(EndPointType.Server, _name, _clientAuthenticator, _serverAuthenticator, networkServiceDiscoverer: null, codeGenerator: _codeGenerator, heartbeatSettings: _heartbeatSettings, latencySettings: _latencySettings, endPointSettings: _endPointSettings); try { var stopwatch = Stopwatch.StartNew(); lock (_syncRoot) { foreach (var subjectRegistration in _subjects.Values) { subjectRegistration.RegisterSubjectWith(endPoint); } endPoint.OnDisconnected += (ep, connectionId) => EndPointOnOnDisconnected(endPoint, connectionId); _internalEndPoints.Add(endPoint); // DO NOT ADD ANYTHING ELSE AFTER HERE } stopwatch.Stop(); Log.DebugFormat("{0}: Registering subjects with newly created endpoint took {1}ms", _name, stopwatch.ElapsedMilliseconds); } catch (Exception) { endPoint.Dispose(); _internalEndPoints.Remove(endPoint); throw; } return(endPoint); }