/// <summary> /// Close the context and free applicable resources. /// <para> /// If <seealso cref="OwnsAeronClient()"/> is true then the <seealso cref="Aeron()"/> client will be closed. /// </para> /// </summary> public void Dispose() { CloseHelper.QuietDispose(markFile); if (ownsAeronClient) { aeron?.Dispose(); } }
private AeronCluster(Context ctx) { _ctx = ctx; Subscription subscription = null; try { ctx.Conclude(); _aeron = ctx.Aeron(); _idleStrategy = ctx.IdleStrategy(); _nanoClock = _aeron.Ctx().NanoClock(); _isUnicast = ctx.ClusterMemberEndpoints() != null; UpdateMemberEndpoints(ctx.ClusterMemberEndpoints()); subscription = _aeron.AddSubscription(ctx.EgressChannel(), ctx.EgressStreamId()); _subscription = subscription; _publication = ConnectToCluster(); _clusterSessionId = OpenSession(); UnsafeBuffer headerBuffer = new UnsafeBuffer(new byte[SessionDecorator.SESSION_HEADER_LENGTH]); _sessionHeaderEncoder .WrapAndApplyHeader(headerBuffer, 0, _messageHeaderEncoder) .ClusterSessionId(_clusterSessionId) .Timestamp(Aeron.Aeron.NULL_VALUE); _vectors[0] = new DirectBufferVector(headerBuffer, 0, SessionDecorator.SESSION_HEADER_LENGTH); _vectors[1] = _messageBuffer; _poller = new Poller(ctx.SessionMessageListener(), _clusterSessionId, this); _fragmentAssembler = new FragmentAssembler(_poller); } catch (Exception) { if (!ctx.OwnsAeronClient()) { CloseHelper.QuietDispose(_publication); CloseHelper.QuietDispose(subscription); } CloseHelper.QuietDispose(ctx); throw; } }
private AeronCluster(Context ctx) { _ctx = ctx; Subscription subscription = null; Publication publication = null; try { ctx.Conclude(); _aeron = ctx.Aeron(); _lock = ctx.Lock(); _idleStrategy = ctx.IdleStrategy(); _nanoClock = _aeron.Ctx().NanoClock(); _isUnicast = ctx.ClusterMemberEndpoints() != null; subscription = _aeron.AddSubscription(ctx.EgressChannel(), ctx.EgressStreamId()); _subscription = subscription; publication = ConnectToCluster(); _publication = publication; _clusterSessionId = OpenSession(); } catch (Exception) { if (!ctx.OwnsAeronClient()) { CloseHelper.QuietDispose(publication); CloseHelper.QuietDispose(subscription); } CloseHelper.QuietDispose(ctx); throw; } }
private Publication ConnectToCluster() { Publication publication = null; string ingressChannel = _ctx.IngressChannel(); int ingressStreamId = _ctx.IngressStreamId(); long deadlineNs = _nanoClock.NanoTime() + _ctx.MessageTimeoutNs(); if (_isUnicast) { ChannelUri channelUri = ChannelUri.Parse(ingressChannel); int memberCount = _endpointByMemberIdMap.Count; Publication[] publications = new Publication[memberCount]; foreach (var entry in _endpointByMemberIdMap) { channelUri.Put(Aeron.Aeron.Context.ENDPOINT_PARAM_NAME, entry.Value); string channel = channelUri.ToString(); publications[entry.Key] = AddIngressPublication(channel, ingressStreamId); } int connectedIndex = -1; while (true) { for (int i = 0; i < memberCount; i++) { if (publications[i].IsConnected) { connectedIndex = i; break; } } if (-1 != connectedIndex) { for (int i = 0; i < memberCount; i++) { if (i == connectedIndex) { publication = publications[i]; } else { publications[i]?.Dispose(); } } break; } if (_nanoClock.NanoTime() > deadlineNs) { for (int i = 0; i < memberCount; i++) { CloseHelper.QuietDispose(publications[i]); } throw new TimeoutException("awaiting connection to cluster"); } _idleStrategy.Idle(); } } else { publication = AddIngressPublication(ingressChannel, ingressStreamId); _idleStrategy.Reset(); while (!publication.IsConnected) { if (_nanoClock.NanoTime() > deadlineNs) { CloseHelper.QuietDispose(publication); throw new TimeoutException("awaiting connection to cluster"); } _idleStrategy.Idle(); } } return(publication); }