kopia lustrzana https://gitlab.com/eliggett/wfview
Slightly better tcp server implementation (still needs UI adding)
rodzic
bfd9ddea52
commit
c5cf0fdf57
|
@ -8,3 +8,4 @@ Q_LOGGING_CATEGORY(logAudio, "audio")
|
|||
Q_LOGGING_CATEGORY(logUdp, "udp")
|
||||
Q_LOGGING_CATEGORY(logUdpServer, "udp.server")
|
||||
Q_LOGGING_CATEGORY(logRigCtlD, "rigctld")
|
||||
Q_LOGGING_CATEGORY(logTcpServer, "tcpserver")
|
||||
|
|
|
@ -11,6 +11,7 @@ Q_DECLARE_LOGGING_CATEGORY(logAudio)
|
|||
Q_DECLARE_LOGGING_CATEGORY(logUdp)
|
||||
Q_DECLARE_LOGGING_CATEGORY(logUdpServer)
|
||||
Q_DECLARE_LOGGING_CATEGORY(logRigCtlD)
|
||||
Q_DECLARE_LOGGING_CATEGORY(logTcpServer)
|
||||
|
||||
|
||||
#if defined(Q_OS_WIN) && !defined(__PRETTY_FUNCTION__)
|
||||
|
|
|
@ -129,6 +129,7 @@ void rigCommander::commSetup(unsigned char rigCivAddr, udpPreferences prefs, aud
|
|||
ptty = new pttyHandler(vsp);
|
||||
|
||||
tcp = new tcpServer(this);
|
||||
tcp->startServer(5010);
|
||||
|
||||
// Data from UDP to the program
|
||||
connect(udp, SIGNAL(haveDataFromPort(QByteArray)), this, SLOT(handleNewData(QByteArray)));
|
||||
|
@ -137,7 +138,7 @@ void rigCommander::commSetup(unsigned char rigCivAddr, udpPreferences prefs, aud
|
|||
connect(udp, SIGNAL(haveDataFromPort(QByteArray)), ptty, SLOT(receiveDataFromRigToPtty(QByteArray)));
|
||||
|
||||
// data from the rig to tcp:
|
||||
connect(udp, SIGNAL(haveDataFromPort(QByteArray)), tcp, SLOT(dataToPort(QByteArray)));
|
||||
connect(udp, SIGNAL(haveDataFromPort(QByteArray)), tcp, SLOT(sendData(QByteArray)));
|
||||
|
||||
|
||||
// Audio from UDP
|
||||
|
@ -150,7 +151,7 @@ void rigCommander::commSetup(unsigned char rigCivAddr, udpPreferences prefs, aud
|
|||
connect(ptty, SIGNAL(haveDataFromPort(QByteArray)), udp, SLOT(receiveDataFromUserToRig(QByteArray)));
|
||||
|
||||
// data from the tcp port to the Rig:
|
||||
connect(tcp, SIGNAL(haveDataFromPort(QByteArray)), udp, SLOT(receiveDataFromUserToRig(QByteArray)));
|
||||
connect(tcp, SIGNAL(haveData(QByteArray)), udp, SLOT(receiveDataFromUserToRig(QByteArray)));
|
||||
|
||||
connect(this, SIGNAL(haveChangeLatency(quint16)), udp, SLOT(changeLatency(quint16)));
|
||||
connect(this, SIGNAL(haveSetVolume(unsigned char)), udp, SLOT(setVolume(unsigned char)));
|
||||
|
|
100
tcpserver.cpp
100
tcpserver.cpp
|
@ -2,55 +2,97 @@
|
|||
|
||||
#include "logcategories.h"
|
||||
|
||||
tcpServer::tcpServer(QObject* parent) : QObject(parent)
|
||||
tcpServer::tcpServer(QObject* parent) : QTcpServer(parent)
|
||||
{
|
||||
server = new QTcpServer(this);
|
||||
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
|
||||
if (!server->listen(QHostAddress::Any, 5010))
|
||||
{
|
||||
qDebug() << "TCP Server could not start";
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "TCP Server started!";
|
||||
}
|
||||
}
|
||||
|
||||
tcpServer::~tcpServer()
|
||||
{
|
||||
if (socket != Q_NULLPTR)
|
||||
{
|
||||
socket->close();
|
||||
delete socket;
|
||||
qInfo(logTcpServer()) << "closing tcpServer";
|
||||
}
|
||||
|
||||
int tcpServer::startServer(qint16 port) {
|
||||
|
||||
if (!this->listen(QHostAddress::Any, port)) {
|
||||
qInfo(logTcpServer()) << "could not start on port " << port;
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
qInfo(logTcpServer()) << "started on port " << port;
|
||||
}
|
||||
server->close();
|
||||
delete server;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void tcpServer::newConnection()
|
||||
void tcpServer::incomingConnection(qintptr socket) {
|
||||
tcpServerClient* client = new tcpServerClient(socket, this);
|
||||
connect(this, SIGNAL(onStopped()), client, SLOT(closeSocket()));
|
||||
}
|
||||
|
||||
void tcpServer::stopServer()
|
||||
{
|
||||
qDebug(logSystem()) << QString("Incoming Connection");
|
||||
socket = server->nextPendingConnection();
|
||||
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
|
||||
qInfo(logTcpServer()) << "stopping server";
|
||||
emit onStopped();
|
||||
}
|
||||
|
||||
void tcpServer::readyRead() {
|
||||
|
||||
void tcpServer::receiveDataFromClient(QByteArray data)
|
||||
{
|
||||
emit haveData(data);
|
||||
}
|
||||
|
||||
void tcpServer::sendData(QByteArray data) {
|
||||
|
||||
emit sendDataToClient(data);
|
||||
|
||||
}
|
||||
|
||||
tcpServerClient::tcpServerClient(int socketId, tcpServer* parent) : QObject(parent)
|
||||
{
|
||||
sessionId = socketId;
|
||||
socket = new QTcpSocket(this);
|
||||
this->parent = parent;
|
||||
if (!socket->setSocketDescriptor(sessionId))
|
||||
{
|
||||
qInfo(logTcpServer()) << " error binding socket: " << sessionId;
|
||||
return;
|
||||
}
|
||||
connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()), Qt::DirectConnection);
|
||||
connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()), Qt::DirectConnection);
|
||||
connect(parent, SIGNAL(sendDataToClient(QByteArray)), this, SLOT(receiveDataToClient(QByteArray)), Qt::DirectConnection);
|
||||
connect(this, SIGNAL(sendDataFromClient(QByteArray)), parent, SLOT(receiveDataFromClient(QByteArray)), Qt::DirectConnection);
|
||||
qInfo(logTcpServer()) << " session connected: " << sessionId;
|
||||
|
||||
}
|
||||
|
||||
void tcpServerClient::socketReadyRead() {
|
||||
QByteArray data;
|
||||
if (socket->bytesAvailable()) {
|
||||
data=socket->readAll();
|
||||
emit haveDataFromPort(data);
|
||||
emit sendDataFromClient(data);
|
||||
}
|
||||
//qDebug(logSystem()) << QString("Data IN!");
|
||||
|
||||
}
|
||||
|
||||
void tcpServerClient::socketDisconnected() {
|
||||
qInfo(logTcpServer()) << sessionId << "disconnected";
|
||||
socket->deleteLater();
|
||||
this->deleteLater();
|
||||
}
|
||||
|
||||
void tcpServer::dataToPort(QByteArray data) {
|
||||
//qDebug(logSystem()) << QString("TCP Send");
|
||||
void tcpServerClient::closeSocket()
|
||||
{
|
||||
socket->close();
|
||||
}
|
||||
|
||||
if (socket != Q_NULLPTR) {
|
||||
void tcpServerClient::receiveDataToClient(QByteArray data) {
|
||||
|
||||
if (socket != Q_NULLPTR && socket->isValid() && socket->isOpen())
|
||||
{
|
||||
socket->write(data);
|
||||
socket->flush();
|
||||
}
|
||||
else
|
||||
{
|
||||
qInfo(logTcpServer()) << "socket not open!";
|
||||
}
|
||||
}
|
54
tcpserver.h
54
tcpserver.h
|
@ -1,26 +1,62 @@
|
|||
#ifndef TCPSERVER_H
|
||||
#define TCPSERVER_H
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QObject>
|
||||
#include <QDebug>
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
#include <qtcpserver.h>
|
||||
class tcpServer :
|
||||
public QObject
|
||||
#include <QSet>
|
||||
#include <QDataStream>
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <typeindex>
|
||||
|
||||
class tcpServer : public QTcpServer
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit tcpServer(QObject* parent = 0);
|
||||
explicit tcpServer(QObject* parent = Q_NULLPTR);
|
||||
~tcpServer();
|
||||
int startServer(qint16 port);
|
||||
void stopServer();
|
||||
|
||||
public slots:
|
||||
void dataToPort(QByteArray data);
|
||||
void readyRead();
|
||||
void newConnection();
|
||||
virtual void incomingConnection(qintptr socketDescriptor);
|
||||
void receiveDataFromClient(QByteArray data);
|
||||
void sendData(QByteArray data);
|
||||
signals:
|
||||
void haveDataFromPort(QByteArray data); // emit this when we have data, connect to rigcommander
|
||||
void onStarted();
|
||||
void onStopped();
|
||||
void haveData(QByteArray data); // emit this when we have data from tcp client, connect to rigcommander
|
||||
void sendDataToClient(QByteArray data);
|
||||
|
||||
private:
|
||||
QTcpServer* server;
|
||||
QTcpSocket* socket = Q_NULLPTR;
|
||||
};
|
||||
|
||||
class tcpServerClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit tcpServerClient(int socket, tcpServer* parent = Q_NULLPTR);
|
||||
public slots:
|
||||
void socketReadyRead();
|
||||
void socketDisconnected();
|
||||
void closeSocket();
|
||||
void receiveDataToClient(QByteArray);
|
||||
|
||||
signals:
|
||||
void sendDataFromClient(QByteArray data);
|
||||
protected:
|
||||
int sessionId;
|
||||
QTcpSocket* socket = Q_NULLPTR;
|
||||
|
||||
private:
|
||||
tcpServer* parent;
|
||||
};
|
||||
|
||||
#endif
|
Ładowanie…
Reference in New Issue