Example #1
0
bool RedisClient::SshTransporter::connectToHost()
{
    ConnectionConfig config = m_connection->getConfig();

    if (config.isSshPasswordUsed())
        m_sshClient->setPassphrase(config.sshPassword());

    QString privateKey = config.getSshPrivateKey();

    if (!privateKey.isEmpty()) {
        m_sshClient->setKeyFiles("", privateKey);
    }    

    //connect to ssh server
    SignalWaiter waiter(config.connectionTimeout());
    waiter.addAbortSignal(this, &RedisClient::SshTransporter::errorOccurred);
    waiter.addSuccessSignal(m_sshClient.data(), &QxtSshClient::connected);

    emit logEvent("Connecting to SSH host...");

    m_sshClient->connectToHost(config.sshUser(), config.sshHost(), config.sshPort());

    if (!waiter.wait()) {
        emit errorOccurred("Cannot connect to SSH host");
        return false;
    }

    emit logEvent("SSH tunnel established. Connecting to redis-server...");    

    return openTcpSocket();
}
Example #2
0
void RedisClient::SshTransporter::reconnect()
{
    emit logEvent("Reconnect to host");

    if (m_loopTimer->isActive())
        m_loopTimer->stop();

    if (m_socket) {
        m_socket->close();
        QObject::disconnect(m_socket, 0, 0, 0);
        delete m_socket;
        m_socket = nullptr;
    }

    if (openTcpSocket()) {
        resetDbIndex();
        m_loopTimer->start();
    }
}
Example #3
0
struct conn* createConnection(const char* ip_address, int port, int protocol, int naggles) 
{
	struct conn* connection = malloc(sizeof(struct conn));
	memset(connection, 0, sizeof(struct conn));

	if(protocol == UDP_MODE) {
		connection->sock = openUdpSocket(ip_address, port);
		connection->protocol = UDP_MODE;
	} 
	else {
		connection->sock = openTcpSocket(ip_address, port);
		connection->protocol = TCP_MODE;

		if(!naggles) {
			//Disable naggle's algorithm
			int flag = 1;
			int result = setsockopt(connection->sock,/* socket affected */
			                        IPPROTO_TCP,     /* set option at TCP level */
			                        TCP_NODELAY,     /* name of option */
			                        (char *) &flag,  /* the cast is historical cruft */
			                         sizeof(int));   /* length of option value */
			if (result < 0){
				printf("couldn't set tcp_nodelay\n");
				exit(-1);
			}
		}
	}

	static int uid_gen;
	connection->uid = uid_gen;
	uid_gen++;

	if (verbose) {
	    printf("Created connection on fd %d, uid %d\n", connection->sock, connection->uid);
	}

	return connection;
}