int main(int argc, char** argv) { ServerWorker *server = new MainServerWorker(8888); try { server->run(); } catch(ServerException &a) { Logger::instance().log(a.getMessage(), FATALERROR); } delete server; pthread_exit(NULL); }
void Server::incomingConnection( const qintptr socketHandle ) { QThread* workerThread = new QThread( this ); ServerWorker* worker = new ServerWorker( socketHandle ); worker->moveToThread( workerThread ); connect( workerThread, SIGNAL( started( )), worker, SLOT( initConnection( ))); connect( worker, SIGNAL( connectionClosed( )), workerThread, SLOT( quit( ))); // Make sure the thread will be deleted connect( workerThread, SIGNAL( finished( )), worker, SLOT( deleteLater( ))); connect( workerThread, SIGNAL( finished( )), workerThread, SLOT( deleteLater( ))); // public signals/slots, forwarding from/to worker connect( worker, SIGNAL( registerToEvents( QString, bool, deflect::EventReceiver* )), this, SIGNAL( registerToEvents( QString, bool, deflect::EventReceiver* ))); connect( this, SIGNAL( _pixelStreamerClosed( QString )), worker, SLOT( closeConnection( QString ))); connect( this, SIGNAL( _eventRegistrationReply( QString, bool )), worker, SLOT( replyToEventRegistration( QString, bool ))); // Commands connect( worker, SIGNAL( receivedCommand( QString, QString )), &_impl->commandHandler, SLOT( process( QString, QString ))); // PixelStreamDispatcher connect( worker, SIGNAL( addStreamSource( QString, size_t )), &_impl->pixelStreamDispatcher, SLOT(addSource( QString, size_t ))); connect( worker, SIGNAL( receivedSegement( QString, size_t, deflect::Segment )), &_impl->pixelStreamDispatcher, SLOT( processSegment( QString, size_t, deflect::Segment ))); connect( worker, SIGNAL( receivedFrameFinished( QString, size_t )), &_impl->pixelStreamDispatcher, SLOT( processFrameFinished( QString, size_t ))); connect( worker, SIGNAL( removeStreamSource( QString, size_t )), &_impl->pixelStreamDispatcher, SLOT( removeSource( QString, size_t ))); workerThread->start(); }
void DataPlaneServer::readyRead(int) { memset(&client_addr, 0, sizeof(struct sockaddr_storage)); /* Create BIO */ bio = BIO_new_dgram(fd, BIO_NOCLOSE); /* Set and activate timeouts */ timeout.tv_sec = 5; timeout.tv_usec = 0; BIO_ctrl(bio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout); ssl = SSL_new(ctx); SSL_set_bio(ssl, bio, bio); SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE); int dtlsRet; errno = 0; while ((dtlsRet = DTLSv1_listen(ssl, &client_addr)) <= 0) { if (errno != EAGAIN) { qWarning() << "DTLSv1_listen error"; qWarning() << SSL_get_error(ssl, dtlsRet); qWarning() << "Errno is" << errno; if (errno == EINVAL) { qWarning() << "!!!!!!!!!!! Your openssl library does not support DTLSv1_listen !!!!!!!!!!!"; qWarning() << "Cannot accept new connection"; SSL_shutdown(ssl); close(fd); SSL_free(ssl); ERR_remove_state(0); return; } } } QThread* workerThread = new QThread(); threads.append(workerThread); addrUnion infServer_addr; addrUnion infClient_addr; memcpy(&infServer_addr, &server_addr, sizeof(struct sockaddr_storage)); memcpy(&infClient_addr, &client_addr, sizeof(struct sockaddr_storage)); // get UID from friend using his IP to create worker thread // if IP is not in DB we close the connection char friendIp[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, &infClient_addr.s6.sin6_addr, friendIp, INET6_ADDRSTRLEN); ConnectionInitiator* init = ConnectionInitiator::getInstance(); QString friendUid = qSql->getUidFromIP(QHostAddress(QString(friendIp))); ControlPlaneConnection* cp = init->getConnection(friendUid); if (friendUid.isEmpty() || cp->getMode() == Closed) { qDebug() << "friendUId NOT in DB or no control plane connection!"; SSL_shutdown(ssl); close(fd); //free(info); SSL_free(ssl); ERR_remove_state(0); qDebug("done, connection closed."); fflush(stdout); return; } // associate with dataplaneconnection DataPlaneConnection* dpc = init->getDpConnection(friendUid); ServerWorker* worker = new ServerWorker(infServer_addr, infClient_addr, ssl, dpc); worker->moveToThread(workerThread); connect(workerThread, SIGNAL(started()), worker, SLOT(connection_handle())); connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater())); //connect(worker, SIGNAL(bufferReady(const char*, int)), dpc, SLOT(readBuffer(const char*, int))); UnixSignalHandler* u = UnixSignalHandler::getInstance(); connect(u, SIGNAL(exiting()), workerThread, SLOT(quit())); workerThread->start(); }