-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChannel.cpp
116 lines (94 loc) · 3.32 KB
/
Channel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
////////////////////////////////////////////////////////////
//
// RedRelay - a Lacewing Relay protocol reimplementation
// Copyright (c) 2019 LekKit (LekKit#4400 in Discord)
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#include "RedRelayServer.hpp"
namespace rs{
/////////////
// Channel //
/////////////
std::string Channel::GetName() const {
return Name;
}
bool Channel::IsHidden() const {
return HideFromList;
}
bool Channel::IsAutoClosed() const {
return CloseOnLeave;
}
const std::vector<uint16_t>& Channel::GetPeerList() const {
return Peers;
}
uint16_t Channel::GetPeersCount() const {
return Peers.size();
}
uint16_t Channel::GetMasterID() const {
return Master;
}
bool Channel::HasPeer(uint16_t PeerID) const {
for (uint16_t peer : Peers) if (peer == PeerID) return true;
return false;
}
void Channel::ErasePeer(uint16_t PeerID){
for (uint32_t i=0; i<Peers.size(); ++i) if (Peers[i] == PeerID) Peers.erase(Peers.begin() + i);
}
void Channel::AddPeer(uint16_t PeerID){
Peers.push_back(PeerID);
}
//////////
// Peer //
//////////
uint32_t Peer::MessageSize() const {
if (packetsize>1 && (uint8_t)buffer[buffbegin+1]<254) return (uint8_t)buffer[buffbegin+1];
if (packetsize>3 && (uint8_t)buffer[buffbegin+1]==254) return (uint8_t)buffer[buffbegin+2] | (uint8_t)buffer[buffbegin+3]<<8;
if (packetsize>5) return (uint8_t)buffer[buffbegin+2] | (uint8_t)buffer[buffbegin+3]<<8 | (uint8_t)buffer[buffbegin+4]<<16 | (uint8_t)buffer[buffbegin+5]<<24;
return 65535;
}
uint8_t Peer::SizeOffset() const {
if (packetsize<2) return 0;
if ((uint8_t)buffer[buffbegin+1]<254) return 1;
if ((uint8_t)buffer[buffbegin+1]==254) return 3;
return 5;
}
bool Peer::MessageReady() const {
return MessageSize()+SizeOffset()<packetsize;
}
std::string Peer::GetName() const {
return Name;
}
const std::vector<uint16_t>& Peer::GetJoinedChannels() const {
return Channels;
}
sf::IpAddress Peer::GetIP() const {
return Socket->getRemoteAddress();
}
bool Peer::IsInChannel(uint16_t ChannelID) const {
for (uint16_t channel : Channels) if (channel == ChannelID) return true;
return false;
}
void Peer::EraseChannel(uint16_t ChannelID){
for (uint32_t i=0; i<Channels.size(); ++i) if (Channels[i] == ChannelID) Channels.erase(Channels.begin() + i);
}
void Peer::AddChannel(uint16_t ChannelID){
Channels.push_back(ChannelID);
}
sf::TcpSocket Peer::defsocket;
}