Files
qt5-legatus-legacy/TetraIF/PrtCnv/PrtCnvSocketThread.hpp
T
2026-08-13 18:33:10 +02:00

262 lines
6.2 KiB
C++

// **************************************************************************
//
// (C) Copyright 2009 Swiss SIMTEC AG
//
// **************************************************************************
//
// $HeadURL$
// $LastChangedBy$
// $LastChangedDate$
// $LastChangedRevision$
//
// Implementation of the Class PrtCnvSocketThread
// **************************************************************************
#pragma once
#include "PrtCnvThread.hpp"
#include <QtNetwork/QAbstractSocket>
#include <memory>
class QTimer;
class QTcpSocket;
namespace N_CoPrtCnv
{
class CoPrtCnvRadioTelegram;
}
namespace PrtCnv
{
class PrtCnvThreadStarter;
class PrtCnvDbThread;
class PrtCnvTelegramLogThread;
class PrtCnvInjectionThread;
/**
* The father-class of radio- and central-thread. It implements common-parts like
* - socket-connection and -disconnection
* - telegram-counter
* - timers
* - telegram-log
* - thread-state
*
* @author wia
* @version 1.0
* @created 26-Jun-2009 07:39:05
*/
class PrtCnvSocketThread : public PrtCnvThread
{
Q_OBJECT
public:
/**
* The different states for the state-event-machine
*
* @author wia
* @version 1.0
* @created 02-Jul-2009 14:16:49
*/
enum E_State
{
e_INITIAL,
e_CONNECTING,
e_REGISTERING,
e_WORKING,
e_DISCONNECTING,
e_REINIT
};
/**
* Constructor with pointer to thread-starter as parameter
*
* @param a_parent
*/
PrtCnvSocketThread(PrtCnvThreadStarter* a_parent, const char *loggerName);
~PrtCnvSocketThread();
/**
* Creates all subcomponents and connects signals to slots
*/
int32_t create(NGL::Settings *settings) override;
/**
* Creates a new socket to avoid getting old stuff from the
* former socket when reconnecting
*/
void createNewSocket();
/**
* Initializes the variables.
*/
int32_t init() override;
/**
* Deallocates subcomponents.
*/
int32_t destroy() override;
void cleanup();
protected:
/**
* this timer is used for controlling connection and disconnection
*/
QTimer *m_ConnectionTimer;
/**
* The TCP-socket
*/
QTcpSocket *m_Socket;
/**
* Return the hostname to connect to
*/
void readHostAdresses();
protected slots:
/**
* Will be called from Qt-Framework when we have connected to TCP-server
*/
virtual void connected();
/**
* Will be called from Qt-Framework when we have have read data over socket
*/
virtual void readyRead();
/**
* this slot is used for connection, disconnection and alive timeouts
*/
virtual void timeoutAliveAndConnection();
/**
* @brief Processes the periodical stuff, like broadcasts, acks
*/
virtual void timeoutTick() {}
/**
* Will be called from Qt-Framework when we have a socket-error
*
* @param a_socketError
*/
virtual void error(QAbstractSocket::SocketError a_socketError);
/**
* Will be called from Qt-Framework when the host was found
*/
virtual void hostFound();
/**
* Will be called from Qt-Framework when socket-state has changed
*
* @param a_socketState
*/
virtual void stateChanged(QAbstractSocket::SocketState a_socketState);
protected:
/**
* used for periodically send an ack to central
*/
QTimer *m_TickTimer;
/**
* Counter of received telegrams (only for sockets)
*/
uint32_t m_ReceiveCounter;
/**
* Acknowledged telegrams counter (only for sockets)
*/
uint32_t m_ReceiveAckCounter;
/**
* The state-event-machine state
*/
PrtCnvSocketThread::E_State m_State;
/**
* Pointer to DbThread
*/
PrtCnvDbThread *m_DbThread;
/**
* The time to wait until connection has happenend [ms]. After this time
* connection will be retryed.
*/
int32_t m_ConnectTimeout;
/**
* The time to wait until disconnection has happenend [ms]. After this time has
* elapsed it will be aborted.
*/
int32_t m_DisconnectTimeout;
/**
* The period to wait until timeout of connection is detected [ms]
*/
int32_t m_AliveTimeout;
/**
* The period of m_TickTimer [ms]
*/
int32_t m_TickTimeout;
/**
* True if the database has to be cleant up (used for fast empty of signal buffers)
*/
QAtomicInt m_DoCleanup;
/**
* for independent logging of telegrams
*/
PrtCnvTelegramLogThread *m_LogThread;
PrtCnvInjectionThread *m_InjectionThread;
/**
* A vector with hostname - port pairs to connect to
*/
QVector<QPair<QString, uint16_t> > m_hostAdresses;
/**
* The current index to connect to in m_hostAdresses
*/
int m_currentHostAdressIndex;
/**
* Will be called from Qt-Framework when we have disconnected to TCP-server
*/
virtual void disconnected();
/**
* used to check whether the other side is still alive
*/
void startAliveTimer();
/**
* used to retry connection
*/
void startConnectionTimer();
/**
* used to abort connection, when normal disconnection is not successful
*/
void startDisconnectionTimer();
/**
* Connect as client to a TCP-server on ip-address configured as HostName and Port
* configured as Port
*/
void connectSocket();
/**
* Disconnects from TCP-server
*/
void disconnectSocket();
/**
* used for periodically ticks (i.e. for acks or sending nachforderungen)
*/
void startTickTimer();
/**
* The counter will be set to a_startCounter if a_maxCounter is reached
* returns true if receive-counter is ok
* returns false if a telegram is repeated or lost
*
* @param a_counter
* @param a_maxCounter
* @param a_startCounter
*/
bool checkAndUpdateReceiveCounter(const uint32_t a_counter, const uint32_t a_maxCounter, const uint32_t a_startCounter);
/**
* calls storeRadioTelegram of DbThread, see there
*
* @param a_telegram
* @param a_readCounter
*/
void storeRadioTelegram(const std::shared_ptr<N_CoPrtCnv::CoPrtCnvRadioTelegram> &a_telegram, const uint32_t a_readCounter);
};
}