protected override void OnAbort()
 {
     try
     {
         channel?.Dispose();
     }
     catch (ObjectDisposedException) { /* no-op */ }
     catch (OperationCanceledException) { /* no-op */ }
     catch (DvcChannelDisconnectedException) { /* no-op */ }
     channel = null;
 }
Exemplo n.º 2
0
        // Called from DVC API
        public async void AcceptConnection(IAsyncDvcChannel channel)
        {
            ServiceRegistration existingRegistration;

            try
            {
                var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));

                // Read service name
                var serviceNameUtf8 = await channel.ReadMessageAsync(cts.Token).ConfigureAwait(false);

                var serviceName = Encoding.UTF8.GetString(serviceNameUtf8);

                // Lookup service
                lock (syncRegisteredServices)
                {
                    if (!RegisteredServices.TryGetValue(serviceName, out existingRegistration))
                    {
                        throw new ChannelNotAvailableException($"Service '{serviceName}' not registered");
                    }
                }

                // Accept
                await channel.SendMessageAsync(Encoding.UTF8.GetBytes("ACCEPTED")).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Exception trying to accept a new brokered connection");

                try
                {
                    await channel.SendMessageAsync(Encoding.UTF8.GetBytes("ERROR Service not registered")).ConfigureAwait(false);

                    channel.Dispose();
                }
                catch (OperationCanceledException) { /* no-op */ }
                catch (ObjectDisposedException) { /* no-op */ }
                catch (DvcChannelDisconnectedException) { /* no-op */ }
                catch (Exception ex2)
                {
                    Logger.Error(ex2, "Could not notify RDS endpoint of failed conncetion attempt");
                }
                return;
            }

            if (!existingRegistration.TryAcceptConnection(channel))
            {
                KillRegistration(existingRegistration);
            }
        }