2024-09-22 05:44:30 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2024 Jon Beniston, M7RCE <jon@beniston.com> //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation as version 3 of the License, or //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// This program is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef INCLUDE_SOCKET_H_
|
|
|
|
#define INCLUDE_SOCKET_H_
|
|
|
|
|
|
|
|
#include <QTcpSocket>
|
|
|
|
#include <QWebSocket>
|
|
|
|
|
2024-10-08 12:20:18 -04:00
|
|
|
#include "export.h"
|
|
|
|
|
2024-09-22 05:44:30 -04:00
|
|
|
// Class to allow easy use of either QTCPSocket or QWebSocket
|
2024-10-08 12:20:18 -04:00
|
|
|
class SDRBASE_API Socket : public QObject {
|
2024-09-22 05:44:30 -04:00
|
|
|
Q_OBJECT
|
|
|
|
protected:
|
|
|
|
Socket(QObject *socket, QObject *parent=nullptr);
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~Socket();
|
|
|
|
virtual qint64 write(const char *data, qint64 length) = 0;
|
|
|
|
virtual void flush() = 0;
|
|
|
|
virtual qint64 read(char *data, qint64 length) = 0;
|
|
|
|
virtual qint64 bytesAvailable() = 0;
|
2024-10-08 12:20:18 -04:00
|
|
|
virtual QByteArray readAll() = 0;
|
2024-09-22 05:44:30 -04:00
|
|
|
virtual void close() = 0;
|
|
|
|
virtual QHostAddress peerAddress() = 0;
|
|
|
|
virtual quint16 peerPort() = 0;
|
2024-10-08 12:20:18 -04:00
|
|
|
virtual bool isConnected() = 0;
|
2024-09-22 05:44:30 -04:00
|
|
|
|
|
|
|
QObject *socket() { return m_socket; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
QObject *m_socket;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2024-10-08 12:20:18 -04:00
|
|
|
class SDRBASE_API TCPSocket : public Socket {
|
2024-09-22 05:44:30 -04:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2024-10-08 17:19:29 -04:00
|
|
|
explicit TCPSocket(QTcpSocket *socket) ;
|
2024-09-22 05:44:30 -04:00
|
|
|
qint64 write(const char *data, qint64 length) override;
|
|
|
|
void flush() override;
|
|
|
|
qint64 read(char *data, qint64 length) override;
|
|
|
|
qint64 bytesAvailable() override;
|
2024-10-08 12:20:18 -04:00
|
|
|
QByteArray readAll() override;
|
2024-09-22 05:44:30 -04:00
|
|
|
void close() override;
|
|
|
|
QHostAddress peerAddress() override;
|
|
|
|
quint16 peerPort() override;
|
2024-10-08 12:20:18 -04:00
|
|
|
bool isConnected() override;
|
2024-09-22 05:44:30 -04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2024-10-08 12:20:18 -04:00
|
|
|
class SDRBASE_API WebSocket : public Socket {
|
2024-09-22 05:44:30 -04:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2024-10-08 17:19:29 -04:00
|
|
|
explicit WebSocket(QWebSocket *socket);
|
2024-09-22 05:44:30 -04:00
|
|
|
qint64 write(const char *data, qint64 length) override;
|
|
|
|
void flush() override;
|
|
|
|
qint64 read(char *data, qint64 length) override;
|
|
|
|
qint64 bytesAvailable() override;
|
2024-10-08 12:20:18 -04:00
|
|
|
QByteArray readAll() override;
|
2024-09-22 05:44:30 -04:00
|
|
|
void close() override;
|
|
|
|
QHostAddress peerAddress() override;
|
|
|
|
quint16 peerPort() override;
|
2024-10-08 12:20:18 -04:00
|
|
|
bool isConnected() override;
|
2024-09-22 05:44:30 -04:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
|
|
|
void binaryFrameReceived(const QByteArray &frame, bool isLastFrame);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
QByteArray m_rxBuffer;
|
|
|
|
QByteArray m_txBuffer;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // INCLUDE_SOCKET_H_
|