summaryrefslogtreecommitdiff
path: root/src/net/packets.cpp
blob: 5595b1d9bc0a3f6bba230ef7ece067eceeb12f78 (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
#include "packets.hpp"
//    packets.cpp - palatable socket buffer accessors
//
//    Copyright © 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 "../io/cxxstdio.hpp"
# include "../io/write.hpp"

#include "vomit.hpp"

#include "../poison.hpp"

size_t packet_avail(Session *s)
{
    return RFIFOREST(s);
}

bool packet_fetch(Session *s, size_t offset, Byte *data, size_t sz)
{
    if (RFIFOREST(s) < offset + sz)
        return false;
    const Byte *start = reinterpret_cast<const Byte *>(RFIFOP(s, offset));
    const Byte *end = start + sz;
    std::copy(start, end, data);
    return true;
}
void packet_discard(Session *s, size_t sz)
{
    RFIFOSKIP(s, sz);
}
bool packet_send(Session *s, const Byte *data, size_t sz)
{
    WFIFOSET(s, sz);
    Byte *end = reinterpret_cast<Byte *>(WFIFOP(s, 0));
    Byte *start = end - sz;
    std::copy(data, data + sz, start);
    return true;
}

void packet_dump(io::WriteFile& logfp, Session *s)
{
    FPRINTF(logfp,
            "---- 00-01-02-03-04-05-06-07  08-09-0A-0B-0C-0D-0E-0F\n"_fmt);
    char tmpstr[16 + 1] {};
    int i;
    for (i = 0; i < RFIFOREST(s); i++)
    {
        if ((i & 15) == 0)
            FPRINTF(logfp, "%04X "_fmt, i);
        FPRINTF(logfp, "%02x "_fmt, RFIFOB(s, i));
        if (RFIFOB(s, i) > 0x1f)
            tmpstr[i % 16] = RFIFOB(s, i);
        else
            tmpstr[i % 16] = '.';
        if ((i - 7) % 16 == 0)  // -8 + 1
            FPRINTF(logfp, " "_fmt);
        else if ((i + 1) % 16 == 0)
        {
            FPRINTF(logfp, " %s\n"_fmt, tmpstr);
            std::fill(tmpstr + 0, tmpstr + 17, '\0');
        }
    }
    if (i % 16 != 0)
    {
        for (int j = i; j % 16 != 0; j++)
        {
            FPRINTF(logfp, "   "_fmt);
            if ((j - 7) % 16 == 0)  // -8 + 1
                FPRINTF(logfp, " "_fmt);
        }
        FPRINTF(logfp, " %s\n"_fmt, tmpstr);
    }
    FPRINTF(logfp, "\n"_fmt);
}