129 lines
3.2 KiB
C
129 lines
3.2 KiB
C
|
|
#ifndef DATA_BUFFER_H
|
||
|
|
#define DATA_BUFFER_H
|
||
|
|
|
||
|
|
#include <QObject>
|
||
|
|
#include <QQueue>
|
||
|
|
#include <QMutex>
|
||
|
|
#include <QWaitCondition>
|
||
|
|
#include <QTimer>
|
||
|
|
#include <QElapsedTimer>
|
||
|
|
#include <QMap>
|
||
|
|
#include <QSet>
|
||
|
|
#include "../common_structures/TradingStructures.h"
|
||
|
|
|
||
|
|
// Data packet types
|
||
|
|
enum class DataPacketType {
|
||
|
|
RealTimeQuote,
|
||
|
|
OrderBook,
|
||
|
|
KLine,
|
||
|
|
Tick,
|
||
|
|
BrokerQueue,
|
||
|
|
Unknown
|
||
|
|
};
|
||
|
|
|
||
|
|
// Data packet structure
|
||
|
|
struct DataPacket {
|
||
|
|
DataPacketType type;
|
||
|
|
QString stockCode;
|
||
|
|
QDateTime timestamp;
|
||
|
|
QVariant data;
|
||
|
|
int priority; // Priority 0-9, 9 is highest
|
||
|
|
|
||
|
|
DataPacket(DataPacketType t, const QString& code, const QVariant& d, int prio = 5)
|
||
|
|
: type(t), stockCode(code), timestamp(QDateTime::currentDateTime()), data(d), priority(prio) {}
|
||
|
|
};
|
||
|
|
|
||
|
|
// Flow statistics
|
||
|
|
struct FlowStatistics {
|
||
|
|
qint64 totalPackets = 0;
|
||
|
|
qint64 droppedPackets = 0;
|
||
|
|
qint64 processedPackets = 0;
|
||
|
|
double packetsPerSecond = 0.0;
|
||
|
|
double dropRate = 0.0;
|
||
|
|
QDateTime lastUpdate;
|
||
|
|
|
||
|
|
void updateStats(qint64 total, qint64 dropped, qint64 processed) {
|
||
|
|
totalPackets = total;
|
||
|
|
droppedPackets = dropped;
|
||
|
|
processedPackets = processed;
|
||
|
|
dropRate = total > 0 ? static_cast<double>(dropped) / total : 0.0;
|
||
|
|
lastUpdate = QDateTime::currentDateTime();
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
class DataBuffer : public QObject
|
||
|
|
{
|
||
|
|
Q_OBJECT
|
||
|
|
public:
|
||
|
|
static DataBuffer* instance();
|
||
|
|
|
||
|
|
// Buffer configuration
|
||
|
|
void setBufferSize(int maxSize);
|
||
|
|
void setFlowControlEnabled(bool enabled);
|
||
|
|
void setMaxPacketsPerSecond(int maxRate);
|
||
|
|
|
||
|
|
// Data enqueue
|
||
|
|
bool enqueue(const DataPacket& packet);
|
||
|
|
bool enqueueHighPriority(const DataPacket& packet);
|
||
|
|
|
||
|
|
// Data dequeue
|
||
|
|
DataPacket dequeue(int timeoutMs = 1000);
|
||
|
|
QList<DataPacket> dequeueBatch(int maxCount, int timeoutMs = 1000);
|
||
|
|
|
||
|
|
// Status queries
|
||
|
|
bool isEmpty() const;
|
||
|
|
int size() const;
|
||
|
|
int getQueueSize(DataPacketType type) const;
|
||
|
|
FlowStatistics getStatistics() const;
|
||
|
|
|
||
|
|
// Flow control
|
||
|
|
void pause();
|
||
|
|
void resume();
|
||
|
|
bool isPaused() const;
|
||
|
|
|
||
|
|
// Cleanup
|
||
|
|
void clear();
|
||
|
|
void clearByType(DataPacketType type);
|
||
|
|
void clearByStock(const QString& stockCode);
|
||
|
|
|
||
|
|
signals:
|
||
|
|
void bufferOverflow(const QString& stockCode, DataPacketType type);
|
||
|
|
void flowControlActivated(bool activated);
|
||
|
|
void statisticsUpdated(const FlowStatistics& stats);
|
||
|
|
void packetDropped(const DataPacket& packet, const QString& reason);
|
||
|
|
|
||
|
|
private:
|
||
|
|
explicit DataBuffer(QObject *parent = nullptr);
|
||
|
|
~DataBuffer();
|
||
|
|
|
||
|
|
void updateStatistics();
|
||
|
|
bool shouldDropPacket(const DataPacket& packet) const;
|
||
|
|
void cleanupExpiredPackets();
|
||
|
|
|
||
|
|
// Buffers
|
||
|
|
QQueue<DataPacket> m_buffer;
|
||
|
|
QQueue<DataPacket> m_highPriorityBuffer;
|
||
|
|
|
||
|
|
// Synchronization control
|
||
|
|
mutable QMutex m_mutex;
|
||
|
|
QWaitCondition m_notEmpty;
|
||
|
|
QWaitCondition m_notFull;
|
||
|
|
|
||
|
|
// Configuration
|
||
|
|
int m_maxBufferSize = 10000;
|
||
|
|
int m_maxPacketsPerSecond = 1000;
|
||
|
|
bool m_flowControlEnabled = true;
|
||
|
|
bool m_paused = false;
|
||
|
|
|
||
|
|
// Statistics
|
||
|
|
FlowStatistics m_stats;
|
||
|
|
QElapsedTimer m_rateTimer;
|
||
|
|
qint64 m_currentSecondPackets = 0;
|
||
|
|
|
||
|
|
// Cleanup timers
|
||
|
|
QTimer* m_cleanupTimer;
|
||
|
|
QTimer* m_statsUpdateTimer;
|
||
|
|
};
|
||
|
|
|
||
|
|
#endif // DATA_BUFFER_H
|