Overte C++ Documentation
NetworkPeer.h
1 //
2 // NetworkPeer.h
3 // libraries/networking/src
4 //
5 // Created by Stephen Birarda on 2014-10-02.
6 // Copyright 2014 High Fidelity, Inc.
7 // Copyright 2021 Vircadia contributors.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 //
12 
13 #ifndef hifi_NetworkPeer_h
14 #define hifi_NetworkPeer_h
15 
16 #include <atomic>
17 
18 #include <QtCore/QObject>
19 #include <QtCore/QSharedPointer>
20 #include <QtCore/QTimer>
21 #include <QtCore/QUuid>
22 
23 #include "SockAddr.h"
24 #include "UUID.h"
25 
26 const QString ICE_SERVER_HOSTNAME = "localhost";
27 const quint16 ICE_SERVER_DEFAULT_PORT = 7337;
28 const int ICE_HEARBEAT_INTERVAL_MSECS = 1 * 1000;
29 const int MAX_ICE_CONNECTION_ATTEMPTS = 5;
30 
31 const int UDP_PUNCH_PING_INTERVAL_MS = 250;
32 
33 class NetworkPeer : public QObject {
34  Q_OBJECT
35 public:
36  NetworkPeer(QObject* parent = 0);
37  NetworkPeer(const QUuid& uuid, const SockAddr& publicSocket, const SockAddr& localSocket, QObject* parent = 0);
38 
39  bool isNull() const { return _uuid.isNull(); }
40  bool hasSockets() const { return !_localSocket.isNull() && !_publicSocket.isNull(); }
41 
42  const QUuid& getUUID() const { return _uuid; }
43  void setUUID(const QUuid& uuid) { _uuid = uuid; }
44 
45  using LocalID = NetworkLocalID;
46  static const LocalID NULL_LOCAL_ID = 0;
47 
48  LocalID getLocalID() const { return _localID; }
49  void setLocalID(LocalID localID) { _localID = localID; }
50 
51  void softReset();
52  void reset();
53 
54  const SockAddr& getPublicSocket() const { return _publicSocket; }
55  const SockAddr& getLocalSocket() const { return _localSocket; }
56  const SockAddr& getSymmetricSocket() const { return _symmetricSocket; }
57 
58  void setPublicSocket(const SockAddr& publicSocket);
59  void setLocalSocket(const SockAddr& localSocket);
60  void setSymmetricSocket(const SockAddr& symmetricSocket);
61 
62  const SockAddr* getActiveSocket() const { return _activeSocket; }
63 
64  void activatePublicSocket();
65  void activateLocalSocket();
66  void activateSymmetricSocket();
67 
68  void activateMatchingOrNewSymmetricSocket(const SockAddr& matchableSockAddr);
69 
70  quint64 getWakeTimestamp() const { return _wakeTimestamp; }
71  void setWakeTimestamp(quint64 wakeTimestamp) { _wakeTimestamp = wakeTimestamp; }
72 
73  quint64 getLastHeardMicrostamp() const { return _lastHeardMicrostamp; }
74  void setLastHeardMicrostamp(quint64 lastHeardMicrostamp) { _lastHeardMicrostamp = lastHeardMicrostamp; }
75 
76  QByteArray toByteArray() const;
77 
78  int getConnectionAttempts() const { return _connectionAttempts; }
79  void incrementConnectionAttempts() { ++_connectionAttempts; }
80  void resetConnectionAttempts() { _connectionAttempts = 0; }
81 
82  // Typically the LimitedNodeList removes nodes after they are "silent"
83  // meaning that we have not received any packets (including simple keepalive pings) from them for a set interval.
84  // The _isForcedNeverSilent flag tells the LimitedNodeList that a Node should never be killed by removeSilentNodes()
85  // even if its the timestamp of when it was last heard from has never been updated.
86 
87  bool isForcedNeverSilent() const { return _isForcedNeverSilent; }
88  void setIsForcedNeverSilent(bool isForcedNeverSilent) { _isForcedNeverSilent = isForcedNeverSilent; }
89 
90  friend QDataStream& operator<<(QDataStream& out, const NetworkPeer& peer);
91  friend QDataStream& operator>>(QDataStream& in, NetworkPeer& peer);
92 public slots:
93  void startPingTimer();
94  void stopPingTimer();
95 
96 signals:
97  void pingTimerTimeout();
98  void socketActivated(const SockAddr& sockAddr);
99  void socketUpdated(SockAddr previousAddress, SockAddr currentAddress);
100 
101 protected:
102  void setActiveSocket(SockAddr* discoveredSocket);
103 
104  QUuid _uuid;
105  LocalID _localID { 0 };
106 
107  SockAddr _publicSocket;
108  SockAddr _localSocket;
109  SockAddr _symmetricSocket;
110  SockAddr* _activeSocket;
111 
112  quint64 _wakeTimestamp;
113  std::atomic_ullong _lastHeardMicrostamp;
114 
115  QTimer* _pingTimer = NULL;
116 
117  int _connectionAttempts;
118 
119  bool _isForcedNeverSilent { false };
120 };
121 
122 QDebug operator<<(QDebug debug, const NetworkPeer &peer);
123 typedef QSharedPointer<NetworkPeer> SharedNetworkPeer;
124 
125 #endif // hifi_NetworkPeer_h