-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEvent.cpp
171 lines (139 loc) · 4.45 KB
/
Event.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
////////////////////////////////////////////////////////////
//
// 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 "RedRelayClient.hpp"
namespace rc{
Event::Event(uint8_t EventType, const std::string& Message, uint16_t Short1, uint16_t Short2, uint16_t Short3){
Type = EventType;
m_string = Message;
m_short1 = Short1;
m_short2 = Short2;
m_short3 = Short3;
}
Event::Event(){
Type = Error;
}
std::string Event::ErrorMessage() const {
if (Type==Error) return m_string;
return "";
}
std::string Event::DenyMessage() const {
if (Type==ConnectDenied || Type==NameDenied || Type==ChannelDenied || Type==ChannelLeaveDenied) return m_string;
return "";
}
std::string Event::WelcomeMessage() const {
if (Type==Connected) return m_string;
return "";
}
std::string Event::DisconnectAddress() const {
if (Type==Disconnected) return m_string;
return "";
}
uint16_t Event::ChannelsCount() const {
if (Type==ListReceived) return m_short1;
return 0;
}
std::string Event::ChannelName() const {
if (Type==ChannelJoin || Type==ChannelLeave || Type==ListEntry) return m_string;
return "";
}
uint16_t Event::ChannelID() const {
return m_short2;
}
uint16_t Event::PeersCount() const {
if (Type==ListEntry) return m_short3;
return 0;
}
std::string Event::PeerName() const {
if (Type==PeerJoined || Type==PeerLeft || Type==PeerChangedName) return m_string;
return "";
}
uint16_t Event::PeerID() const {
return m_short1;
}
bool Event::PeerWasMaster() const {
if (Type != PeerLeft) return false;
return m_short3 == 1;
}
const char* Event::Address() const {
return m_string.c_str();
}
uint32_t Event::Size() const {
return m_string.length();
}
uint8_t Event::Subchannel() const {
if (Type>=ChannelBlast) return m_short3&255;
return 0;
}
uint8_t Event::Variant() const {
if (Type>=ChannelBlast) return (m_short3>>8)&255;
return 0;
}
uint8_t Event::UByte(uint32_t Index) const {
if (m_string.length()<Index+1) return 0;
return (uint8_t)m_string[Index];
}
int8_t Event::Byte(uint32_t Index) const {
return (int8_t)UByte(Index);
}
uint16_t Event::UShort(uint32_t Index) const {
if (m_string.length()<Index+2) return 0;
return (uint8_t)m_string[Index]|(uint8_t)m_string[Index+1]<<8;
}
int16_t Event::Short(uint32_t Index) const {
return (int16_t)UShort(Index);
}
uint32_t Event::UInt(uint32_t Index) const {
if (m_string.length()<Index+4) return 0;
return (uint8_t)m_string[Index]|(uint8_t)m_string[Index+1]<<8|(uint8_t)m_string[Index+2]<<16|(uint8_t)m_string[Index+3]<<24;
}
int32_t Event::Int(uint32_t Index) const {
return (int32_t)UInt(Index);
}
uint64_t Event::ULong(uint32_t Index) const {
if (m_string.length()<Index+8) return 0;
return (uint64_t)UInt(Index) | (uint64_t)UInt(Index+4)<<32;
}
int64_t Event::Long(uint32_t Index) const {
return (int64_t)ULong(Index);
}
float Event::Float(uint32_t Index) const {
uint32_t tmp = UInt(Index);
float output;
memcpy(&output, &tmp, 4);
return output;
}
double Event::Double(uint32_t Index) const {
uint64_t tmp = ULong(Index);
double output;
memcpy(&output, &tmp, 8);
return output;
}
std::string Event::String(uint32_t Index) const {
if (m_string.length()<Index+1) return "";
return std::string(&m_string[Index]);
}
std::string Event::String(uint32_t Index, uint32_t Size) const {
if (m_string.length()<Index+Size) return "";
return std::string(&m_string[Index], Size);
}
}