40 lines
966 B
C++
40 lines
966 B
C++
#include "TcpConnectionHandler.h"
|
|
#include "Log.h"
|
|
#include "TcpResponse.h"
|
|
|
|
TcpConnectionHandler::TcpConnectionHandler(QTcpSocket* socket, TcpRequestHandler* handler, QObject* parent)
|
|
: socket(socket)
|
|
, handler(handler)
|
|
, QObject(parent)
|
|
{
|
|
request = new TcpRequest();
|
|
response = new TcpResponse(socket);
|
|
connect(socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
|
|
connect(socket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
|
|
}
|
|
|
|
TcpConnectionHandler::~TcpConnectionHandler()
|
|
{
|
|
delete request;
|
|
delete response;
|
|
}
|
|
|
|
void TcpConnectionHandler::onReadyRead()
|
|
{
|
|
bool ret = request->readFromSocket(socket);
|
|
if (!ret) {
|
|
response->setUrl("Unkown Url");
|
|
response->error(request->getErrorString());
|
|
return;
|
|
}
|
|
handler->service(request, response);
|
|
}
|
|
|
|
void TcpConnectionHandler::onDisconnected()
|
|
{
|
|
Log::info("one client disconnected");
|
|
emit disconnected();
|
|
|
|
socket->close();
|
|
}
|