Ali Hatami Tajik
3 weeks ago
7 changed files with 164 additions and 24 deletions
@ -0,0 +1,43 @@ |
|||
#include <QObject> |
|||
#include <QTcpServer> |
|||
#include <QTcpSocket> |
|||
|
|||
#include "parser/IParser.h" |
|||
|
|||
class TcpServerWorker : public QObject |
|||
{ |
|||
Q_OBJECT |
|||
|
|||
public: |
|||
TcpServerWorker(Parser_ptr parser, QObject* parent = nullptr) : QObject(parent) |
|||
{ |
|||
_parser = std::move(parser); |
|||
//Initialize the server
|
|||
server = new QTcpServer(this); |
|||
connect(server, &QTcpServer::newConnection, this, &TcpServerWorker::handleNewConnection); |
|||
} |
|||
|
|||
bool startServer(quint16 port) { |
|||
return server->listen(QHostAddress::Any, port); |
|||
} |
|||
|
|||
signals: |
|||
void log(Log_ptr log); |
|||
|
|||
//uncrustify off
|
|||
private slots: |
|||
//uncrustify on
|
|||
void handleNewConnection() { |
|||
QTcpSocket* clientSocket = server->nextPendingConnection(); |
|||
connect(clientSocket, &QTcpSocket::readyRead, this, [this, clientSocket]() { |
|||
while(clientSocket->canReadLine()) |
|||
{ |
|||
emit log(_parser->parse(clientSocket->readLine())); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
private: |
|||
QTcpServer* server; |
|||
Parser_ptr _parser; |
|||
}; |
Loading…
Reference in new issue