js8call/JS8_Network/TCPClient.cpp
Chris-AC9KH 356bdc0f50 Code cleanup - move loose source and header files in source tree to group folders
Rename all header files with .h extension
Fix function declaration in HamlibTransceiver.h that overrode member function but was not marked override
Remove unused variable in Modulator.cpp
Remove unused files from source tree
Remove duplicate LazyFillComboBox files
Reformat codebase to LLVM C++ standard
2026-01-02 16:46:38 -06:00

69 lines
1.5 KiB
C++

#include "TCPClient.h"
#include <QDebug>
#include <QTcpSocket>
#include "JS8_Include/pimpl_impl.h"
#include "moc_TCPClient.cpp"
class TCPClient::impl : public QTcpSocket {
Q_OBJECT
public:
using port_type = quint16;
impl(TCPClient *self) : self_{self} {}
~impl() {}
bool isConnected(QString host, port_type port) {
if (host_ != host || port_ != port) {
disconnectFromHost();
return false;
}
return state() == QTcpSocket::ConnectedState;
}
void connectToHostPort(QString host, port_type port) {
host_ = host;
port_ = port;
QTcpSocket::connectToHost(host_, port_);
}
qint64 send(QByteArray const &message, bool crlf) {
return write(message + (crlf ? "\r\f" : ""));
}
TCPClient *self_;
QString host_;
port_type port_;
};
#include "TCPClient.moc"
TCPClient::TCPClient(QObject *parent) : QObject(parent), m_{this} {}
bool TCPClient::ensureConnected(QString host, port_type port, int msecs) {
if (!m_->isConnected(host, port)) {
m_->connectToHostPort(host, port);
}
return m_->waitForConnected(msecs);
}
bool TCPClient::sendNetworkMessage(QString host, port_type port,
QByteArray const &message, bool crlf,
int msecs) {
if (!ensureConnected(host, port, msecs)) {
return false;
}
qint64 n = m_->send(message, crlf);
if (n <= 0) {
return false;
}
return m_->flush();
}