Skip to content

Commit

Permalink
CLI udp server now supports macro replies
Browse files Browse the repository at this point in the history
  • Loading branch information
dannagle committed Jun 5, 2024
1 parent e4da453 commit 825a520
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ int main(int argc, char *argv[])
QCommandLineOption serverOption(QStringList() << "l" << "listen", "Listen instead of send. Use bind options to specify port/IP. Otherwise, dynamic/All.");
parser.addOption(serverOption);

// Command line server response
QCommandLineOption responseOption(QStringList() << "r" << "response", "Server mode response data in mixed-ascii. Macro supported.", "ascii");
parser.addOption(responseOption);


// An option with a value
QCommandLineOption waitOption(QStringList() << "w" << "wait",
Expand Down Expand Up @@ -342,6 +346,8 @@ int main(int argc, char *argv[])
bool wol = parser.isSet(wolOption);

bool server = parser.isSet(serverOption);
QString response = parser.value(responseOption);
QDEBUGVAR(response);
bool okbps = false;
bool okrate = false;
bool maxrate = parser.isSet(maxOption);
Expand Down Expand Up @@ -588,6 +594,16 @@ int main(int argc, char *argv[])
if(server) {
bool bindResult = false;
MainPacketReceiver * receiver = new MainPacketReceiver(nullptr);
if(!response.isEmpty()) {
Packet replyPacket;
QDEBUGVAR(response);
replyPacket.hexString = Packet::ASCIITohex(response);
receiver->responsePacket(replyPacket);
if(!replyPacket.hexString.isEmpty()) {
OUTIF() << "Loading response packet.";

}
}
QString bindIP = "any";
if(!bindIPstr.isEmpty()) {
bindIP = bindIPstr;
Expand Down Expand Up @@ -753,6 +769,9 @@ int main(int argc, char *argv[])
QDEBUGVAR(usdelay);
QDEBUGVAR(translateMacroSend);
QDEBUGVAR(maxrate);
QDEBUGVAR(server);
QDEBUGVAR(response);



//NOW LETS DO THIS!
Expand Down
37 changes: 34 additions & 3 deletions src/mainpacketreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ MainPacketReceiver::MainPacketReceiver(QObject *parent) :QObject(parent) {
finished = false;
packetNetwork = new PacketNetwork(parent);
packetNetwork->consoleMode = true;
packetReply.clear();

QDEBUG();


if (!connect(this->packetNetwork, &PacketNetwork::packetReceived, this, &MainPacketReceiver::toTrafficLog)) {
Expand All @@ -18,7 +18,6 @@ MainPacketReceiver::MainPacketReceiver(QObject *parent) :QObject(parent) {
if (!connect(this->packetNetwork, &PacketNetwork::packetSent, this, &MainPacketReceiver::toTrafficLog)) {
QDEBUG() << "packetNetwork packetReceived false";
}
QDEBUG();


if (!connect(this, &MainPacketReceiver::sendPacket, packetNetwork, &PacketNetwork::packetToSend)) {
Expand All @@ -42,15 +41,41 @@ void MainPacketReceiver::httpFinished()
}



void MainPacketReceiver::readPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {

QString output = MainPacketReceiver::datagramOutput(udpSocket->receiveDatagram(10000000), false);
QNetworkDatagram datagram = udpSocket->receiveDatagram(10000000);


QTextStream out(stdout);
QString output = MainPacketReceiver::datagramOutput(datagram, false);
out << output << Qt::endl;
out.flush();
output.clear();

if(!packetReply.hexString.isEmpty()) {

Packet sendit = packetReply;
sendit.tcpOrUdp = "UDP";
sendit.fromIP = "You (Response)";
sendit.toIP = datagram.senderAddress().toString();
sendit.port = datagram.senderPort();
QString data = Packet::macroSwap(packetReply.asciiString());
sendit.hexString = Packet::ASCIITohex(data);

emit sendPacket(sendit);

out << "\nFrom: " << sendit.fromIP << ", Port:" << sendit.port;
out << "\nResponse Time:" << QDateTime::currentDateTime().toString(DATETIMEFORMAT);
out << "\nResponse HEX:" << sendit.hexString;
out << "\nResponse ASCII:" << sendit.asciiString();
out << Qt::endl;

}

out.flush();

}

Expand Down Expand Up @@ -108,6 +133,12 @@ QString MainPacketReceiver::datagramOutput(QNetworkDatagram theDatagram, bool qu
return output;
}

void MainPacketReceiver::responsePacket(Packet packetToSend)
{
packetReply = packetToSend;

}


void MainPacketReceiver::send(Packet packetToSend) {
QDEBUG();
Expand Down
2 changes: 2 additions & 0 deletions src/mainpacketreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MainPacketReceiver : public QObject
bool initSSL(QString host, int port, bool encrypted);

static QString datagramOutput(QNetworkDatagram theDatagram, bool quiet = false);
void responsePacket(Packet packetToSend);
signals:
void sendPacket(Packet packetToSend);
//void receivedPacket(Packet packetReceived);
Expand All @@ -30,5 +31,6 @@ class MainPacketReceiver : public QObject
private:
PacketNetwork * packetNetwork;
void httpFinished();
Packet packetReply;
};

0 comments on commit 825a520

Please sign in to comment.