RunicVTT Open Source Virtual Tabletop for TTRPG using P2P
Loading...
Searching...
No Matches
PeerLink.h
Go to the documentation of this file.
1#pragma once
2#include <rtc/rtc.hpp>
3#include <functional>
4#include <string>
5
6class NetworkManager; // forward declare
7
9{
10public:
11 PeerLink(const std::string& id, std::weak_ptr<NetworkManager> parent);
13
14 void close();
15
16 void createChannels(); // creates Intent, State, Snapshot, Chat (as offerer)
17
18 //void createPeerConnection();
19 void createDataChannel(const std::string& label);
20 rtc::Description createOffer();
21 rtc::Description createAnswer();
22 void setRemoteDescription(const rtc::Description& desc);
23 void addIceCandidate(const rtc::Candidate& candidate);
24
25 void send(const std::string& msg);
26 bool sendOn(const std::string& label, const std::vector<uint8_t>& bytes);
27 bool sendOn(const std::string& label, std::string_view text);
28 bool sendGame(const std::vector<uint8_t>& bytes);
29 bool sendChat(const std::vector<uint8_t>& bytes);
30 bool sendNote(const std::vector<uint8_t>& bytes);
31 bool sendMarkerMove(const std::vector<uint8_t>& bytes);
32 void sendChatJson(const std::string& jsonText);
33
34 void setDisplayName(std::string n);
35 const std::string& displayName() const;
36
37 void attachChannelHandlers(const std::shared_ptr<rtc::DataChannel>& ch, const std::string& label);
38 void attachMarkerMoveChannelHandlers(const std::shared_ptr<rtc::DataChannel>& ch, const std::string& label);
39
40 bool isDataChannelOpen() const;
41 rtc::PeerConnection::State pcState() const; // optional
42 const char* pcStateString() const;
43 bool isClosedOrFailed() const;
44 const char* pcStateToStr(rtc::PeerConnection::State s);
45
46 // send helpers
47 /* void sendIntent(const std::vector<uint8_t>& bytes);
48 void sendState(const std::vector<uint8_t>& bytes);
49 void sendSnapshot(const std::vector<uint8_t>& bytes);
50 void sendChat(const std::vector<uint8_t>& bytes);*/
51
52 // accessors
53 bool isConnected() const; // PC connected + *at least* Intent channel open
54 bool isPcConnectedOnly() const;
55 //static constexpr size_t kMaxBufferedBytes = 5 /*MB*/ * 1024 * 1024; //(tune as you like)
56 void setOpen(std::string label, bool open)
57 {
58 dcOpen_[label] = open;
59 }
60
61 bool allRequiredOpen() const;
62 bool bootstrapSent() const
63 {
64 return bootstrapSent_;
65 }
67 {
68 bootstrapSent_ = true;
69 }
71 {
72 bootstrapSent_ = false;
73 }
74
75 std::shared_ptr<rtc::PeerConnection> getPeerConnection()
76 {
77 return pc;
78 }
79
80private:
81 std::string peerId;
82 std::string displayName_;
83 std::shared_ptr<rtc::PeerConnection> pc;
84 //std::shared_ptr<rtc::DataChannel> dc;
85 std::unordered_map<std::string, std::shared_ptr<rtc::DataChannel>> dcs_;
86 std::atomic<bool> closing_{false};
87 std::weak_ptr<NetworkManager> network_manager;
88
89 std::atomic<rtc::PeerConnection::State> lastState_{rtc::PeerConnection::State::New};
90 std::atomic<double> lastStateAt_{0.0};
91
92 std::atomic<bool> remoteDescSet_{false};
93 std::vector<rtc::Candidate> pendingRemoteCandidates_;
94 std::mutex candMx_;
95
96 bool bootstrapSent_ = false;
97 std::unordered_map<std::string, bool> dcOpen_;
98
100 //void onIntentMessage(const std::vector<uint8_t>& bytes);
101 //void onStateMessage(const std::vector<uint8_t>& bytes);
102 //void onSnapshotMessage(const std::vector<uint8_t>& bytes);
103 //void onChatMessage(const std::vector<uint8_t>& bytes);
104
105 void setupCallbacks(); // internal
106};
107
108/*
109peerLink->onSendDescription = [this](const rtc::Description& desc) {
110 signalingClient.sendSdp(desc);
111};
112
113peerLink->onSendCandidate = [this](const rtc::Candidate& cand) {
114 signalingClient.sendCandidate(cand);
115};
116
117
118*/
Definition Message.h:28