Skip to content

Commit

Permalink
both udp and tcp servers now suppported on command line
Browse files Browse the repository at this point in the history
  • Loading branch information
dannagle committed May 27, 2024
1 parent be23394 commit 02fae20
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
25 changes: 8 additions & 17 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,33 +588,24 @@ int main(int argc, char *argv[])
if(server) {
bool bindResult = false;
MainPacketReceiver * receiver = new MainPacketReceiver(nullptr);
QString bindIP = "any";
if(!bindIPstr.isEmpty()) {
bindIP = bindIPstr;

}

if(udp) {
QDEBUG() << "Listening for UDP packets in server mode. ";
QUdpSocket sock;
if(bindIPstr.isEmpty()) {
bindResult = receiver->initUDP("0.0.0.0", bind);
} else {
bindResult = receiver->initUDP(bindIPstr, bind);
}

bindResult = receiver->initUDP(bindIP, bind);
} else {
if(tcp) {
QDEBUG() << "Listening for TCP packets in server mode. ";

}
if(ssl) {
QDEBUG() << "Listening for SSL packets in server mode. ";
}
QSslSocket sock;
if(bindIPstr.isEmpty()) {
bindResult = sock.bind(QHostAddress::Any, bind);
} else {
QHostAddress addy (bindIPstr);
bindResult = sock.bind(addy, bind);
}


QUdpSocket sockUDP;
bindResult = receiver->initSSL(bindIP, bind, ssl);
}


Expand Down
11 changes: 8 additions & 3 deletions src/mainpacketreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ bool MainPacketReceiver::initUDP(QString host, int port)
{
udpSocket = new QUdpSocket(this);
QHostAddress addy(host);
udpSocket->bind(addy, port);
if(host == "any" || addy.isNull()) {
udpSocket->bind(QHostAddress::Any, port);
} else {
udpSocket->bind(addy, port);
}

connect(udpSocket, &QUdpSocket::readyRead,
this, &MainPacketReceiver::readPendingDatagrams);
Expand All @@ -70,10 +74,11 @@ bool MainPacketReceiver::initUDP(QString host, int port)



bool MainPacketReceiver::initSSL(QString host, int port)
bool MainPacketReceiver::initSSL(QString host, int port, bool encrypted)
{
tcpServer = new ThreadedTCPServer(nullptr);
tcpServer->init(port, false, host);
tcpServer->init(port, encrypted, host);
tcpServer->consoleMode = true;

return tcpServer->isListening();
}
Expand Down
2 changes: 1 addition & 1 deletion src/mainpacketreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MainPacketReceiver : public QObject
QUdpSocket * udpSocket;
ThreadedTCPServer * tcpServer;
bool initUDP(QString host, int port);
bool initSSL(QString host, int port);
bool initSSL(QString host, int port, bool encrypted);

static QString datagramOutput(QNetworkDatagram theDatagram, bool quiet = false);
signals:
Expand Down
44 changes: 40 additions & 4 deletions src/threadedtcpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ThreadedTCPServer::ThreadedTCPServer(QObject *parent) :
{

threads.clear();
consoleMode = false;


}
Expand Down Expand Up @@ -53,7 +54,7 @@ void ThreadedTCPServer::incomingConnection(qintptr socketDescriptor)
QDEBUG() << "new tcp connection";

QSettings settings(SETTINGSFILE, QSettings::IniFormat);
bool persistentConnectCheck = settings.value("persistentTCPCheck", false).toBool();
bool persistentConnectCheck = settings.value("persistentTCPCheck", false).toBool() && (!consoleMode);

QDEBUGVAR(persistentConnectCheck);

Expand Down Expand Up @@ -99,9 +100,20 @@ void ThreadedTCPServer::incomingConnection(qintptr socketDescriptor)

connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

QDEBUG() << connect(thread, SIGNAL(packetReceived(Packet)), this, SLOT(packetReceivedECHO(Packet)))
<< connect(thread, SIGNAL(toStatusBar(QString, int, bool)), this, SLOT(toStatusBarECHO(QString, int, bool)))
<< connect(thread, SIGNAL(packetSent(Packet)), this, SLOT(packetSentECHO(Packet)));

if(consoleMode) {
QDEBUG() << "consoleout" << connect(thread, &TCPThread::packetReceived,
this, &ThreadedTCPServer::outputTCPPacket);

QDEBUG() << connect(thread, SIGNAL(packetSent(Packet)), this, SLOT(outputTCPPacket(Packet)));


} else {
QDEBUG() << connect(thread, SIGNAL(packetReceived(Packet)), this, SLOT(packetReceivedECHO(Packet)))
<< connect(thread, SIGNAL(toStatusBar(QString, int, bool)), this, SLOT(toStatusBarECHO(QString, int, bool)))
<< connect(thread, SIGNAL(packetSent(Packet)), this, SLOT(packetSentECHO(Packet)));

}

thread->start();

Expand All @@ -111,6 +123,30 @@ void ThreadedTCPServer::incomingConnection(qintptr socketDescriptor)
}


void ThreadedTCPServer::outputTCPPacket(Packet receivePacket)
{
QTextStream out(stdout);

bool quiet = false;

if (quiet) {
out << "\n" << receivePacket.hexString;
} else {
out << "\nFrom: " << receivePacket.fromIP << ", Port:" << receivePacket.fromPort;
out << "\nResponse Time:" << QDateTime::currentDateTime().toString(DATETIMEFORMAT);
out << "\nResponse HEX:" << receivePacket.hexString;
out << "\nResponse ASCII:" << receivePacket.asciiString();
}

out << Qt::endl;

out.flush();


}



void ThreadedTCPServer::packetReceivedECHO(Packet sendpacket)
{
emit packetReceived(sendpacket);
Expand Down
2 changes: 2 additions & 0 deletions src/threadedtcpserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ThreadedTCPServer : public QTcpServer
bool encrypted;

bool init(quint16 port, bool isEncrypted, QString ipMode);
bool consoleMode;
protected:
void incomingConnection(qintptr socketDescriptor);

Expand All @@ -30,6 +31,7 @@ class ThreadedTCPServer : public QTcpServer
void packetReceivedECHO(Packet sendpacket);
void toStatusBarECHO(const QString & message, int timeout = 0, bool override = false);
void packetSentECHO(Packet sendpacket);
void outputTCPPacket(Packet receivePacket);



Expand Down

0 comments on commit 02fae20

Please sign in to comment.