/
v-str
/
cerera_android
Обзор
Документация
Войти
/
v-str
/
cerera_android
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
proj/src/AppCore.cpp
77 строк
2 KB
v-str
Graceful TCP disconnect + TCP_NODELAY + чертежи CNC в freecad/
02 июл 2026, 19:34
Верифицирован
02 июл 2026, 19:34
a0f1e56
Код
Авторство
О чём код?
#include "AppCore.hpp" #include <QDebug> #include <QQmlApplicationEngine> #include <QTcpSocket> #include "common.hpp" AppCore::AppCore(QQmlApplicationEngine& engine, QObject* parent) : QObject(parent) { setupSpaceMill(engine); } AppCore::~AppCore() { serverDisconnect(); } void AppCore::serverConnect(const QString& ip, quint16 port) { serverDisconnect(); if (m_socket) { delete m_socket; m_socket = nullptr; } m_socket = new QTcpSocket(this); m_socket->setSocketOption(QAbstractSocket::LowDelayOption, 1); connect(m_socket, &QTcpSocket::connected, this, &AppCore::onConnected); connect(m_socket, &QTcpSocket::disconnected, this, &AppCore::onDisconnected); connect(m_socket, &QAbstractSocket::errorOccurred, this, &AppCore::onErrorOccurred); connect(m_socket, &QTcpSocket::readyRead, this, &AppCore::onReadyRead); qDebug() << "trying to connect" << ip << ":" << port; m_socket->connectToHost(ip, port); } void AppCore::serverDisconnect() { if (!m_socket) return; m_socket->disconnectFromHost(); if (m_socket->state() != QAbstractSocket::UnconnectedState) m_socket->waitForDisconnected(1000); } void AppCore::sendData(const QString& text) { if (m_socket && m_socket->state() == QAbstractSocket::ConnectedState) { qDebug() << "sending:" << text; m_socket->write(text.toUtf8() + "\n"); m_socket->flush(); } } void AppCore::onConnected() { qDebug() << "connected to server"; m_isConnected = true; emit isConnectedChanged(); } void AppCore::onDisconnected() { qDebug() << "disconnected from server"; m_isConnected = false; emit isConnectedChanged(); } void AppCore::onErrorOccurred(QAbstractSocket::SocketError error) { Q_UNUSED(error) QString msg = m_socket ? m_socket->errorString() : "Unknown error"; qDebug() << "socket error:" << msg; emit connectionError(msg); } void AppCore::onReadyRead() { while (m_socket && m_socket->bytesAvailable() > 0) { QByteArray data = m_socket->readAll(); qDebug() << "received:" << data; emit dataReceived(QString::fromUtf8(data)); } }