Overte C++ Documentation
PacketSender.h
1 //
2 // PacketSender.h
3 // libraries/networking/src
4 //
5 // Created by Brad Hefta-Gaub on 8/12/13.
6 // Copyright 2013 High Fidelity, Inc.
7 //
8 // Threaded or non-threaded packet sender.
9 //
10 // Distributed under the Apache License, Version 2.0.
11 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
12 //
13 
14 #ifndef hifi_PacketSender_h
15 #define hifi_PacketSender_h
16 
17 #include <QWaitCondition>
18 
19 #include "GenericThread.h"
20 #include "NodeList.h"
21 #include "SharedUtil.h"
22 
24 class PacketSender : public GenericThread {
25  Q_OBJECT
26 public:
27 
28  static const quint64 USECS_PER_SECOND;
29  static const quint64 SENDING_INTERVAL_ADJUST;
30  static const int TARGET_FPS;
31  static const int MAX_SLEEP_INTERVAL;
32 
33  static const int DEFAULT_PACKETS_PER_SECOND;
34  static const int MINIMUM_PACKETS_PER_SECOND;
35  static const int MINIMAL_SLEEP_INTERVAL;
36 
37  PacketSender(int packetsPerSecond = DEFAULT_PACKETS_PER_SECOND);
38  ~PacketSender();
39 
41  void queuePacketForSending(const SharedNodePointer& destinationNode, std::unique_ptr<NLPacket> packet);
42  void queuePacketListForSending(const SharedNodePointer& destinationNode, std::unique_ptr<NLPacketList> packetList);
43 
44  void setPacketsPerSecond(int packetsPerSecond);
45  int getPacketsPerSecond() const { return _packetsPerSecond; }
46 
47  virtual bool process() override;
48  virtual void terminating() override;
49 
51  bool hasPacketsToSend() const { return _packets.size() > 0; }
52 
54  size_t packetsToSendCount() const { return _packets.size(); }
55 
59  void setProcessCallIntervalHint(int usecsPerProcessCall) { _usecsPerProcessCallHint = usecsPerProcessCall; }
60 
62  float getLifetimePPS() const
63  { return getLifetimeInSeconds() == 0 ? 0 : (float)((float)_totalPacketsSent / getLifetimeInSeconds()); }
64 
66  float getLifetimeBPS() const
67  { return getLifetimeInSeconds() == 0 ? 0 : (float)((float)_totalBytesSent / getLifetimeInSeconds()); }
68 
70  float getLifetimePPSQueued() const
71  { return getLifetimeInSeconds() == 0 ? 0 : (float)((float)_totalPacketsQueued / getLifetimeInSeconds()); }
72 
74  float getLifetimeBPSQueued() const
75  { return getLifetimeInSeconds() == 0 ? 0 : (float)((float)_totalBytesQueued / getLifetimeInSeconds()); }
76 
78  quint64 getLifetimeInUsecs() const { return (usecTimestampNow() - _started); }
79 
81  float getLifetimeInSeconds() const { return ((float)getLifetimeInUsecs() / (float)USECS_PER_SECOND); }
82 
84  quint64 getLifetimePacketsSent() const { return _totalPacketsSent; }
85 
87  quint64 getLifetimeBytesSent() const { return _totalBytesSent; }
88 
90  quint64 getLifetimePacketsQueued() const { return _totalPacketsQueued; }
91 
93  quint64 getLifetimeBytesQueued() const { return _totalBytesQueued; }
94 signals:
95  void packetSent(quint64);
96 protected:
97  int _packetsPerSecond;
98  int _usecsPerProcessCallHint;
99  quint64 _lastProcessCallTime;
100  SimpleMovingAverage _averageProcessCallTime;
101 
102 private:
103  std::list<NodePacketOrPacketListPair> _packets;
104  quint64 _lastSendTime;
105 
106  bool threadedProcess();
107  bool nonThreadedProcess();
108 
109  quint64 _lastPPSCheck;
110  size_t _packetsOverCheckInterval;
111 
112  quint64 _started;
113  quint64 _totalPacketsSent;
114  quint64 _totalBytesSent;
115 
116  quint64 _totalPacketsQueued;
117  quint64 _totalBytesQueued;
118 
119  QWaitCondition _hasPackets;
120  QMutex _waitingOnPacketsMutex;
121 };
122 
123 #endif // hifi_PacketSender_h
Definition: GenericThread.h:23
Generalized threaded processor for queueing and sending of outbound packets.
Definition: PacketSender.h:24
quint64 getLifetimeBytesQueued() const
returns the total bytes queued by this object over its lifetime
Definition: PacketSender.h:93
quint64 getLifetimeInUsecs() const
returns lifetime of this object from first packet sent to now in usecs
Definition: PacketSender.h:78
quint64 getLifetimeBytesSent() const
returns the total bytes sent by this object over its lifetime
Definition: PacketSender.h:87
float getLifetimePPS() const
returns the packets per second send rate of this object over its lifetime
Definition: PacketSender.h:62
quint64 getLifetimePacketsSent() const
returns the total packets sent by this object over its lifetime
Definition: PacketSender.h:84
float getLifetimePPSQueued() const
returns the packets per second queued rate of this object over its lifetime
Definition: PacketSender.h:70
bool hasPacketsToSend() const
are there packets waiting in the send queue to be sent
Definition: PacketSender.h:51
quint64 getLifetimePacketsQueued() const
returns the total packets queued by this object over its lifetime
Definition: PacketSender.h:90
virtual bool process() override
Override this function to do whatever your class actually does, return false to exit thread early.
Definition: PacketSender.cpp:81
void queuePacketForSending(const SharedNodePointer &destinationNode, std::unique_ptr< NLPacket > packet)
Add packet to outbound queue.
Definition: PacketSender.cpp:52
float getLifetimeBPS() const
returns the bytes per second send rate of this object over its lifetime
Definition: PacketSender.h:66
void setProcessCallIntervalHint(int usecsPerProcessCall)
Definition: PacketSender.h:59
size_t packetsToSendCount() const
how many packets are there in the send queue waiting to be sent
Definition: PacketSender.h:54
float getLifetimeInSeconds() const
returns lifetime of this object from first packet sent to now in usecs
Definition: PacketSender.h:81
float getLifetimeBPSQueued() const
returns the bytes per second queued rate of this object over its lifetime
Definition: PacketSender.h:74