-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRedRelayClient.cpp
461 lines (425 loc) · 16.1 KB
/
RedRelayClient.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
////////////////////////////////////////////////////////////
//
// 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 "Platform.hpp"
#include "RedRelayClient.hpp"
namespace rc{
RedRelayClient::RedRelayClient(){
TcpSocket.setBlocking(false);
UdpSocket.setBlocking(false);
TcpSocket.connect(sf::IpAddress(), 0);
}
void RedRelayClient::SendTcp(const void* data, std::size_t size){
std::size_t sent;
while (TcpSocket.send(data, size, sent) == sf::Socket::Partial) {
data=(char*)data+sent;
size-=sent;
}
}
void RedRelayClient::HandleTCP(const char* Msg, std::size_t Size, uint8_t Type){
switch (Type>>4){
case 0:
if (Size<2){
nextevents.push_back(Event(Event::Error, "Reader error - message too short for a response"));
return;
}
switch (Msg[0]){
case 0:
if (Msg[1]){
if (Size<4){
nextevents.push_back(Event(Event::Error, "Reader error - message too short for a connect response"));
return;
}
PeerID=(uint8_t)Msg[2]|(uint8_t)Msg[3]<<8;
Events.push_back(Event(Event::Connected, std::string(&Msg[4], Size-4)));
ConnectState=RequestingUdp;
LastTimer=Timer();
UdpBuffer[0]=7<<4;
UdpBuffer[1]=PeerID&255;
UdpBuffer[2]=(PeerID>>8)&255;
UdpSocket.send(UdpBuffer, 3, TcpSocket.getRemoteAddress(), TcpSocket.getRemotePort());
} else {
Events.push_back(Event(Event::ConnectDenied, std::string(&Msg[2], Size-2)));
TcpSocket.disconnect();
}
break;
case 1:
if (Size<4){
nextevents.push_back(Event(Event::Error, "Reader error - message too short for a rename response"));
return;
}
if (Msg[1]){
Name=std::string(&Msg[3], (uint8_t)Msg[2]);
Events.push_back(Event(Event::NameSet));
} else {
Events.push_back(Event(Event::NameDenied, std::string(&Msg[3+(uint8_t)Msg[2]], Size-3-(uint8_t)Msg[2])));
}
break;
case 2:
if (Size<6){
nextevents.push_back(Event(Event::Error, "Reader error - message too short for a channel response"));
return;
}
if (Msg[1]){
uint16_t ChannelID = (uint8_t)Msg[4+(uint8_t)Msg[3]]|(uint8_t)Msg[5+(uint8_t)Msg[3]]<<8;
for (Channel&i : Channels) if (i.ID == ChannelID) break;
Channels.push_back(Channel(ChannelID, std::string(&Msg[4], (uint8_t)Msg[3]), Msg[2]));
SelectedChannel=ChannelID;
Channels.at(Channels.size()-1).Master=SelfID();
if (uint32_t(6+(uint8_t)Msg[3])<Size) Channels.at(Channels.size()-1).Master=65535;
for (uint32_t i=6+(uint8_t)Msg[3]; i<Size; i+=(uint8_t)Msg[i+3]+4){
Channels.at(Channels.size()-1).Peers.push_back(Peer((uint8_t)Msg[i]|(uint8_t)Msg[i+1]<<8, std::string(&Msg[i+4], (uint8_t)Msg[i+3])));
if ((Msg[i+2]&1)!=0) Channels.at(Channels.size()-1).Master=(uint8_t)Msg[i]|(uint8_t)Msg[i+1]<<8;
}
Events.push_back(Event(Event::ChannelJoin, Channels.at(Channels.size()-1).Name, 0, SelectedChannel));
} else {
Events.push_back(Event(Event::ChannelDenied, std::string(&Msg[3+(uint8_t)Msg[2]], Size-(uint8_t)Msg[2]-3)));
}
break;
case 3:
if (Size<4){
nextevents.push_back(Event(Event::Error, "Reader error - message too short for a leave response"));
return;
}
if (Msg[1]){
uint16_t ChannelID = (uint8_t)Msg[2]|(uint8_t)Msg[3]<<8;
for (uint32_t i=0; i<Channels.size(); ++i) if (Channels.at(i).ID == ChannelID){
Events.push_back(Event(Event::ChannelLeave, Channels.at(i).Name, 0, Channels.at(i).ID));
Channels.erase(Channels.begin()+i);
if (i>0) SelectedChannel=Channels.at(i-1).ID;
}
} else {
Events.push_back(Event(Event::ChannelLeaveDenied, std::string(&Msg[4], Size-4), 0, (uint8_t)Msg[2]|(uint8_t)Msg[3]<<8));
}
break;
case 4:
if (Msg[1]){
uint16_t ChannelsCount=0;
for (uint32_t i=2; i<Size; i+=(uint8_t)Msg[i+2]+3) ++ChannelsCount;
Events.push_back(Event(Event::ListReceived, "", ChannelsCount));
for (uint32_t i=2; i<Size; i+=(uint8_t)Msg[i+2]+3) Events.push_back(Event(Event::ListEntry, std::string(&Msg[i+3], (uint8_t)Msg[i+2]), 0, 0, (uint8_t)Msg[i]|(uint8_t)Msg[i+1]<<8));
} else Events.push_back(Event(Event::ListDenied, std::string(&Msg[2], Size-2)));
default:
break;
}
break;
case 2:
if (Size<6) break;
Events.push_back(Event(Event::ChannelSent, std::string(&Msg[5], Size-5), (uint8_t)Msg[3]|(uint8_t)Msg[4]<<8, (uint8_t)Msg[1]|(uint8_t)Msg[2]<<8, (uint8_t)Msg[0]|(Type&15)<<8));
break;
case 3:
if (Size<6) break;
Events.push_back(Event(Event::PeerSent, std::string(&Msg[5], Size-5), (uint8_t)Msg[3]|(uint8_t)Msg[4]<<8, (uint8_t)Msg[1]|(uint8_t)Msg[2]<<8, (uint8_t)Msg[0]|(Type&15)<<8));
break;
case 9:
{
if (Size<4) break;
uint16_t channel=(uint8_t)Msg[0]|(uint8_t)Msg[1]<<8, peer=(uint8_t)Msg[2]|(uint8_t)Msg[3]<<8;
bool quit=false;
for (Channel&i : Channels) if (i.ID==channel){
for (uint32_t j=0; j<i.Peers.size(); ++j) if (i.Peers.at(j).ID==peer){
if (Size==4){
if (i.Peers.at(j).ID==i.Master) i.Master=65535;
Events.push_back(Event(Event::PeerLeft, i.Peers.at(j).Name, i.Peers.at(j).ID, i.ID, i.Master==65535));
i.Peers.erase(i.Peers.begin()+j);
quit=true;
break;
}
if (Size==5 && Msg[4]){
i.Master=peer;
quit=true;
break;
}
if (Size>5){
Events.push_back(Event(Event::PeerChangedName, i.Peers.at(j).Name, i.Peers.at(j).ID, i.ID));
i.Peers.at(j).Name=std::string(&Msg[5], Size-5);
quit=true;
break;
}
}
if (quit || Size<5) break;
i.Peers.push_back(Peer(peer, std::string(&Msg[5], Size-5)));
Events.push_back(Event(Event::PeerJoined, std::string(&Msg[5], Size-5), peer, i.ID));
break;
}
}
break;
case 11:
packet.Clear();
packet.SetType(9);
SendTcp(packet.GetPacket(), packet.GetPacketSize());
break;
default:
break;
}
}
void RedRelayClient::HandleUDP(std::size_t received){
switch (((unsigned char)UdpBuffer[0])>>4){
case 2:
if (received<6) return;
Events.push_back(Event(Event::ChannelBlast, std::string(&UdpBuffer[6], received-6), (uint8_t)UdpBuffer[4]|(uint8_t)UdpBuffer[5]<<8, (uint8_t)UdpBuffer[2]|(uint8_t)UdpBuffer[3]<<8, (uint8_t)UdpBuffer[1]|((uint8_t)UdpBuffer[0]&15)<<8));
break;
case 3:
if (received<6) return;
Events.push_back(Event(Event::PeerBlast, std::string(&UdpBuffer[6], received-6), (uint8_t)UdpBuffer[4]|(uint8_t)UdpBuffer[5]<<8, (uint8_t)UdpBuffer[2]|(uint8_t)UdpBuffer[3]<<8, (uint8_t)UdpBuffer[1]|((uint8_t)UdpBuffer[0]&15)<<8));
case 10:
if (ConnectState==RequestingUdp) Events.push_back(Event::Established);
ConnectState=Established;
break;
default:
break;
}
}
float RedRelayClient::Timer(){
return (float)(TimerClock.getElapsedTime().asMilliseconds()*0.001);
}
std::string RedRelayClient::GetVersion() const {
return "RedRelay Client #"+std::to_string(REDRELAY_CLIENT_BUILD)+" ("+OPERATING_SYSTEM+"/"+ARCHITECTURE+")";
}
void RedRelayClient::Connect(const std::string& Address, uint16_t Port){
if (ConnectState>Disconnected){
nextevents.push_back(Event(Event::Error, "Socket error - Already connected to a server"));
return;
}
reader.Clear();
TcpSocket.connect(sf::IpAddress(Address), Port);
LastTimer=Timer();
ConnectState=Connecting;
}
void RedRelayClient::Disconnect(){
if (ConnectState==Disconnected) return;
nextevents.push_back(Event(Event::Disconnected, TcpSocket.getRemoteAddress().toString()+":"+std::to_string(TcpSocket.getRemotePort())));
TcpSocket.disconnect();
Channels.clear();
reader.Clear();
ConnectState=Disconnected;
}
std::string RedRelayClient::GetHostAddress() const {
return TcpSocket.getRemoteAddress().toString();
}
uint16_t RedRelayClient::GetHostPort() const {
return TcpSocket.getRemotePort();
}
uint8_t RedRelayClient::GetConnectState() const {
return ConnectState;
}
void RedRelayClient::Update(){
for (Event&i : nextevents) Events.push_back(i);
nextevents.clear();
std::size_t received;
sf::Socket::Status status;
do{
reader.CheckBounds();
status = TcpSocket.receive(reader.GetReceiveAddr(), reader.GetReceiveSize(), received);
if (status == sf::Socket::Done){
reader.Received(received);
while (reader.PacketReady()){
HandleTCP(reader.GetPacket(), reader.PacketSize(), reader.GetPacketType());
reader.NextPacket();
}
}
} while (status==sf::Socket::Done);
if (status==sf::Socket::Disconnected && ConnectState>Connecting) Disconnect();
sf::IpAddress UdpAddress; uint16_t UdpPort;
while (UdpSocket.receive(UdpBuffer, 65536, received, UdpAddress, UdpPort) == sf::Socket::Done) if (UdpAddress==TcpSocket.getRemoteAddress()) HandleUDP(received);
if (ConnectState<Established && ConnectState>Disconnected){
switch (ConnectState){
case Connecting:
if (TcpSocket.receive(&received, 0, received)==sf::Socket::Disconnected || TcpSocket.getRemotePort()==0){
if (Timer()>LastTimer+3){
Events.push_back(Event(Event::Error, "Socket error - Error connecting"));
Disconnect();
}
return;
} else {
char tmp=0;
SendTcp(&tmp, 1);
sf::sleep(sf::milliseconds(10));
packet.Clear();
packet.SetType(0);
packet.AddByte(0);
packet.AddString("revision 3");
SendTcp(packet.GetPacket(), packet.GetPacketSize());
ConnectState=RequestingTcp;
}
break;
case RequestingUdp:
if (Timer()<LastTimer+1) return;
LastTimer=Timer();
UdpBuffer[0]=7<<4;
UdpBuffer[1]=PeerID&255;
UdpBuffer[2]=(PeerID>>8)&255;
UdpSocket.send(UdpBuffer, 3, TcpSocket.getRemoteAddress(), TcpSocket.getRemotePort());
break;
default:
break;
}
}
}
uint16_t RedRelayClient::SelfID() const {
return PeerID;
}
void RedRelayClient::SetName(const std::string& Name){
if (ConnectState<RequestingUdp) return;
packet.Clear();
packet.SetType(0);
packet.AddByte(1);
packet.AddString(Name);
SendTcp(packet.GetPacket(), packet.GetPacketSize());
}
std::string RedRelayClient::SelfName() const {
return Name;
}
void RedRelayClient::JoinChannel(const std::string& ChannelName, uint8_t Flags){
if (ConnectState<RequestingUdp) return;
packet.Clear();
packet.SetType(0);
packet.AddByte(2);
packet.AddByte(Flags);
packet.AddString(ChannelName);
SendTcp(packet.GetPacket(), packet.GetPacketSize());
}
const std::vector<Channel>& RedRelayClient::GetJoinedChannels() const {
return Channels;
}
const Channel& RedRelayClient::GetChannel(const std::string& Name) const {
if (Name=="" && Channels.size()>0) return GetChannel(SelectedChannel);
for (const Channel&i : Channels) if (i.Name==Name) return i;
return defchannel;
}
const Channel& RedRelayClient::GetChannel(uint16_t ID) const {
for (const Channel&i : Channels) if (i.ID==ID) return i;
return defchannel;
}
void RedRelayClient::LeaveChannel(const std::string& Name){
if (ConnectState<RequestingUdp) return;
if (Name=="" && Channels.size()>0){
LeaveChannel(SelectedChannel);
return;
} else
for (Channel&i : Channels) if (i.Name==Name){
packet.Clear();
packet.SetType(0);
packet.AddByte(3);
packet.AddShort(i.ID);
SendTcp(packet.GetPacket(), packet.GetPacketSize());
}
}
void RedRelayClient::LeaveChannel(uint16_t ID){
for (Channel&i : Channels) if (i.ID==ID){
packet.Clear();
packet.SetType(0);
packet.AddByte(3);
packet.AddShort(ID);
SendTcp(packet.GetPacket(), packet.GetPacketSize());
}
}
void RedRelayClient::RequestChannelsList(){
if (ConnectState<RequestingUdp) return;
packet.Clear();
packet.SetType(0);
packet.AddByte(4);
SendTcp(packet.GetPacket(), packet.GetPacketSize());
}
void RedRelayClient::SelectChannel(const std::string& Name){
for (Channel&i : Channels) if (i.Name==Name) SelectedChannel=i.ID;
}
void RedRelayClient::SelectChannel(uint16_t ID){
for (Channel&i : Channels) if (i.ID==ID) SelectedChannel=ID;
}
void RedRelayClient::ChannelSend(const void* Data, std::size_t Size, uint8_t Subchannel, uint8_t Variant, uint16_t ChannelID){
if (ConnectState<RequestingUdp) return;
if (ChannelID==65535) ChannelID=SelectedChannel;
for (const Channel&i : Channels) if (i.ID==ChannelID){
packet.Clear();
packet.SetType(2);
packet.SetVariant(Variant);
packet.AddByte(Subchannel);
packet.AddShort(ChannelID);
packet.AddBinary(Data, Size);
SendTcp(packet.GetPacket(), packet.GetPacketSize());
return;
}
}
void RedRelayClient::ChannelSend(const Binary& Binary, uint8_t Subchannel, uint8_t Variant, uint16_t ChannelID){
ChannelSend(Binary.GetAddress(), Binary.GetSize(), Subchannel, Variant, ChannelID);
}
void RedRelayClient::PeerSend(const void* Data, std::size_t Size, uint16_t PeerID, uint8_t Subchannel, uint8_t Variant, uint16_t ChannelID){
if (ConnectState<RequestingUdp) return;
if (ChannelID==65535) ChannelID=SelectedChannel;
for (const Channel&i : Channels) if (i.ID==ChannelID) for (const Peer&j : i.Peers) if (j.ID==PeerID){
packet.Clear();
packet.SetType(3);
packet.SetVariant(Variant);
packet.AddByte(Subchannel);
packet.AddShort(ChannelID);
packet.AddShort(PeerID);
packet.AddBinary(Data, Size);
SendTcp(packet.GetPacket(), packet.GetPacketSize());
return;
}
}
void RedRelayClient::PeerSend(const Binary& Binary, uint16_t PeerID, uint8_t Subchannel, uint8_t Variant, uint16_t ChannelID){
PeerSend(Binary.GetAddress(), Binary.GetSize(), PeerID, Subchannel, Variant, ChannelID);
}
void RedRelayClient::ChannelBlast(const void* Data, std::size_t Size, uint8_t Subchannel, uint8_t Variant, uint16_t ChannelID){
if (ConnectState<RequestingUdp) return;
if (ChannelID==65535) ChannelID=SelectedChannel;
if (Size > 65530) Size = 65530;
for (const Channel&i : Channels) if (i.ID==ChannelID){
UdpBuffer[0]=(2<<4)|(Variant&15);
UdpBuffer[1]=PeerID&255;
UdpBuffer[2]=(PeerID>>8)&255;
UdpBuffer[3]=Subchannel;
UdpBuffer[4]=ChannelID&255;
UdpBuffer[5]=(ChannelID>>8)&255;
memcpy(&UdpBuffer[6], Data, Size);
UdpSocket.send(UdpBuffer, 6+Size, TcpSocket.getRemoteAddress(), TcpSocket.getRemotePort());
return;
}
}
void RedRelayClient::ChannelBlast(const Binary& Binary, uint8_t Subchannel, uint8_t Variant, uint16_t ChannelID){
ChannelBlast(Binary.GetAddress(), Binary.GetSize(), Subchannel, Variant, ChannelID);
}
void RedRelayClient::PeerBlast(const void* Data, std::size_t Size, uint16_t PeerID, uint8_t Subchannel, uint8_t Variant, uint16_t ChannelID){
if (ConnectState<RequestingUdp) return;
if (ChannelID==65535) ChannelID=SelectedChannel;
if (Size > 65528) Size = 65528;
for (const Channel&i : Channels) if (i.ID==ChannelID) for (const Peer&j : i.Peers) if (j.ID==PeerID){
UdpBuffer[0]=(3<<4)|(Variant&15);
UdpBuffer[1]=this->PeerID&255;
UdpBuffer[2]=(this->PeerID>>8)&255;
UdpBuffer[3]=Subchannel;
UdpBuffer[4]=ChannelID&255;
UdpBuffer[5]=(ChannelID>>8)&255;
UdpBuffer[6]=PeerID&255;
UdpBuffer[7]=(PeerID>>8)&255;
memcpy(&UdpBuffer[8], Data, Size);
UdpSocket.send(UdpBuffer, 8+Size, TcpSocket.getRemoteAddress(), TcpSocket.getRemotePort());
return;
}
}
void RedRelayClient::PeerBlast(const Binary& Binary, uint16_t PeerID, uint8_t Subchannel, uint8_t Variant, uint16_t ChannelID){
PeerBlast(Binary.GetAddress(), Binary.GetSize(), PeerID, Subchannel, Variant, ChannelID);
}
const Channel RedRelayClient::defchannel(0, "", 0);
}