예제 #1
0
        internal async Task ConnectSocketAsync(IPAddress address, int port)
        {
            InitClient();

#if NET46
            var connectTask = Task.Factory.FromAsync(_client.BeginConnect, _client.EndConnect, address, port, null);
#else
            var connectTask = _client.ConnectAsync(address, port);
#endif
            var finishedTask = await Task.WhenAny(connectTask, Task.Delay(_connectionTimeout)).ConfigureAwait(false);

            if (connectTask != finishedTask) // timed out
            {
                try
                {
                    // close client immediately when failed to connect within timeout
                    await DisconnectAsync().ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    _logger?.Error(e, $"Failed to close connect to the server {address}:{port}" +
                                   $" after connection timed out {_connectionTimeout.TotalMilliseconds}ms.");
                }

                throw new OperationCanceledException(
                          $"Failed to connect to server {address}:{port} within {_connectionTimeout.TotalMilliseconds}ms.");
            }

            await connectTask.ConfigureAwait(false);
        }
예제 #2
0
        public async Task ReceiveOneAsync(IResponsePipeline responsePipeline)
        {
            try
            {
                await Reader.ReadAsync(responsePipeline).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger?.Error(ex, $"Unable to read message from server {_uri}, connection will be terminated.");
                await StopAsync().ConfigureAwait(false);

                throw;
            }

            // We force ProtocolException's to be thrown here to shortcut the communication with the server
            try
            {
                responsePipeline.AssertNoProtocolViolation();
            }
            catch (ProtocolException exc)
            {
                _logger?.Warn(exc, "A bolt protocol error has occurred with server {0}, connection will be terminated.",
                              _uri.ToString());
                await StopAsync().ConfigureAwait(false);

                throw;
            }
        }
예제 #3
0
 public void ReceiveOne(IMessageResponseHandler responseHandler)
 {
     try
     {
         Reader.Read(responseHandler);
     }
     catch (Exception ex)
     {
         _logger?.Error(ex, $"Unable to read message from server {_uri}, connection will be terminated.");
         Stop();
         throw;
     }
     if (responseHandler.HasProtocolViolationError)
     {
         _logger?.Warn(responseHandler.Error, $"Received bolt protocol error from server {_uri}, connection will be terminated.");
         Stop();
         throw responseHandler.Error;
     }
 }
예제 #4
0
 public static async Task <T> TryExecuteAsync <T>(IDriverLogger logger, Func <Task <T> > func, string message = null)
 {
     try
     {
         return(await func().ConfigureAwait(false));
     }
     catch (Exception ex)
     {
         logger?.Error(ex, message);
         throw;
     }
 }
예제 #5
0
 public static T TryExecute <T>(IDriverLogger logger, Func <T> func, string message = null)
 {
     try
     {
         return(func());
     }
     catch (Exception ex)
     {
         logger?.Error(ex, message);
         throw;
     }
 }
예제 #6
0
 public static void TryExecute(IDriverLogger logger, Action action, string message = null)
 {
     try
     {
         action();
     }
     catch (Exception ex)
     {
         logger?.Error(ex, message);
         throw;
     }
 }
예제 #7
0
        internal void ConnectSocket(IPAddress address, int port)
        {
            InitClient();

            using (var cts = new CancellationTokenSource(_connectionTimeout))
            {
#if NET452
                try
                {
                    _client.Connect(address, port);
                }
                catch (SocketException ex) when(ex.Message.Contains(
                                                    "An address incompatible with the requested protocol was used"))
                {
                    throw new NotSupportedException("This protocol version is not supported.");
                }
#else
                _client.ConnectAsync(address, port).ConfigureAwait(false).GetAwaiter().GetResult();
#endif

                if (cts.IsCancellationRequested)
                {
                    try
                    {
                        // close client immediately when failed to connect within timeout
                        Disconnect();
                    }
                    catch (Exception e)
                    {
                        _logger?.Error(e, $"Failed to close connect to the server {address}:{port}" +
                                       $" after connection timed out {_connectionTimeout.TotalMilliseconds}ms.");
                    }

                    throw new OperationCanceledException(
                              $"Failed to connect to server {address}:{port} within {_connectionTimeout.TotalMilliseconds}ms.",
                              cts.Token);
                }
            }
        }
예제 #8
0
        public async Task <IRoutingTable> UpdateRoutingTableAsync(ISet <Uri> triedUris = null,
                                                                  Func <IConnection, Task <IRoutingTable> > rediscoveryFunc = null)
        {
            var knownRouters = _routingTable.Routers;

            foreach (var router in knownRouters)
            {
                triedUris?.Add(router);
                try
                {
                    var conn = await _poolManager.CreateClusterConnectionAsync(router).ConfigureAwait(false);

                    if (conn == null)
                    {
                        _routingTable.Remove(router);
                    }
                    else
                    {
                        var newRoutingTable = await _discovery.DiscoverAsync(conn).ConfigureAwait(false);

                        if (!IsRoutingTableStale(newRoutingTable))
                        {
                            return(newRoutingTable);
                        }
                    }
                }
                catch (SecurityException e)
                {
                    _logger?.Error(e,
                                   "Failed to update routing table from server '{0}' because of a security exception.", router);
                    throw;
                }
                catch (Exception e)
                {
                    _logger?.Warn(e, "Failed to update routing table from server '{0}'.", router);
                }
            }

            return(null);
        }
예제 #9
0
 public void Error(Exception cause, string message, params object[] args)
 {
     _delegate?.Error(cause, Reformat(message), args);
 }