Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NetGram.h
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.h
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Datagram (UDP) packet that implements the basic
13  packet-oriented network protocol.
14 */
15 
16 
17 #ifndef NetGram_h
18 #define NetGram_h
19 
20 #include <windows.h>
21 #include "NetAddr.h"
22 #include "Text.h"
23 
24 // +-------------------------------------------------------------------+
25 
26 const int NET_GRAM_ACK = 0x80000000;
27 const int NET_GRAM_RELIABLE = 0x40000000;
28 const int NET_GRAM_SEQ_MASK = 0x3fffffff;
29 
30 const int NET_GRAM_HEADER_SIZE = 4;
31 const int NET_GRAM_MAX_SIZE = 1024;
32 
33 // +-------------------------------------------------------------------+
34 
35 class NetGram
36 {
37 public:
38  static const char* TYPENAME() { return "NetGram"; }
39 
40  // for receiving packets from remote hosts:
41  NetGram(const NetAddr& src, Text msg);
42 
43  // for composing packets to send to remote hosts:
44  NetGram(const NetAddr& dst, Text user_data, int retries);
45 
46  int operator == (const NetGram& g) const { return packet_id == g.packet_id &&
47  addr == g.addr; }
48  int operator < (const NetGram& g) const { return Sequence() < g.Sequence(); }
49 
50  DWORD PacketID() const { return packet_id; }
51  DWORD Sequence() const { return packet_id & NET_GRAM_SEQ_MASK; }
52  DWORD SendTime() const { return send_time; }
53  BYTE* Data() const { return (BYTE*) body.data(); }
54  BYTE* UserData() const { return (BYTE*) body.data() + NET_GRAM_HEADER_SIZE; }
55  int Size() const { return body.length(); }
56  const Text& Body() const { return body; }
57  const NetAddr& Address() const { return addr; }
58 
59  bool IsAck() const { return packet_id & NET_GRAM_ACK ? true : false; }
60  bool IsReliable() const { return packet_id & NET_GRAM_RELIABLE ? true : false; }
61  int Retries() const { return retries; }
62 
63  void Retry();
64  NetGram Ack();
66 
67 protected:
68  NetGram();
69 
70  NetAddr addr; // network address of remote host
71  int retries; // number of retries remaining (reliable packets only)
72  DWORD send_time; // time in msec of most recent send attempt
73 
74  DWORD packet_id; // copy of packet id from header in body
75  Text body; // header plus user data
76 };
77 
78 
79 #endif NetGram_h