コード例 #1
0
 public static void RemoveProxy(SSHProxy proxy)
 {
     Initialize();
     lock (_lock)
     {
         _proxies.Remove(proxy);
     }
 }
コード例 #2
0
 private static void UseProxy(SSHProxy proxy, Action <SSHProxy> action)
 {
     try
     {
         action(proxy);
     }
     catch
     {
         throw;
     }
     finally
     {
         proxy.EndUse();
     }
 }
コード例 #3
0
        public static bool TryUseProxy(string server, int port, string sshServer, int sshPort, SshProxyCredentials credentials, Action <SSHProxy> action, out string error)
        {
            error = null;

            Initialize();

            SSHProxy proxy = null;

            if (TryAcquireExistingProxy(server, port, out proxy))
            {
                UseProxy(proxy, action);
                return(true);
            }
            else
            {
                proxy = new SSHProxy(server, port, sshServer, sshPort, credentials);

                lock (_lock)
                {
                    _proxies.Add(proxy);
                }

                Task.Run(() => proxy.Run());
                proxy.WaitUntilStarted();

                if (proxy.RequestUse())
                {
                    UseProxy(proxy, action);
                    return(true);
                }
                else
                {
                    error = proxy.Error;
                    return(false);
                }
            }
        }
コード例 #4
0
        private static bool TryAcquireExistingProxy(string server, int port, out SSHProxy proxy)
        {
            proxy = null;

            for (int i = 0; i < 4; i++)
            {
                lock (_lock)
                {
                    // try to reuse an existing proxy
                    var validProxies = _proxies.Where(p => p.Server == server && p.Port == port).ToList();

                    foreach (var validProxy in validProxies)
                    {
                        if (validProxy.RequestUse())
                        {
                            proxy = validProxy;
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }