-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRedRelayServer.hpp
More file actions
225 lines (201 loc) · 7.75 KB
/
Copy pathRedRelayServer.hpp
File metadata and controls
225 lines (201 loc) · 7.75 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
////////////////////////////////////////////////////////////
//
// 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.
//
////////////////////////////////////////////////////////////
#ifndef REDRELAY_SERVER
#define REDRELAY_SERVER
#define REDRELAY_SERVER_BUILD 10
#include <ctime>
#include <unordered_map>
#include <string>
#include <vector>
#include "IDPool.hpp"
#include <SFML/Network.hpp>
#ifdef REDRELAY_EPOLL
#include "EpollSelector.hpp"
#endif
#ifdef REDRELAY_MULTITHREAD
#include <thread>
#endif
namespace rs{
class RelayPacket{
private:
std::size_t capacity;
std::size_t size;
uint8_t type;
char* buffer;
public:
RelayPacket(std::size_t Size=65536);
~RelayPacket();
void Reallocate(std::size_t newcapacity);
void SetType(const uint8_t Type);
void AddByte(const uint8_t Byte);
void AddShort(const uint16_t Short);
void AddInt(const uint32_t Int);
void AddString(const std::string& String);
const char* GetPacket();
std::size_t GetPacketSize() const;
std::size_t GetSize() const;
void Clear();
};
class Peer{
friend class RedRelayServer;
private:
static sf::TcpSocket defsocket;
char buffer[65536];
uint32_t packetsize=0;
uint32_t buffbegin=0;
sf::TcpSocket* Socket=&defsocket;
uint32_t IpAddr=0;
uint16_t UdpPort=0;
uint8_t PingTries=0;
std::string Name;
std::vector<uint16_t> Channels; //Channels used by peer, represented as ID
uint32_t MessageSize() const;
uint8_t SizeOffset() const;
bool MessageReady() const;
void EraseChannel(uint16_t ChannelID);
void AddChannel(uint16_t ChannelID);
public:
std::string GetName() const;
const std::vector<uint16_t>& GetJoinedChannels() const;
sf::IpAddress GetIP() const;
bool IsInChannel(uint16_t ChannelID) const;
};
class Channel{
friend class RedRelayServer;
private:
std::string Name;
std::vector<uint16_t> Peers; //Peers in channel, represented as ID
bool HideFromList=false, CloseOnLeave=false; //Channel flags
uint16_t Master; //Channel master ID
void ErasePeer(uint16_t PeerID);
void AddPeer(uint16_t PeerID);
public:
std::string GetName() const;
bool IsHidden() const;
bool IsAutoClosed() const;
const std::vector<uint16_t>& GetPeerList() const;
uint16_t GetPeersCount() const;
uint16_t GetMasterID() const;
bool HasPeer(uint16_t PeerID) const;
};
class Connection{ //Used for clients before handshake
friend class RedRelayServer;
private:
sf::TcpSocket* Socket=NULL;
std::size_t received=0;
char buffer[14];
};
class RedRelayServer{
private:
struct callstruct{
void (*Error)(const std::string& ErrorMessage);
void (*ServerStart)(uint16_t Port);
bool (*PeerConnect)(uint16_t PeerID, const sf::IpAddress& Address, std::string& DenyReason);
void (*PeerDisconnect)(uint16_t PeerID);
bool (*NameSet)(uint16_t PeerID, std::string& Name, std::string& DenyReason);
bool (*ChannelJoin)(uint16_t PeerID, uint16_t ChannelID, std::string& DenyReason);
bool (*ChannelLeave)(uint16_t PeerID, uint16_t ChannelID, std::string& DenyReason);
void (*ChannelClosed)(uint16_t ChannelID);
bool (*ChannelsListRequest)(uint16_t PeerID, std::string& DenyReason);
void (*ServerMessageSent)(uint16_t PeerID, uint8_t Subchannel, const char* Packet, std::size_t Size);
void (*ServerMessageBlast)(uint16_t PeerID, uint8_t Subchannel, const char* Packet, std::size_t Size);
};
callstruct Callbacks;
//Server configuration
uint16_t ConnectionsLimit, PeersLimit, ChannelsLimit, PeerChannelsLimit;
bool GiveNewMaster, LoggingEnabled;
uint8_t PingInterval;
std::string WelcomeMessage;
#ifdef REDRELAY_MULTITHREAD
volatile bool Running, Destructible;
#else
bool Running, Destructible;
#endif
//Packet buffer
RelayPacket packet;
char UdpBuffer[65536];
//Data containers for peers, channels
std::unordered_map<std::string, uint16_t> ChannelNames;
IndexedPool<Connection> ConnectionsPool;
IndexedPool<Peer> PeersPool;
IndexedPool<Channel> ChannelsPool;
//Network interfaces
sf::TcpListener TcpListener;
sf::UdpSocket UdpSocket;
#ifdef REDRELAY_EPOLL
EpollSelector Selector;
#else
sf::SocketSelector Selector;
#endif
#ifdef REDRELAY_MULTITHREAD
void UdpHandler();
#endif
//Timers
sf::Clock DeltaClock;
float DeltaTime();
float Timer();
//Peers and connections related stuff
void DropConnection(uint16_t ID);
void DenyConnection(uint16_t ID, const std::string& Reason);
void DenyNameChange(uint16_t ID, const std::string& Name, const std::string& Reason);
void DenyChannelJoin(uint16_t ID, const std::string& Name, const std::string& Reason);
void PeerLeftChannel(uint16_t Channel, uint16_t Peer);
void PeerDroppedFromChannel(uint16_t Channel, uint16_t Peer);
//Handling messages
void HandleTCP(uint16_t ID, char* Msg, std::size_t Size, uint8_t Type);
void NewConnection();
void ReceiveUdp();
void ReceiveTcp(uint16_t PeerID);
void HandleConnection(uint16_t ConnectionID);
public:
RedRelayServer();
~RedRelayServer();
std::string GetVersion() const;
void Log(std::string message, uint8_t colour=15);
void SetPingInterval(uint8_t Interval);
void SetConnectionsLimit(uint16_t Limit);
void SetPeersLimit(uint16_t Limit);
void SetChannelsLimit(uint16_t Limit);
void SetChannelsPerPeerLimit(uint16_t Limit);
void SetWelcomeMessage(const std::string& String);
void SetLogEnabled(bool Flag);
const Peer& GetPeer(uint16_t PeerID);
const Channel& GetChannel(uint16_t ChannelID);
void SetErrorCallback(void(*Error)(const std::string& ErrorMessage));
void SetStartCallback(void(*ServerStarted)(uint16_t));
void SetConnectCallback(bool(*PeerConnect)(uint16_t, const sf::IpAddress&, std::string&));
void SetDisconnectCallback(void(*DisconnectCallback)(uint16_t));
void SetNameCallback(bool(*NameSet)(uint16_t, std::string&, std::string&));
void SetChannelJoinCallback(bool(*ChannelJoin)(uint16_t, uint16_t, std::string&));
void SetChannelLeaveCallback(bool(*ChannelLeave)(uint16_t, uint16_t, std::string&));
void SetChannelClosedCallback(void(*ChannelClosed)(uint16_t));
void SetChannelsListRequestCallback(bool(*ChannelsListRequest)(uint16_t, std::string&));
void SetServerSentCallback(void(*ServerMessageSent)(uint16_t, uint8_t, const char*, std::size_t));
void SetServerBlastCallback(void(*ServerMessageBlast)(uint16_t, uint8_t, const char*, std::size_t));
void DropPeer(uint16_t ID);
void Start(uint16_t Port=6121);
void Stop(bool Block=false);
bool IsRunning();
};
}
#endif