Ejemplo n.º 1
0
        public IConnection GetConnection(BigInteger?token)
        {
            IConnection connection = null;

            try
            {
                while (null == connection)
                {
                    // pick and initialize a new endpoint connection
                    IPAddress endpoint = _endpointStrategy.Pick(token);
                    if (null == endpoint)
                    {
                        throw new ArgumentException("Can't find any valid endpoint");
                    }

                    connection = _ip2Connection.GetOrAdd(endpoint, ep => CreateTransportOrMarkEndpointForRecovery(ep));
                    if (connection == null)
                    {
                        _ip2Connection.TryRemove(endpoint, out connection);
                    }
                }
                return(connection);
            }
            catch
            {
                connection.SafeDispose();
                throw;
            }
        }
Ejemplo n.º 2
0
        public void TestRandomness()
        {
            IPAddress[] ips = new IPAddress[4];
            for (byte i = 0; i < ips.Length; ++i)
            {
                ips[i] = new IPAddress(new byte[] { 192, 168, 0, i });
            }

            IEndpointStrategy endpointStrategy = ServiceActivator <Factory> .Create <IEndpointStrategy>("Random", ips.AsEnumerable());

            List <IPAddress> alls = new List <IPAddress>();

            for (int i = 0; i < 10000; ++i)
            {
                IPAddress nextEndpoint = endpointStrategy.Pick(null);
                if (!alls.Contains(nextEndpoint))
                {
                    alls.Add(nextEndpoint);
                }
            }

            foreach (IPAddress ip in alls)
            {
                Assert.IsTrue(alls.Contains(ip));
            }
        }
        public IConnection GetConnection(BigInteger?token, bool ForceCreateNew = false)
        {
            IConnection connection = null;

            try
            {
                while (null == connection)
                {
                    // pick and initialize a new endpoint connection
                    IPAddress endpoint = _endpointStrategy.Pick(token);
                    if (null == endpoint)
                    {
                        throw new ArgumentException("Can't find any valid endpoint");
                    }

                    var connections = _ip2Connection.GetOrAdd(endpoint, ep => new IConnection[] { });

                    var availableConnection = connections.Where(c => c.GetAvailableStreamIdCount > Transport.LongRunningConnection.SUGGEST_AVIALABLE_STREAM_COUNT).FirstOrDefault();
                    if (availableConnection != null)
                    {
                        return(availableConnection);
                    }

                    // cannot find enough availableStreamId connections, try to create one.
                    connection = CreateTransportOrMarkEndpointForRecovery(endpoint);

                    if (connection != null)
                    {
                        _ip2Connection.AddOrUpdate(endpoint,
                                                   ep =>
                        {
                            return(new IConnection[] { connection });
                        },
                                                   (ep, oldconnections) =>
                        {
                            var newConnections = new IConnection[oldconnections.Length + 1];
                            newConnections[0]  = connection;
                            for (int i = 0; i < oldconnections.Length; i++)
                            {
                                newConnections[i + 1] = oldconnections[i];
                            }
                            _logger.Info("endpoint {0} extend to connection count {1}", ep.ToString(), newConnections.Length);
                            return(newConnections);
                        });
                    }
                }
                return(connection);
            }
            catch
            {
                connection.SafeDispose();
                throw;
            }
        }
Ejemplo n.º 4
0
        public IConnection GetConnection(BigInteger?token)
        {
            lock (_globalLock)
            {
                IConnection connection = null;
                try
                {
                    while (null == connection)
                    {
                        // pick and initialize a new endpoint connection
                        IPAddress endpoint = _endpointStrategy.Pick(token);
                        if (null == endpoint)
                        {
                            throw new ArgumentException("Can't find any valid endpoint");
                        }

                        if (!_ip2Connection.TryGetValue(endpoint, out connection))
                        {
                            // try to create a new connection - if this fails, recover the endpoint
                            connection = CreateTransportOrMarkEndpointForRecovery(endpoint);
                            if (null != connection)
                            {
                                _ip2Connection.Add(endpoint, connection);
                            }
                        }
                    }

                    return(connection);
                }
                catch
                {
                    connection.SafeDispose();
                    throw;
                }
            }
        }