Update 更新文档
This commit is contained in:
92
cleaned_source_code/Sqbase/qdatabuffer.h
Normal file
92
cleaned_source_code/Sqbase/qdatabuffer.h
Normal file
@@ -0,0 +1,92 @@
|
||||
#ifndef QDATABUFFER_H
|
||||
#define QDATABUFFER_H
|
||||
#include <QObject>
|
||||
#include <QQueue>
|
||||
#include <QMutex>
|
||||
#include <QWaitCondition>
|
||||
#include <QTimer>
|
||||
#include <QElapsedTimer>
|
||||
#include <QMap>
|
||||
#include <QSet>
|
||||
#include "BZStruct.h"
|
||||
enum class DataPacketType {
|
||||
RealTimeQuote,
|
||||
OrderBook,
|
||||
KLine,
|
||||
Tick,
|
||||
BrokerQueue,
|
||||
Unknown
|
||||
};
|
||||
struct DataPacket {
|
||||
DataPacketType type;
|
||||
QString stockCode;
|
||||
QDateTime timestamp;
|
||||
QVariant data;
|
||||
int priority;
|
||||
DataPacket(DataPacketType t, const QString& code, const QVariant& d, int prio = 5)
|
||||
: type(t), stockCode(code), timestamp(QDateTime::currentDateTime()), data(d), priority(prio) {}
|
||||
};
|
||||
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 QDataBuffer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static QDataBuffer* instance();
|
||||
void setBufferSize(int maxSize);
|
||||
void setFlowControlEnabled(bool enabled);
|
||||
void setMaxPacketsPerSecond(int maxRate);
|
||||
bool enqueue(const DataPacket& packet);
|
||||
bool enqueueHighPriority(const DataPacket& packet);
|
||||
DataPacket dequeue(int timeoutMs = 1000);
|
||||
QList<DataPacket> dequeueBatch(int maxCount, int timeoutMs = 1000);
|
||||
bool isEmpty() const;
|
||||
int size() const;
|
||||
int getQueueSize(DataPacketType type) const;
|
||||
FlowStatistics getStatistics() const;
|
||||
void pause();
|
||||
void resume();
|
||||
bool isPaused() const;
|
||||
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 QDataBuffer(QObject *parent = nullptr);
|
||||
~QDataBuffer();
|
||||
void updateStatistics();
|
||||
bool shouldDropPacket(const DataPacket& packet) const;
|
||||
void cleanupExpiredPackets();
|
||||
QQueue<DataPacket> m_buffer;
|
||||
QQueue<DataPacket> m_highPriorityBuffer;
|
||||
mutable QMutex m_mutex;
|
||||
QWaitCondition m_notEmpty;
|
||||
QWaitCondition m_notFull;
|
||||
int m_maxBufferSize = 10000;
|
||||
int m_maxPacketsPerSecond = 1000;
|
||||
bool m_flowControlEnabled = true;
|
||||
bool m_paused = false;
|
||||
FlowStatistics m_stats;
|
||||
QElapsedTimer m_rateTimer;
|
||||
qint64 m_currentSecondPackets = 0;
|
||||
QTimer* m_cleanupTimer;
|
||||
QTimer* m_statsUpdateTimer;
|
||||
};
|
||||
#endif
|
||||
Reference in New Issue
Block a user