Overte C++ Documentation
OctreeEditPacketSender.h
1 //
2 // OctreeEditPacketSender.h
3 // libraries/octree/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_OctreeEditPacketSender_h
13 #define hifi_OctreeEditPacketSender_h
14 
15 #include <unordered_map>
16 
17 #include <PacketSender.h>
18 #include <udt/PacketHeaders.h>
19 
20 #include "SentPacketHistory.h"
21 
24  Q_OBJECT
25 public:
28 
32  void queueOctreeEditMessage(PacketType type, QByteArray& editMessage);
33 
40  void releaseQueuedMessages();
41 
44 
48 
50  virtual bool process() override;
51 
57  void setMaxPendingMessages(int maxPendingMessages) { _maxPendingMessages = maxPendingMessages; }
58 
59  // the default number of pending messages we will store if no servers are available
60  static const int DEFAULT_MAX_PENDING_MESSAGES;
61 
62  // is there an octree server available to send packets to
63  bool serversExist() const;
64 
65  // you must override these...
66  virtual char getMyNodeType() const = 0;
67  virtual void adjustEditPacketForClockSkew(PacketType type, QByteArray& buffer, qint64 clockSkew) { }
68 
69  void processNackPacket(ReceivedMessage& message, SharedNodePointer sendingNode);
70 
71 public slots:
72  void nodeKilled(SharedNodePointer node);
73 
74 protected:
75  using EditMessagePair = std::pair<PacketType, QByteArray>;
76 
77  void queuePacketToNode(const QUuid& nodeID, std::unique_ptr<NLPacket> packet);
78  void queuePacketListToNode(const QUuid& nodeUUID, std::unique_ptr<NLPacketList> packetList);
79 
80  void queuePendingPacketToNodes(std::unique_ptr<NLPacket> packet);
81  void queuePacketToNodes(std::unique_ptr<NLPacket> packet);
82  std::unique_ptr<NLPacket> initializePacket(PacketType type, qint64 nodeClockSkew);
83  void releaseQueuedPacket(const QUuid& nodeUUID, std::unique_ptr<NLPacket> packetBuffer); // releases specific queued packet
84  void releaseQueuedPacketList(const QUuid& nodeID, std::unique_ptr<NLPacketList> packetList);
85 
86  void processPreServerExistsPackets();
87 
88  // These are packets which are destined from know servers but haven't been released because they're still too small
89  // protected by _packetsQueueLock
90  std::unordered_map<QUuid, PacketOrPacketList> _pendingEditPackets;
91 
92  // These are packets that are waiting to be processed because we don't yet know if there are servers
93  int _maxPendingMessages;
94  bool _releaseQueuedMessagesPending;
95  QMutex _pendingPacketsLock;
96  QRecursiveMutex _packetsQueueLock; // don't let different threads release the queue while another thread is writing to it
97  std::list<EditMessagePair> _preServerEdits; // these will get packed into other larger packets
98  std::list<std::unique_ptr<NLPacket>> _preServerSingleMessagePackets; // these will go out as is
99 
100  QMutex _releaseQueuedPacketMutex;
101 
102  // protected by _packetsQueueLock
103  std::unordered_map<QUuid, SentPacketHistory> _sentPacketHistories;
104  std::unordered_map<QUuid, quint16> _outgoingSequenceNumbers;
105 };
106 #endif // hifi_OctreeEditPacketSender_h
Utility for processing, packing, queueing and sending of outbound edit messages.
Definition: OctreeEditPacketSender.h:23
void setMaxPendingMessages(int maxPendingMessages)
Definition: OctreeEditPacketSender.h:57
void queueOctreeEditMessage(PacketType type, QByteArray &editMessage)
Definition: OctreeEditPacketSender.cpp:163
void releaseQueuedMessages()
Definition: OctreeEditPacketSender.cpp:255
virtual bool process() override
if you're running in non-threaded mode, you must call this method regularly
Definition: OctreeEditPacketSender.cpp:318
Generalized threaded processor for queueing and sending of outbound packets.
Definition: PacketSender.h:24