summaryrefslogtreecommitdiff
path: root/src/net/vomit.hpp
blob: 84c54acd8e335c493b9a430be34d563340f0ad9a (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#ifndef TMWA_NET_VOMIT_HPP
#define TMWA_NET_VOMIT_HPP
//    vomit.hpp - sickening socket accessors
//
//    Copyright © ????-2004 Athena Dev Teams
//    Copyright © 2004-2011 The Mana World Development Team
//    Copyright © 2011-2014 Ben Longbons <b.r.longbons@gmail.com>
//
//    This file is part of The Mana World (Athena server)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

# include "fwd.hpp"

# include "socket.hpp"

// these first three are really in socket.cpp
// but here for cleanliness

/// Check how much can be read
inline
size_t RFIFOREST(Session *s)
{
    return s->rdata_size - s->rdata_pos;
}

/// Done reading
void RFIFOSKIP(Session *s, size_t len);

/// Finish writing
void WFIFOSET(Session *s, size_t len);


template<class T>
uint8_t *pod_addressof_m(T& structure)
{
    static_assert(is_trivially_copyable<T>::value, "Can only byte-copy POD-ish structs");
    return &reinterpret_cast<uint8_t&>(structure);
}

template<class T>
const uint8_t *pod_addressof_c(const T& structure)
{
    static_assert(is_trivially_copyable<T>::value, "Can only byte-copy POD-ish structs");
    return &reinterpret_cast<const uint8_t&>(structure);
}

/// Read from the queue
inline
const void *RFIFOP(Session *s, size_t pos)
{
    return &s->rdata[s->rdata_pos + pos];
}
inline
uint8_t RFIFOB(Session *s, size_t pos)
{
    return *static_cast<const uint8_t *>(RFIFOP(s, pos));
}
inline
uint16_t RFIFOW(Session *s, size_t pos)
{
    return *static_cast<const uint16_t *>(RFIFOP(s, pos));
}
inline
uint32_t RFIFOL(Session *s, size_t pos)
{
    return *static_cast<const uint32_t *>(RFIFOP(s, pos));
}
template<class T>
void RFIFO_STRUCT(Session *s, size_t pos, T& structure)
{
    really_memcpy(pod_addressof_m(structure), static_cast<const uint8_t *>(RFIFOP(s, pos)), sizeof(T));
}
inline
IP4Address RFIFOIP(Session *s, size_t pos)
{
    IP4Address o;
    RFIFO_STRUCT(s, pos, o);
    return o;
}
template<uint8_t len>
inline
VString<len-1> RFIFO_STRING(Session *s, size_t pos)
{
    const char *const begin = static_cast<const char *>(RFIFOP(s, pos));
    const char *const end = begin + len-1;
    const char *const mid = std::find(begin, end, '\0');
    return XString(begin, mid, nullptr);
}
inline
AString RFIFO_STRING(Session *s, size_t pos, size_t len)
{
    const char *const begin = static_cast<const char *>(RFIFOP(s, pos));
    const char *const end = begin + len;
    const char *const mid = std::find(begin, end, '\0');
    return XString(begin, mid, nullptr);
}
inline
void RFIFO_BUF_CLONE(Session *s, uint8_t *buf, size_t len)
{
    really_memcpy(buf, static_cast<const uint8_t *>(RFIFOP(s, 0)), len);
}

/// Read from an arbitrary buffer
inline
const void *RBUFP(const uint8_t *p, size_t pos)
{
    return p + pos;
}
inline
uint8_t RBUFB(const uint8_t *p, size_t pos)
{
    return *static_cast<const uint8_t *>(RBUFP(p, pos));
}
inline
uint16_t RBUFW(const uint8_t *p, size_t pos)
{
    return *static_cast<const uint16_t *>(RBUFP(p, pos));
}
inline
uint32_t RBUFL(const uint8_t *p, size_t pos)
{
    return *static_cast<const uint32_t *>(RBUFP(p, pos));
}
template<class T>
void RBUF_STRUCT(const uint8_t *p, size_t pos, T& structure)
{
    really_memcpy(pod_addressof_m(structure), p + pos, sizeof(T));
}
inline
IP4Address RBUFIP(const uint8_t *p, size_t pos)
{
    IP4Address o;
    RBUF_STRUCT(p, pos, o);
    return o;
}
template<uint8_t len>
inline
VString<len-1> RBUF_STRING(const uint8_t *p, size_t pos)
{
    const char *const begin = static_cast<const char *>(RBUFP(p, pos));
    const char *const end = begin + len-1;
    const char *const mid = std::find(begin, end, '\0');
    return XString(begin, mid, nullptr);
}
inline
AString RBUF_STRING(const uint8_t *p, size_t pos, size_t len)
{
    const char *const begin = static_cast<const char *>(RBUFP(p, pos));
    const char *const end = begin + len;
    const char *const mid = std::find(begin, end, '\0');
    return XString(begin, mid, nullptr);
}


/// Unused - check how much data can be written
// the existence of this seems scary
inline
size_t WFIFOSPACE(Session *s)
{
    return s->max_wdata - s->wdata_size;
}
/// Write to the queue
inline
void *WFIFOP(Session *s, size_t pos)
{
    return &s->wdata[s->wdata_size + pos];
}
inline
uint8_t& WFIFOB(Session *s, size_t pos)
{
    return *static_cast<uint8_t *>(WFIFOP(s, pos));
}
inline
uint16_t& WFIFOW(Session *s, size_t pos)
{
    return *static_cast<uint16_t *>(WFIFOP(s, pos));
}
inline
uint32_t& WFIFOL(Session *s, size_t pos)
{
    return *static_cast<uint32_t *>(WFIFOP(s, pos));
}
template<class T>
void WFIFO_STRUCT(Session *s, size_t pos, T& structure)
{
    really_memcpy(static_cast<uint8_t *>(WFIFOP(s, pos)), pod_addressof_c(structure), sizeof(T));
}
inline
IP4Address& WFIFOIP(Session *s, size_t pos)
{
    static_assert(is_trivially_copyable<IP4Address>::value, "That was the whole point");
    return *static_cast<IP4Address *>(WFIFOP(s, pos));
}
inline
void WFIFO_STRING(Session *s, size_t pos, XString str, size_t len)
{
    char *const begin = static_cast<char *>(WFIFOP(s, pos));
    char *const end = begin + len;
    char *const mid = std::copy(str.begin(), str.end(), begin);
    std::fill(mid, end, '\0');
}
inline
void WFIFO_ZERO(Session *s, size_t pos, size_t len)
{
    uint8_t *b = static_cast<uint8_t *>(WFIFOP(s, pos));
    uint8_t *e = b + len;
    std::fill(b, e, '\0');
}
inline
void WFIFO_BUF_CLONE(Session *s, const uint8_t *buf, size_t len)
{
    really_memcpy(static_cast<uint8_t *>(WFIFOP(s, 0)), buf, len);
}

/// Write to an arbitrary buffer
inline
void *WBUFP(uint8_t *p, size_t pos)
{
    return p + pos;
}
inline
uint8_t& WBUFB(uint8_t *p, size_t pos)
{
    return *static_cast<uint8_t *>(WBUFP(p, pos));
}
inline
uint16_t& WBUFW(uint8_t *p, size_t pos)
{
    return *static_cast<uint16_t *>(WBUFP(p, pos));
}
inline
uint32_t& WBUFL(uint8_t *p, size_t pos)
{
    return *static_cast<uint32_t *>(WBUFP(p, pos));
}
template<class T>
void WBUF_STRUCT(uint8_t *p, size_t pos, T& structure)
{
    really_memcpy(p + pos, pod_addressof_c(structure), sizeof(T));
}
inline
IP4Address& WBUFIP(uint8_t *p, size_t pos)
{
    return *static_cast<IP4Address *>(WBUFP(p, pos));
}
inline
void WBUF_STRING(uint8_t *p, size_t pos, XString s, size_t len)
{
    char *const begin = static_cast<char *>(WBUFP(p, pos));
    char *const end = begin + len;
    char *const mid = std::copy(s.begin(), s.end(), begin);
    std::fill(mid, end, '\0');
}
inline
void WBUF_ZERO(uint8_t *p, size_t pos, size_t len)
{
    uint8_t *b = static_cast<uint8_t *>(WBUFP(p, pos));
    uint8_t *e = b + len;
    std::fill(b, e, '\0');
}

inline
void RFIFO_WFIFO_CLONE(Session *rs, Session *ws, size_t len)
{
    really_memcpy(static_cast<uint8_t *>(WFIFOP(ws, 0)),
            static_cast<const uint8_t *>(RFIFOP(rs, 0)), len);
}

#endif // TMWA_NET_VOMIT_HPP