Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NetGram.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: NetEx.lib
6  FILE: NetGram.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Generic Network Packet (Datagram) Implementation
13 */
14 
15 
16 #include "MemDebug.h"
17 #include "NetGram.h"
18 #include "NetLayer.h"
19 
20 // +-------------------------------------------------------------------+
21 
22 static DWORD net_gram_sequence = 1;
23 
24 // +-------------------------------------------------------------------+
25 
30  : retries(0), packet_id(0), send_time(0)
31 { }
32 
36 NetGram::NetGram(const NetAddr& src, Text msg)
37  : addr(src), retries(0), send_time(0)
38 {
39  body = msg;
40 
41  if (body.length() >= NET_GRAM_HEADER_SIZE) {
42  BYTE* data = (BYTE*) body.data();
43 
44  packet_id = (((DWORD) data[0]) << 24) +
45  (((DWORD) data[1]) << 16) +
46  (((DWORD) data[2]) << 8) +
47  ((DWORD) data[3]);
48  }
49 }
50 
54 NetGram::NetGram(const NetAddr& dst, Text user_data, int r)
55  : addr(dst), retries(r)
56 {
58  packet_id = net_gram_sequence++;
59 
60  if (retries)
62 
63  static BYTE buf[NET_GRAM_MAX_SIZE];
64  buf[0] = (BYTE) (packet_id >> 24) & 0xff;
65  buf[1] = (BYTE) (packet_id >> 16) & 0xff;
66  buf[2] = (BYTE) (packet_id >> 8) & 0xff;
67  buf[3] = (BYTE) (packet_id) & 0xff;
68 
69  int len = user_data.length();
72 
73  CopyMemory(buf+NET_GRAM_HEADER_SIZE, user_data.data(), len);
74 
75  body = Text((char*) buf, len+NET_GRAM_HEADER_SIZE);
76 }
77 
78 // +--------------------------------------------------------------------+
79 
80 void
82 {
83  if (retries > 0) {
84  retries--;
86  }
87 }
88 
89 // +--------------------------------------------------------------------+
90 
91 NetGram
93 {
94  NetGram ack;
95 
98 
99  static BYTE buf[NET_GRAM_HEADER_SIZE];
100  buf[0] = (BYTE) (ack.packet_id >> 24) & 0xff;
101  buf[1] = (BYTE) (ack.packet_id >> 16) & 0xff;
102  buf[2] = (BYTE) (ack.packet_id >> 8) & 0xff;
103  buf[3] = (BYTE) (ack.packet_id) & 0xff;
104 
105  ack.body = Text((char*) buf, NET_GRAM_HEADER_SIZE);
106 
107  return ack;
108 }
109 
110 
111