first re-commit.

This commit is contained in:
2025-08-05 22:37:51 +02:00
commit 5295a82aa3
109 changed files with 9928 additions and 0 deletions

173
libMiniCash/mcreceiver.cpp Normal file
View File

@@ -0,0 +1,173 @@
/***************************************************************************
miniCashConnect
Copyright © 2022 christoph holzheuer
c.holzheuer@sourceworx.org
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; either version 3 of the License, or
(at your option) any later version.
***************************************************************************/
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QMetaType>
#include <QSet>
#include <QTcpSocket>
#include <QDebug>
#include <libMiniCash.h>
#include <mcreceiver.h>
/**
* @brief Destructor, alles schließen
*/
MCReceiver::~MCReceiver()
{
onDiscardConnection();
deleteLater();
}
/**
* @brief Empfangsbereit machen: hier passiert weiter nichts,
* erst wir nur der parameter 'port' neu gesetzt, die
* entsprechende Aktion wird über ein SIGNAL getriggert.
*/
void MCReceiver::setupConnection( int port )
{
qDebug() << "MCReceiver::setupConnection: " << port;
_port = port;
}
/**
* @brief Slot,
*/
void MCReceiver::onCreateConnection()
{
if( isListening() )
onDiscardConnection();
//qDebug( "MCReceiver::onCreateConnection() 1: listen...." );
if( listen( QHostAddress::Any, _port ) )
{
//qDebug( "MCReceiver::onCreateConnection() 2: Server is listening...");
connect( this, &QTcpServer::newConnection, this, &MCReceiver::onNewConnection );
//qDebug( "MCReceiver::onCreateConnection() 3: Server is listening...");
emit connectionChanged( miniCash::IsServer );
return;
}
emit connectionChanged( miniCash::Error );
//QMessageBox::critical(this,"QTCPServer",QString("Unable to start the server: %1.").arg(errorString()));
//exit(EXIT_FAILURE);
}
void MCReceiver::onDiscardConnection()
{
//qDebug( "MCReceiver::onDiscardConnection()");
foreach( QTcpSocket* socket, _connections )
{
socket->close();
socket->deleteLater();
}
close();
emit connectionChanged( miniCash::UnConnected );
}
/**
* @brief Nimmt neue Connections an und schreibt sie
* in die Connectionliste.
*/
void MCReceiver::onNewConnection()
{
while( hasPendingConnections() )
appendToSocketList( nextPendingConnection() );
emit connectionChanged( miniCash::ServerReady );
}
void MCReceiver::appendToSocketList( QTcpSocket* socket )
{
_connections.insert(socket);
connect( socket, &QTcpSocket::readyRead, this, &MCReceiver::onReadReady );
connect( socket, &QTcpSocket::disconnected, this, &MCReceiver::onSocketDisconnected );
//qDebug() << QString("INFO :: Client with sockd:%1 has just entered the room").arg(socket->socketDescriptor() );
}
void MCReceiver::onReadReady()
{
QTcpSocket* socket = reinterpret_cast<QTcpSocket*>( sender() );
QByteArray buffer;
QDataStream socketStream( socket );
socketStream.setVersion( QDataStream::Qt_5_15 );
socketStream.startTransaction();
socketStream >> buffer;
if( !socketStream.commitTransaction() )
return;
/*
{
QString message = QString("%1 :: Waiting for more data to come..").arg(socket->socketDescriptor());
emit newData(message);
return;
}
*/
// warum??
//QString message = QString::fromStdString( buffer.toStdString() );
emit newTransaction( QString( buffer ) );
}
void MCReceiver::onSocketDisconnected()
{
QTcpSocket* socket = reinterpret_cast<QTcpSocket*>(sender());
QSet<QTcpSocket*>::iterator it = _connections.find(socket);
if (it != _connections.end())
{
//displayMessage( QString("INFO :: A client has just left the room").arg(socket->socketDescriptor()) );
_connections.remove( *it );
}
socket->deleteLater();
}