• Main Page
  • Classes
  • Files
  • File List
  • File Members

lib/bfHttpServer/src/httplistener.cpp

Go to the documentation of this file.
00001 
00006 #include "httplistener.h"
00007 #include "httpconnectionhandler.h"
00008 #include "httpconnectionhandlerpool.h"
00009 #include <QCoreApplication>
00010 
00011 HttpListener::HttpListener(QSettings* settings, HttpRequestHandler* requestHandler, QObject *parent)
00012     : QTcpServer(parent), pool(settings,requestHandler)
00013 {
00014     Q_ASSERT(settings!=0);
00015     this->settings=settings;
00016     // Start listening
00017     int port=settings->value("port").toInt();
00018     listen(QHostAddress::Any, port);
00019     if (!isListening()) {
00020         qCritical("HttpListener: Cannot bind on port %i: %s",port,qPrintable(errorString()));
00021     }
00022     else {
00023         qDebug("HttpListener: Listening on port %i",port);
00024     }
00025 }
00026 
00027 HttpListener::~HttpListener() {
00028     close();
00029     qDebug("HttpListener: closed");
00030 }
00031 
00032 
00033 void HttpListener::incomingConnection(int socketDescriptor) {
00034 #ifdef SUPERVERBOSE
00035     qDebug("HttpListener: New connection");
00036 #endif
00037     HttpConnectionHandler* freeHandler=pool.getConnectionHandler();
00038 
00039     // Let the handler process the new connection.
00040     if (freeHandler) {
00041         // The descriptor is passed via signal/slot because the handler lives in another
00042         // thread and cannot open the socket when called by another thread.
00043         connect(this,SIGNAL(handleConnection(int)),freeHandler,SLOT(handleConnection(int)));
00044         emit handleConnection(socketDescriptor);
00045         disconnect(this,SIGNAL(handleConnection(int)),freeHandler,SLOT(handleConnection(int)));
00046     }
00047     else {
00048         // Reject the connection
00049         qDebug("HttpListener: Too many connections");
00050         QTcpSocket* socket=new QTcpSocket(this);
00051         socket->setSocketDescriptor(socketDescriptor);
00052         connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
00053         socket->write("HTTP/1.1 503 too many connections\r\nConnection: close\r\n\r\nToo many connections\r\n");
00054         socket->disconnectFromHost();
00055     }
00056 }

Generated on Mon Dec 26 2011 12:09:22 for QtWebApp by  doxygen 1.7.1