File indexing completed on 2024-05-12 05:51:07
0001 /* 0002 SPDX-FileCopyrightText: 2022 Héctor Mesa Jiménez <wmj.py@gmx.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 #include "socketbus.h" 0007 #include <QDebug> 0008 0009 #include "settings.h" 0010 0011 namespace dap 0012 { 0013 SocketBus::SocketBus(QObject *parent) 0014 : Bus(parent) 0015 { 0016 connect(&socket, &QTcpSocket::readyRead, this, &Bus::readyRead); 0017 connect(&socket, &QTcpSocket::stateChanged, this, &SocketBus::onStateChanged); 0018 } 0019 0020 QByteArray SocketBus::read() 0021 { 0022 return socket.readAll(); 0023 } 0024 0025 quint16 SocketBus::write(const QByteArray &data) 0026 { 0027 return socket.write(data); 0028 } 0029 0030 bool SocketBus::start(const settings::BusSettings &configuration) 0031 { 0032 if (!configuration.hasConnection()) 0033 return false; 0034 0035 socket.connectToHost(configuration.connection->host, configuration.connection->port); 0036 0037 return true; 0038 } 0039 0040 void SocketBus::close() 0041 { 0042 socket.close(); 0043 setState(State::Closed); 0044 } 0045 0046 void SocketBus::onStateChanged(QAbstractSocket::SocketState socketState) 0047 { 0048 // any 0049 // connectedstate 0050 // !connectestate 0051 0052 if (socketState == QAbstractSocket::SocketState::ConnectedState) { 0053 setState(State::Running); 0054 } else { 0055 const bool errorOccurred = socket.error() != QAbstractSocket::SocketError::UnknownSocketError; 0056 if (errorOccurred) { 0057 qWarning() << "Socket Error: " << socket.errorString(); 0058 Q_EMIT error(socket.errorString()); 0059 } 0060 if (errorOccurred || (state() == State::Running)) { 0061 setState(State::Closed); 0062 } 0063 } 0064 } 0065 0066 } 0067 0068 #include "moc_socketbus.cpp"