summaryrefslogtreecommitdiffstats
path: root/src/network/packet.cpp
blob: 7e1a812f340aee486c4d47fe0d1c0b88167fdd5b (plain) (blame)
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
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#ifdef _WIN32
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif
#include <cstring>
#include <string>
#include "network/packet.h"

namespace Network {

#ifndef htonll
u64 htonll(u64 x) {
    return ((1 == htonl(1)) ? (x) : ((uint64_t)htonl((x)&0xFFFFFFFF) << 32) | htonl((x) >> 32));
}
#endif

#ifndef ntohll
u64 ntohll(u64 x) {
    return ((1 == ntohl(1)) ? (x) : ((uint64_t)ntohl((x)&0xFFFFFFFF) << 32) | ntohl((x) >> 32));
}
#endif

void Packet::Append(const void* in_data, std::size_t size_in_bytes) {
    if (in_data && (size_in_bytes > 0)) {
        std::size_t start = data.size();
        data.resize(start + size_in_bytes);
        std::memcpy(&data[start], in_data, size_in_bytes);
    }
}

void Packet::Read(void* out_data, std::size_t size_in_bytes) {
    if (out_data && CheckSize(size_in_bytes)) {
        std::memcpy(out_data, &data[read_pos], size_in_bytes);
        read_pos += size_in_bytes;
    }
}

void Packet::Clear() {
    data.clear();
    read_pos = 0;
    is_valid = true;
}

const void* Packet::GetData() const {
    return !data.empty() ? &data[0] : nullptr;
}

void Packet::IgnoreBytes(u32 length) {
    read_pos += length;
}

std::size_t Packet::GetDataSize() const {
    return data.size();
}

bool Packet::EndOfPacket() const {
    return read_pos >= data.size();
}

Packet::operator bool() const {
    return is_valid ? &Packet::CheckSize : nullptr;
}

Packet& Packet::operator>>(bool& out_data) {
    u8 value;
    if (*this >> value) {
        out_data = (value != 0);
    }
    return *this;
}

Packet& Packet::operator>>(s8& out_data) {
    Read(&out_data, sizeof(out_data));
    return *this;
}

Packet& Packet::operator>>(u8& out_data) {
    Read(&out_data, sizeof(out_data));
    return *this;
}

Packet& Packet::operator>>(s16& out_data) {
    s16 value;
    Read(&value, sizeof(value));
    out_data = ntohs(value);
    return *this;
}

Packet& Packet::operator>>(u16& out_data) {
    u16 value;
    Read(&value, sizeof(value));
    out_data = ntohs(value);
    return *this;
}

Packet& Packet::operator>>(s32& out_data) {
    s32 value;
    Read(&value, sizeof(value));
    out_data = ntohl(value);
    return *this;
}

Packet& Packet::operator>>(u32& out_data) {
    u32 value;
    Read(&value, sizeof(value));
    out_data = ntohl(value);
    return *this;
}

Packet& Packet::operator>>(s64& out_data) {
    s64 value;
    Read(&value, sizeof(value));
    out_data = ntohll(value);
    return *this;
}

Packet& Packet::operator>>(u64& out_data) {
    u64 value;
    Read(&value, sizeof(value));
    out_data = ntohll(value);
    return *this;
}

Packet& Packet::operator>>(float& out_data) {
    Read(&out_data, sizeof(out_data));
    return *this;
}

Packet& Packet::operator>>(double& out_data) {
    Read(&out_data, sizeof(out_data));
    return *this;
}

Packet& Packet::operator>>(char* out_data) {
    // First extract string length
    u32 length = 0;
    *this >> length;

    if ((length > 0) && CheckSize(length)) {
        // Then extract characters
        std::memcpy(out_data, &data[read_pos], length);
        out_data[length] = '\0';

        // Update reading position
        read_pos += length;
    }

    return *this;
}

Packet& Packet::operator>>(std::string& out_data) {
    // First extract string length
    u32 length = 0;
    *this >> length;

    out_data.clear();
    if ((length > 0) && CheckSize(length)) {
        // Then extract characters
        out_data.assign(&data[read_pos], length);

        // Update reading position
        read_pos += length;
    }

    return *this;
}

Packet& Packet::operator<<(bool in_data) {
    *this << static_cast<u8>(in_data);
    return *this;
}

Packet& Packet::operator<<(s8 in_data) {
    Append(&in_data, sizeof(in_data));
    return *this;
}

Packet& Packet::operator<<(u8 in_data) {
    Append(&in_data, sizeof(in_data));
    return *this;
}

Packet& Packet::operator<<(s16 in_data) {
    s16 toWrite = htons(in_data);
    Append(&toWrite, sizeof(toWrite));
    return *this;
}

Packet& Packet::operator<<(u16 in_data) {
    u16 toWrite = htons(in_data);
    Append(&toWrite, sizeof(toWrite));
    return *this;
}

Packet& Packet::operator<<(s32 in_data) {
    s32 toWrite = htonl(in_data);
    Append(&toWrite, sizeof(toWrite));
    return *this;
}

Packet& Packet::operator<<(u32 in_data) {
    u32 toWrite = htonl(in_data);
    Append(&toWrite, sizeof(toWrite));
    return *this;
}

Packet& Packet::operator<<(s64 in_data) {
    s64 toWrite = htonll(in_data);
    Append(&toWrite, sizeof(toWrite));
    return *this;
}

Packet& Packet::operator<<(u64 in_data) {
    u64 toWrite = htonll(in_data);
    Append(&toWrite, sizeof(toWrite));
    return *this;
}

Packet& Packet::operator<<(float in_data) {
    Append(&in_data, sizeof(in_data));
    return *this;
}

Packet& Packet::operator<<(double in_data) {
    Append(&in_data, sizeof(in_data));
    return *this;
}

Packet& Packet::operator<<(const char* in_data) {
    // First insert string length
    u32 length = static_cast<u32>(std::strlen(in_data));
    *this << length;

    // Then insert characters
    Append(in_data, length * sizeof(char));

    return *this;
}

Packet& Packet::operator<<(const std::string& in_data) {
    // First insert string length
    u32 length = static_cast<u32>(in_data.size());
    *this << length;

    // Then insert characters
    if (length > 0)
        Append(in_data.c_str(), length * sizeof(std::string::value_type));

    return *this;
}

bool Packet::CheckSize(std::size_t size) {
    is_valid = is_valid && (read_pos + size <= data.size());

    return is_valid;
}

} // namespace Network