void CDaemon::newConnection()
{
	while(m_server->hasPendingConnections())
	{
		QTcpSocket * client = m_server->nextPendingConnection();
		connect(client, SIGNAL(readyRead()), this, SLOT(handleClientRead()));
		//connect(client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(handleClientDisconnected()));
		connect(client, SIGNAL(disconnected()), this, SLOT(handleClientDisconnected()));
		m_clients.append(client);
	}
}
Ejemplo n.º 2
0
void
runServer(void *arg) {
    struct memcachedServer *server = (struct memcachedServer *)arg;

    fd_set rfds, master;
    FD_ZERO(&rfds);
    FD_ZERO(&master);

    FD_SET(server->sockfd, &master);

    int sfd, new_fd, fd_max, first_conn_fd = 0; 
    int ii;
    fd_max = server->sockfd;
    makeSocketNonBlocking(server->sockfd);
    rfds = master;

    while(1) {
        if(select(fd_max+1, &rfds, NULL, NULL, NULL) < 0){
            fprintf(stderr, "error on select");
            exit(1);
        }

        fprintf(stderr, "\n Got a new event on the server socket!!");

        for (ii = 0; ii <= fd_max; ii++) {

            if (FD_ISSET(ii, &rfds)) {
                if (ii == server->sockfd) {
                    fprintf(stderr, "\n Accepting a new connection on port %d", server->port);

                    //Accept new client connection
                    struct sockaddr_in caddr;
                    socklen_t addrlen;

                    new_fd = accept(server->sockfd, (struct sockaddr *)&caddr, &addrlen);
                    if(new_fd < 0) {
                        fprintf(stderr, "Unable to accept new connection");
                        exit(1);
                    } else {
                        fprintf(stderr, "\n New connection %d", new_fd);
                    }

                    //store the starting fd for connections
                    if(server->startfd == 0) {
                        server->startfd = new_fd;
                    }

                    conn *c = conn_new(new_fd);

                    if(server->ctx) {
                        SSL *ssl = SSL_new(server->ctx);
                        SSL_set_accept_state(ssl);
                        SSL_set_fd(ssl, new_fd);

                        fprintf(stderr, "\n Trying the SSL Handshake on fd %d", new_fd);

                        //Doing a blocking SSL handshake 
                        int ret = SSL_accept(ssl);
                        if (ret != 1) {
                            int err = SSL_get_error(ssl, ret);
                            while (err == SSL_ERROR_WANT_READ) {
                                ret = SSL_accept(ssl);
                                err = SSL_get_error(ssl, ret);
                            }
                            if(err == SSL_ERROR_ZERO_RETURN || err == SSL_ERROR_SYSCALL) {
                                fprintf(stderr, "\n SSL library errored");
                                //destroy_conn(c);
                                close(new_fd);
                                continue;
                            }

                            if(err == SSL_ERROR_NONE) {
                                fprintf(stderr, "\n Successfully completed SSL handshake");
                            }

                            sslObjs[counter_ssl_objects] = ssl;
                            fprintf(stderr, "\n Successfully loaded fd %d", new_fd - server->startfd);
                        }
                    }
                    makeSocketNonBlocking(new_fd);
                    if (new_fd > fd_max) {
                        fd_max = new_fd;
                    }
                    FD_SET(new_fd, &master);
                } else {
                    fprintf(stderr, "Got an event from the connections \n");
                    //close(ii);
                    //FD_CLR(ii, &master);

                    //read request from client socket 
                    if(server->ctx) {
                        int j = 0;
                        for(j = 0; j < MAX_CLIENT_CONNS ; j++) { 
                            if(SSL_get_fd(sslObjs[j])  == ii) {
                                if(handleSSLClientRead(sslObjs[j]) < 0) {
                                    fprintf(stderr, "\n Client read failed");
                                    close(ii);
                                    FD_CLR(ii, &master);
                                }
                                break;
                            }
                        }
                    } else { 
                        if(handleClientRead(ii) < 0) {
                            close(ii);
                            FD_CLR(ii, &master);
                        }
                    }

                }
            }
        }

        //reload the read fds as select changes the fds
        rfds = master;
    }
}