RecordControlApplication/TcpConnectionHandler.cpp

39 lines
926 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->error(request->getErrorString());
return;
}
handler->service(request, response);
}
void TcpConnectionHandler::onDisconnected()
{
Log::info("one client disconnected");
emit disconnected();
socket->close();
}