summaryrefslogtreecommitdiffhomepage
path: root/NetEx/NetGram.cpp
blob: a2dc34641504a628287fbf7c84c20d2f26a76e1c (plain)
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
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

     AUTHOR:       John DiCamillo


     OVERVIEW
     ========
     Generic Network Packet (Datagram) Implementation
*/


#include "MemDebug.h"
#include "NetGram.h"
#include "NetLayer.h"

// +-------------------------------------------------------------------+

static DWORD net_gram_sequence = 1;

// +-------------------------------------------------------------------+

/**
 * NetGram constructor for ACK packets
 */
NetGram::NetGram()
    : retries(0), packet_id(0), send_time(0)
{ }

/**
 * NetGram constructor for receiving packets from remote hosts
 */
NetGram::NetGram(const NetAddr& src, Text msg)
    : addr(src), retries(0), send_time(0)
{
    body = msg;

    if (body.length() >= NET_GRAM_HEADER_SIZE) {
        BYTE* data = (BYTE*) body.data();

        packet_id = (((DWORD) data[0]) << 24) +
                        (((DWORD) data[1]) << 16) +
                        (((DWORD) data[2]) <<  8) +
                         ((DWORD) data[3]);
    }
}

/**
 * NetGram constructor for composing packets to send to remote hosts
 */
NetGram::NetGram(const NetAddr& dst, Text user_data, int r)
    : addr(dst), retries(r)
{
    send_time = NetLayer::GetTime();
    packet_id = net_gram_sequence++;

    if (retries)
        packet_id |= NET_GRAM_RELIABLE;

    static BYTE buf[NET_GRAM_MAX_SIZE];
    buf[0] = (BYTE) (packet_id >> 24) & 0xff;
    buf[1] = (BYTE) (packet_id >> 16) & 0xff;
    buf[2] = (BYTE) (packet_id >>  8) & 0xff;
    buf[3] = (BYTE) (packet_id)       & 0xff;

    int len = user_data.length();
    if (len >= NET_GRAM_MAX_SIZE - NET_GRAM_HEADER_SIZE)
        len = NET_GRAM_MAX_SIZE - NET_GRAM_HEADER_SIZE - 1;

    CopyMemory(buf+NET_GRAM_HEADER_SIZE, user_data.data(), len);

    body = Text((char*) buf, len+NET_GRAM_HEADER_SIZE);
}

// +--------------------------------------------------------------------+

void
NetGram::Retry()
{
    if (retries > 0) {
        retries--;
        send_time = NetLayer::GetTime();
    }
}

// +--------------------------------------------------------------------+

NetGram
NetGram::Ack()
{
    NetGram ack;

    ack.packet_id  = packet_id | NET_GRAM_ACK;
    ack.send_time  = NetLayer::GetTime();

    static BYTE buf[NET_GRAM_HEADER_SIZE];
    buf[0] = (BYTE) (ack.packet_id >> 24) & 0xff;
    buf[1] = (BYTE) (ack.packet_id >> 16) & 0xff;
    buf[2] = (BYTE) (ack.packet_id >>  8) & 0xff;
    buf[3] = (BYTE) (ack.packet_id)       & 0xff;

    ack.body = Text((char*) buf, NET_GRAM_HEADER_SIZE);

    return ack;
}