305 lines
7.5 KiB
C++
Executable File
305 lines
7.5 KiB
C++
Executable File
|
|
|
|
|
|
#include "workerbase.h"
|
|
|
|
#include "commandinterface.h"
|
|
#include "tcpserver.h"
|
|
#include "tcpserver_logger.h"
|
|
|
|
#include <QtCore/QList>
|
|
#include <QtCore/QThread>
|
|
#include <QtCore/QCoreApplication>
|
|
|
|
#include <QtNetwork/QAbstractSocket>
|
|
#include <QtNetwork/QTcpSocket>
|
|
#include <ngltools/ngltools_convert.h>
|
|
|
|
#include <typeinfo>
|
|
|
|
WorkerBase::WorkerBase(qintptr sd, unsigned short port)
|
|
: m_socketDescriptor(sd)
|
|
, m_connection(nullptr)
|
|
, m_port(port)
|
|
, m_bRedirectIncomingData(false)
|
|
, m_bRedirectDataHandler(nullptr)
|
|
{}
|
|
|
|
QAbstractSocket *WorkerBase::connection()
|
|
{
|
|
return m_connection;
|
|
}
|
|
|
|
const QAbstractSocket *WorkerBase::connection() const
|
|
{
|
|
return m_connection;
|
|
}
|
|
|
|
void WorkerBase::newDataInternal()
|
|
{
|
|
|
|
if (m_bRedirectIncomingData)
|
|
{
|
|
if (m_bRedirectDataHandler)
|
|
m_bRedirectDataHandler->newPlainData();
|
|
return;
|
|
}
|
|
|
|
if (!m_connection || !m_connection->isOpen())
|
|
return;
|
|
|
|
while (m_connection && m_connection->canReadLine())
|
|
{
|
|
QByteArray ba(2048, '\0');
|
|
bool bGotEOL = false;
|
|
int iOffset = 0;
|
|
do
|
|
{
|
|
qint64 length = m_connection->readLine(ba.data() + iOffset, ba.size());
|
|
if (length < 0)
|
|
{
|
|
TCPSERVER_ERROR("Error reading data from " << m_connString.constData()
|
|
<< qPrintable(m_connection->errorString()))
|
|
return;
|
|
}
|
|
bGotEOL = ba.contains('\n');
|
|
if (bGotEOL)
|
|
{
|
|
|
|
ba.resize(iOffset + length);
|
|
}
|
|
else
|
|
{
|
|
|
|
iOffset = ba.size();
|
|
ba.resize(ba.size() * 2);
|
|
|
|
memset(ba.data() + iOffset, 0, ba.size() - iOffset);
|
|
}
|
|
}
|
|
while (!bGotEOL);
|
|
|
|
|
|
while (ba.endsWith('\r') || ba.endsWith('\n'))
|
|
{
|
|
ba.data() [ba.size() - 1] = '\0';
|
|
ba.resize(ba.size() - 1);
|
|
}
|
|
|
|
int idx = ba.indexOf(' ');
|
|
if (idx == -1)
|
|
idx = ba.size();
|
|
|
|
QByteArray command = ba.left(idx);
|
|
if (command.isEmpty())
|
|
{
|
|
TCPSERVER_DEBUG("Got empty command from connection " << m_connString.constData())
|
|
continue;
|
|
}
|
|
|
|
CommandMap::ConstIterator it = m_registeredCommands.constFind(command.toLower());
|
|
if (it == m_registeredCommands.constEnd())
|
|
{
|
|
TCPSERVER_WARN("Got unknown command " << formatForDebug(command).constData()
|
|
<< " from connection "
|
|
<< m_connString.constData())
|
|
m_connection->write("UNKNOWN COMMAND ");
|
|
m_connection->write(command);
|
|
m_connection->write("\n");
|
|
if (!m_connection->waitForBytesWritten(1000))
|
|
{
|
|
TCPSERVER_WARN("waitForBytesWritten returned false for connection "
|
|
<< m_connString.constData()
|
|
<< " "
|
|
<< m_connection->error()
|
|
<< " "
|
|
<< m_connection->errorString())
|
|
}
|
|
}
|
|
else
|
|
{
|
|
TCPSERVER_DEBUG("Got command " << formatForDebug(command).constData()
|
|
<< " from connection "
|
|
<< m_connString.constData())
|
|
|
|
CommandInterface *interface = *it;
|
|
const QVector<QByteArray> args = NGL::parseFromTelnet(ba, idx + 1);
|
|
interface->handleCommand(args);
|
|
if (interface->errorOccured())
|
|
{
|
|
TCPSERVER_ERROR("Write error occured on connection " << m_connString.constData())
|
|
}
|
|
}
|
|
|
|
if (m_bRedirectIncomingData && m_connection && m_connection->bytesAvailable())
|
|
{
|
|
if (m_bRedirectDataHandler)
|
|
m_bRedirectDataHandler->newPlainData();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void WorkerBase::redirectDataHandling(bool bDoRedirect, CommandInterface *newDataHandler)
|
|
{
|
|
m_bRedirectIncomingData = bDoRedirect;
|
|
m_bRedirectDataHandler = newDataHandler;
|
|
|
|
if (!bDoRedirect)
|
|
newDataInternal();
|
|
}
|
|
|
|
QByteArray WorkerBase::formatForDebug(const QByteArray &data)
|
|
{
|
|
const int size = data.size();
|
|
QByteArray ret;
|
|
ret.reserve(size);
|
|
for (int i = 0; i < size; ++i)
|
|
{
|
|
const unsigned char c = data[i];
|
|
if (c < ' ' || c >= 0x7f)
|
|
ret += "\\x" + QByteArray::number(c, 16);
|
|
else
|
|
ret += c;
|
|
if (i > 31)
|
|
{
|
|
ret += "...";
|
|
break;
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
WorkerBase::CommandMap WorkerBase::getCommands() const
|
|
{
|
|
return m_registeredCommands;
|
|
}
|
|
|
|
QByteArray WorkerBase::formatForDebug(const QAbstractSocket *dev)
|
|
{
|
|
const QAbstractSocket *socket = qobject_cast<const QAbstractSocket *>(dev);
|
|
if (socket)
|
|
return socket->peerAddress().toString().toUtf8() + ":" + QByteArray::number(socket->peerPort());
|
|
try
|
|
{
|
|
return typeid(*dev).name();
|
|
}
|
|
catch (std::bad_typeid &e)
|
|
{
|
|
TCPSERVER_ERROR("TcpSocket null? - " << e.what())
|
|
}
|
|
return "<NULL>";
|
|
}
|
|
|
|
int WorkerBase::initCommands(unsigned short port)
|
|
{
|
|
const TcpServer::CommandInterfaceList cbList = TcpServer::registeredCommands(port);
|
|
TcpServer::CommandInterfaceList::ConstIterator it = cbList.begin();
|
|
TcpServer::CommandInterfaceList::ConstIterator end = cbList.end();
|
|
for (; it != end; ++it)
|
|
{
|
|
CommandInterface *(*factory)(unsigned short) = *it;
|
|
CommandInterface *cbi = factory(port);
|
|
cbi->setWorkerBase(this);
|
|
cbi->setSocket(m_connection);
|
|
m_registeredCommands.insert(cbi->command().toLower(), cbi);
|
|
}
|
|
return m_registeredCommands.count();
|
|
}
|
|
|
|
void WorkerBase::cleanupCommands()
|
|
{
|
|
CommandMap::ConstIterator it = m_registeredCommands.constBegin();
|
|
CommandMap::ConstIterator end = m_registeredCommands.constEnd();
|
|
for (; it != end; ++it)
|
|
{
|
|
if (*it)
|
|
{
|
|
(*it)->aboutToDestroy();
|
|
(*it)->deleteLater();
|
|
}
|
|
}
|
|
m_registeredCommands.clear();
|
|
}
|
|
|
|
|
|
|
|
WorkerInstance::WorkerInstance(qintptr sd, unsigned short port, QObject *parent)
|
|
: QObject(parent)
|
|
, WorkerBase(sd, port)
|
|
, m_bFinished(false)
|
|
{}
|
|
|
|
WorkerInstance::~WorkerInstance()
|
|
{
|
|
if (m_connection && m_connection->state() != QAbstractSocket::UnconnectedState)
|
|
{
|
|
m_connection->disconnectFromHost();
|
|
if (m_connection)
|
|
m_connection->waitForDisconnected();
|
|
}
|
|
cleanupCommands();
|
|
|
|
delete m_connection;
|
|
m_connection = nullptr;
|
|
}
|
|
|
|
void WorkerInstance::doWork()
|
|
{
|
|
m_connection = new QTcpSocket(this);
|
|
initCommands(m_port);
|
|
if (!m_connection->setSocketDescriptor(m_socketDescriptor))
|
|
{
|
|
m_connString = formatForDebug(m_connection);
|
|
TCPSERVER_ERROR("Connection to " << m_connString.constData()
|
|
<< " failed - Aborting ("
|
|
<< m_connection->error() << ")")
|
|
return;
|
|
}
|
|
m_connString = formatForDebug(m_connection);
|
|
TCPSERVER_DEBUG("New incoming connection from "
|
|
<< m_connString.constData()
|
|
<< " to " << m_connection->localAddress().toString().toUtf8().constData()
|
|
<< ":" << m_connection->localPort());
|
|
connect(m_connection.data(), &QTcpSocket::readyRead, this, &WorkerInstance::newData);
|
|
connect(m_connection.data(), &QTcpSocket::disconnected, this, &WorkerInstance::connectionDisconnected);
|
|
}
|
|
|
|
void WorkerInstance::newData()
|
|
{
|
|
WorkerBase::newDataInternal();
|
|
}
|
|
|
|
void WorkerInstance::connectionDisconnected()
|
|
{
|
|
TCPSERVER_DEBUG(m_connString.constData() << " closed - exiting instance " << this);
|
|
if (m_connection)
|
|
m_connection->deleteLater();
|
|
m_connection = nullptr;
|
|
m_bFinished = true;
|
|
|
|
QThread *workerThread = QThread::currentThread();
|
|
if (workerThread != qApp->thread())
|
|
{
|
|
workerThread->disconnect(this);
|
|
workerThread->quit();
|
|
}
|
|
emit finished();
|
|
}
|
|
|
|
bool WorkerInstance::isRunning() const
|
|
{
|
|
return m_connection && m_connection->isOpen();
|
|
}
|
|
|
|
bool WorkerInstance::isFinished() const
|
|
{
|
|
return m_bFinished;
|
|
}
|
|
|
|
void WorkerInstance::quit()
|
|
{
|
|
if (m_connection)
|
|
m_connection->close();
|
|
}
|