Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
    int i, ret = 1;
    char *url, *host, *js;
    struct timeval tv;
    notifier_t n[2];

#if defined(_WIN32) || defined(__CYGWIN__)
    WSADATA wsaData;
    if (WSAStartup(0x202, &wsaData)) {
        fprintf(stderr, "Failed to initialize WSA\n");
        exit(1);
    }
#endif

    if (argc <= 2 || argc % 2 != 0)
        usage(argv[0]);

    js = read_pacfile(argv[1]);
    if (!js)
        goto out;

    if (create_notifier(n) < 0) {
        fprintf(stderr, "Failed to create notifier\n");
        goto out;
    }

    pac = pac_init(js, 16, notify, (void *)(long)n[1]);

    for (i = 2; i < argc; i += 2) {
        url = argv[i];
        host = argv[i + 1];
        pac_find_proxy(pac, url, host, proxy_found, NULL);
        tv.tv_sec = 0;
        tv.tv_usec = 0;
        if (is_notified(n[0], &tv))
            pac_run_callbacks(pac);
    }

    i = 0;
    while (finished < argc / 2 - 1) {
        tv.tv_sec = 0;
        tv.tv_usec = 10000;
        if (is_notified(n[0], &tv))
            pac_run_callbacks(pac);
        if (++i > 60 * 100)
            goto out;
    }

    ret = 0;

out:
    free(js);
    return ret;
}
Ejemplo n.º 2
0
void
tcp_client::disconnect(void) {
    if (not m_is_connected)
        throw redis_error("Not connected");

    m_is_connected = false;

    std::mutex close_socket_mutex;
    std::condition_variable close_socket_cond_var;
    std::unique_lock<std::mutex> lock(close_socket_mutex);

    std::atomic_bool is_notified(false);
    m_io_service.post([this, &close_socket_cond_var, &is_notified]() {
        m_socket.close();

        is_notified = true;
        close_socket_cond_var.notify_one();
    });

    if (not is_notified)
      close_socket_cond_var.wait(lock);
}
Ejemplo n.º 3
0
void
tcp_client::connect(const std::string& host, unsigned int port) {
    if (m_is_connected)
        throw redis_error("Already connected");

    std::condition_variable conn_cond_var;

    //! resolve host name
    boost::asio::ip::tcp::resolver resolver(m_io_service.get());
    boost::asio::ip::tcp::resolver::query query(host, std::to_string(port));
    boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query);
    boost::asio::ip::tcp::endpoint endpoint = iter->endpoint();

    //! async connect
    std::atomic_bool is_notified(false);
    m_socket.async_connect(endpoint, [&](boost::system::error_code error) {
        if (not error) {
            m_is_connected = true;
            async_read();
        }

        is_notified = true;
        conn_cond_var.notify_one();
    });

    //! start loop and wait for async connect result
    std::mutex conn_mutex;
    std::unique_lock<std::mutex> lock(conn_mutex);
    m_io_service.run();

    if (not is_notified)
      conn_cond_var.wait(lock);

    if (not m_is_connected)
        throw redis_error("Fail to connect to " + host + ":" + std::to_string(port));
}