Overte C++ Documentation
ReceivedPacketProcessor.h
1 //
2 // ReceivedPacketProcessor.h
3 // libraries/networking/src
4 //
5 // Created by Brad Hefta-Gaub on 8/12/13.
6 // Copyright 2013 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_ReceivedPacketProcessor_h
13 #define hifi_ReceivedPacketProcessor_h
14 
15 #include <QtCore/QSharedPointer>
16 #include <QWaitCondition>
17 
18 #include "NodeList.h"
19 
20 #include "GenericThread.h"
21 
22 class ReceivedMessage;
23 
26  Q_OBJECT
27 public:
28  static const uint64_t MAX_WAIT_TIME { 100 }; // Max wait time in ms
29 
31 
33  void queueReceivedPacket(QSharedPointer<ReceivedMessage> message, SharedNodePointer sendingNode);
34 
36  bool hasPacketsToProcess() const { return _packets.size() > 0; }
37 
39  bool isAlive(const QUuid& nodeUUID) const {
40  return _nodePacketCounts.contains(nodeUUID);
41  }
42 
44  bool hasPacketsToProcessFrom(const SharedNodePointer& sendingNode) const {
45  return hasPacketsToProcessFrom(sendingNode->getUUID());
46  }
47 
49  bool hasPacketsToProcessFrom(const QUuid& nodeUUID) const {
50  return _nodePacketCounts[nodeUUID] > 0;
51  }
52 
54  int packetsToProcessCount() const { return (int)_packets.size(); }
55 
56  float getIncomingPPS() const { return _incomingPPS.getAverage(); }
57  float getProcessedPPS() const { return _processedPPS.getAverage(); }
58 
59  virtual void terminating() override;
60 
61 public slots:
62  void nodeKilled(SharedNodePointer node);
63 
64 protected:
68  virtual void processPacket(QSharedPointer<ReceivedMessage> message, SharedNodePointer sendingNode) = 0;
69 
71  virtual bool process() override;
72 
74  virtual uint32_t getMaxWait() const { return MAX_WAIT_TIME; }
75 
77  virtual void preProcess() { }
78 
80  virtual void midProcess() { }
81 
83  virtual void postProcess() { }
84 
85 protected:
86  std::list<NodeSharedReceivedMessagePair> _packets;
87  QHash<QUuid, int> _nodePacketCounts;
88 
89  QWaitCondition _hasPackets;
90  QMutex _waitingOnPacketsMutex;
91 
92  quint64 _lastWindowAt = 0;
93  int _lastWindowIncomingPackets = 0;
94  int _lastWindowProcessedPackets = 0;
95  SimpleMovingAverage _incomingPPS;
96  SimpleMovingAverage _processedPPS;
97 };
98 
99 #endif // hifi_ReceivedPacketProcessor_h
Definition: GenericThread.h:23
Generalized threaded processor for handling received inbound packets.
Definition: ReceivedPacketProcessor.h:25
virtual uint32_t getMaxWait() const
Determines the timeout of the wait when there are no packets to process. Default value is 100ms to al...
Definition: ReceivedPacketProcessor.h:74
virtual void processPacket(QSharedPointer< ReceivedMessage > message, SharedNodePointer sendingNode)=0
virtual void preProcess()
Override to do work before the packets processing loop. Default does nothing.
Definition: ReceivedPacketProcessor.h:77
int packetsToProcessCount() const
How many received packets waiting are to be processed.
Definition: ReceivedPacketProcessor.h:54
bool hasPacketsToProcess() const
Are there received packets waiting to be processed.
Definition: ReceivedPacketProcessor.h:36
virtual void postProcess()
Override to do work after the packets processing loop. Default does nothing.
Definition: ReceivedPacketProcessor.h:83
void queueReceivedPacket(QSharedPointer< ReceivedMessage > message, SharedNodePointer sendingNode)
Add packet from network receive thread to the processing queue.
Definition: ReceivedPacketProcessor.cpp:28
bool hasPacketsToProcessFrom(const QUuid &nodeUUID) const
Are there received packets waiting to be processed from a specified node.
Definition: ReceivedPacketProcessor.h:49
virtual void midProcess()
Override to do work inside the packet processing loop after a packet is processed....
Definition: ReceivedPacketProcessor.h:80
bool hasPacketsToProcessFrom(const SharedNodePointer &sendingNode) const
Are there received packets waiting to be processed from a specified node.
Definition: ReceivedPacketProcessor.h:44
bool isAlive(const QUuid &nodeUUID) const
Is a specified node still alive?
Definition: ReceivedPacketProcessor.h:39
virtual bool process() override
Implements generic processing behavior for this thread.
Definition: ReceivedPacketProcessor.cpp:39