qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
coreext.h
Go to the documentation of this file.
1 /*
2  Copyright © 2019-2020 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include "src/model/status.h"
23 #include "icoreextpacket.h"
24 
25 #include <QObject>
26 #include <QMap>
27 
28 #include <bitset>
29 #include <memory>
30 #include <mutex>
31 #include <unordered_map>
32 
33 struct Tox;
34 struct ToxExt;
35 struct ToxExtensionMessages;
36 struct ToxExtPacketList;
37 
41 class CoreExt : public QObject, public ICoreExtPacketAllocator
42 {
43  Q_OBJECT
44 private:
45  // PassKey idiom to prevent others from making PacketBuilders
46  struct PacketPassKey {};
47 public:
48 
56  static std::unique_ptr<CoreExt> makeCoreExt(Tox* core);
57 
58  // We do registration with our own pointer, need to ensure we're in a stable location
59  CoreExt(CoreExt const& other) = delete;
60  CoreExt(CoreExt&& other) = delete;
61  CoreExt& operator=(CoreExt const& other) = delete;
62  CoreExt& operator=(CoreExt&& other) = delete;
63 
67  void process();
68 
75  void onLosslessPacket(uint32_t friendId, const uint8_t* data, size_t length);
76 
80  class Packet : public ICoreExtPacket
81  {
82  public:
86  Packet(
87  ToxExtPacketList* packetList,
88  ToxExtensionMessages* toxExtMessages,
89  uint32_t friendId,
90  std::mutex* toxext_mutex,
92 
93  // Delete copy constructor, we shouldn't be able to copy
94  Packet(Packet const& other) = delete;
95 
96  Packet(Packet&& other)
97  {
99  packetList = other.packetList;
100  friendId = other.friendId;
101  hasBeenSent = other.hasBeenSent;
102  toxext_mutex = other.toxext_mutex;
103  other.toxExtMessages = nullptr;
104  other.packetList = nullptr;
105  other.friendId = 0;
106  other.hasBeenSent = false;
107  other.toxext_mutex = nullptr;
108  }
109 
110  uint64_t addExtendedMessage(QString message) override;
111 
112  bool send() override;
113  private:
114  std::mutex* toxext_mutex;
115  bool hasBeenSent = false;
116  // Note: non-owning pointer
117  ToxExtensionMessages* toxExtMessages;
118  // Note: packetList is freed on send() call
119  ToxExtPacketList* packetList;
120  uint32_t friendId;
121  };
122 
123  std::unique_ptr<ICoreExtPacket> getPacket(uint32_t friendId) override;
124 
125  uint64_t getMaxExtendedMessageSize();
126 
127 signals:
128  void extendedMessageReceived(uint32_t friendId, const QString& message);
129  void extendedReceiptReceived(uint32_t friendId, uint64_t receiptId);
130  void extendedMessageSupport(uint32_t friendId, bool supported);
131 
132 public slots:
133  void onFriendStatusChanged(uint32_t friendId, Status::Status status);
134 
135 private:
136 
137  static void onExtendedMessageReceived(uint32_t friendId, const uint8_t* data, size_t size, void* userData);
138  static void onExtendedMessageReceipt(uint32_t friendId, uint64_t receiptId, void* userData);
139  static void onExtendedMessageNegotiation(uint32_t friendId, bool compatible, uint64_t maxMessageSize, void* userData);
140 
141  // A little extra cost to hide the deleters, but this lets us fwd declare
142  // and prevent any extension headers from leaking out to the rest of the
143  // system
144  template <class T>
145  using ExtensionPtr = std::unique_ptr<T, void(*)(T*)>;
146 
148 
149  std::mutex toxext_mutex;
150  std::unordered_map<uint32_t, Status::Status> currentStatuses;
153 };
CoreExt::onExtendedMessageReceived
static void onExtendedMessageReceived(uint32_t friendId, const uint8_t *data, size_t size, void *userData)
Definition: coreext.cpp:175
CoreExt::onLosslessPacket
void onLosslessPacket(uint32_t friendId, const uint8_t *data, size_t length)
Handles extension related lossless packets.
Definition: coreext.cpp:66
Status::Status
Status
Definition: status.h:28
ICoreExtPacket
Definition: icoreextpacket.h:36
CoreExt::process
void process()
Periodic service function.
Definition: coreext.cpp:60
CoreExt::currentStatuses
std::unordered_map< uint32_t, Status::Status > currentStatuses
Definition: coreext.h:150
CoreExt::onExtendedMessageReceipt
static void onExtendedMessageReceipt(uint32_t friendId, uint64_t receiptId, void *userData)
Definition: coreext.cpp:181
CoreExt::extendedMessageSupport
void extendedMessageSupport(uint32_t friendId, bool supported)
CoreExt::toxExt
ExtensionPtr< ToxExt > toxExt
Definition: coreext.h:151
icoreextpacket.h
CoreExt::Packet::toxExtMessages
ToxExtensionMessages * toxExtMessages
Definition: coreext.h:117
CoreExt::Packet::addExtendedMessage
uint64_t addExtendedMessage(QString message) override
Adds message to packet.
Definition: coreext.cpp:98
CoreExt::toxExtMessages
ExtensionPtr< ToxExtensionMessages > toxExtMessages
Definition: coreext.h:152
CoreExt::Packet::Packet
Packet(Packet &&other)
Definition: coreext.h:96
CoreExt::ExtensionPtr
std::unique_ptr< T, void(*)(T *)> ExtensionPtr
Definition: coreext.h:145
CoreExt::extendedMessageReceived
void extendedMessageReceived(uint32_t friendId, const QString &message)
CoreExt::PacketPassKey
Definition: coreext.h:46
CoreExt::Packet::friendId
uint32_t friendId
Definition: coreext.h:120
CoreExt::Packet
Definition: coreext.h:80
CoreExt
Definition: coreext.h:41
HistMessageContentType::message
@ message
CoreExt::Packet::packetList
ToxExtPacketList * packetList
Definition: coreext.h:119
CoreExt::toxext_mutex
std::mutex toxext_mutex
Definition: coreext.h:149
CoreExt::Packet::toxext_mutex
std::mutex * toxext_mutex
Definition: coreext.h:114
CoreExt::Packet::hasBeenSent
bool hasBeenSent
Definition: coreext.h:115
CoreExt::Packet::send
bool send() override
Consumes the packet constructed with PacketBuilder packet and sends it to toxext.
Definition: coreext.cpp:138
CoreExt::CoreExt
CoreExt(CoreExt const &other)=delete
CoreExt::Packet::Packet
Packet(ToxExtPacketList *packetList, ToxExtensionMessages *toxExtMessages, uint32_t friendId, std::mutex *toxext_mutex, PacketPassKey)
Internal constructor for a packet.
Definition: coreext.cpp:74
CoreExt::onExtendedMessageNegotiation
static void onExtendedMessageNegotiation(uint32_t friendId, bool compatible, uint64_t maxMessageSize, void *userData)
Definition: coreext.cpp:186
CoreExt::extendedReceiptReceived
void extendedReceiptReceived(uint32_t friendId, uint64_t receiptId)
CoreExt::makeCoreExt
static std::unique_ptr< CoreExt > makeCoreExt(Tox *core)
Creates a CoreExt instance. Using a pointer here makes our registrations with extensions significantl...
Definition: coreext.cpp:35
CoreExt::getMaxExtendedMessageSize
uint64_t getMaxExtendedMessageSize()
Definition: coreext.cpp:152
ICoreExtPacketAllocator
Definition: icoreextpacket.h:63
CoreExt::onFriendStatusChanged
void onFriendStatusChanged(uint32_t friendId, Status::Status status)
Definition: coreext.cpp:157
status.h
CoreExt::operator=
CoreExt & operator=(CoreExt const &other)=delete
CoreExt::getPacket
std::unique_ptr< ICoreExtPacket > getPacket(uint32_t friendId) override
Gets a new packet builder for friend with core friend id friendId.
Definition: coreext.cpp:88