swift
clientconnectiondata.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include "misc/logmessage.h"
7 
8 using namespace swift::misc;
9 using namespace swift::core::afv::crypto;
10 
11 namespace swift::core::afv::connection
12 {
13  const QStringList &CClientConnectionData::getLogCategories()
14  {
15  static const QStringList cats { CLogCategories::audio(), CLogCategories::vatsimSpecific() };
16  return cats;
17  }
18 
19  qint64 CClientConnectionData::secondsSinceAuthentication() const
20  {
21  return m_authenticatedDateTimeUtc.secsTo(QDateTime::currentDateTimeUtc());
22  }
23 
24  bool CClientConnectionData::isVoiceServerAlive() const
25  {
26  if (!m_lastVoiceServerHeartbeatAckUtc.isValid()) { return false; }
27  const qint64 d = qAbs(m_lastVoiceServerHeartbeatAckUtc.secsTo(QDateTime::currentDateTimeUtc()));
28  return d < ServerTimeoutSecs;
29  }
30 
31  bool CClientConnectionData::isDataServerAlive() const
32  {
33  if (!m_lastDataServerHeartbeatAckUtc.isValid()) { return false; }
34  const qint64 d = qAbs(m_lastDataServerHeartbeatAckUtc.secsTo(QDateTime::currentDateTimeUtc()));
35  return d < ServerTimeoutSecs;
36  }
37 
38  void CClientConnectionData::createCryptoChannels()
39  {
40  if (!m_tokens.isValid)
41  {
42  CLogMessage(this).warning(u"Tokens not set");
43  return;
44  }
45  try
46  {
47  m_voiceCryptoChannel.reset(new CCryptoDtoChannel(m_tokens.VoiceServer.channelConfig));
48  }
49  catch (const std::invalid_argument &)
50  {
51  m_voiceCryptoChannel.reset();
52  }
53  }
54 
55  void CClientConnectionData::setTsAuthenticatedToNow()
56  {
57  m_authenticatedDateTimeUtc = QDateTime::currentDateTimeUtc();
58  }
59 
60  void CClientConnectionData::setTsHeartbeatToNow()
61  {
62  const QDateTime now = QDateTime::currentDateTimeUtc();
63  m_lastVoiceServerHeartbeatAckUtc = now;
64  m_lastDataServerHeartbeatAckUtc = now;
65  }
66 
67  void CClientConnectionData::reset()
68  {
69  m_userName.clear();
70  m_callsign.clear();
71  m_authenticatedDateTimeUtc = QDateTime();
72  m_lastVoiceServerHeartbeatAckUtc = QDateTime();
73  this->setTokens({});
74  }
75 
76  bool CClientConnectionData::voiceServerAlive() const
77  {
78  return (m_authenticatedDateTimeUtc.isValid() && timeSinceAuthenticationSecs() < ServerTimeoutSecs) ||
79  (m_lastVoiceServerHeartbeatAckUtc.isValid() &&
80  m_lastVoiceServerHeartbeatAckUtc.secsTo(QDateTime::currentDateTimeUtc()) < ServerTimeoutSecs);
81  }
82 } // namespace swift::core::afv::connection
static const QString & vatsimSpecific()
VATSIM specific.
static const QString & audio()
Audio related.
Definition: logcategories.h:52
Class for emitting a log message.
Definition: logmessage.h:27
Derived & warning(const char16_t(&format)[N])
Set the severity to warning, providing a format string.
Free functions in swift::misc.