File: ArNetPacket.h

package info (click to toggle)
libaria 2.8.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,628 kB
  • ctags: 16,574
  • sloc: cpp: 135,490; makefile: 925; python: 597; java: 570; ansic: 182
file content (84 lines) | stat: -rw-r--r-- 2,714 bytes parent folder | download | duplicates (2)
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
#ifndef NLNETPACKET_H
#define NLNETPACKET_H

#include "Aria.h"

/// our packet for the network stuff
class ArNetPacket : public ArBasePacket
{
public:
  /// Constructor
  AREXPORT ArNetPacket(
	  ArTypes::UByte2 bufferSize = ArNetPacket::MAX_LENGTH + 5);

  /// Copy constructor
  AREXPORT ArNetPacket(const ArNetPacket &other);

  /// Assignment operator
  AREXPORT ArNetPacket &operator=(const ArNetPacket &other);

  /// Destructor
  AREXPORT virtual ~ArNetPacket();


  /// Sets the command this packet is
  AREXPORT void setCommand(ArTypes::UByte2 command);
  /// Gets the command this packet is
  AREXPORT ArTypes::UByte2 getCommand(void);
  enum { 
    SIZE_OF_LENGTH = 2,  ///< Number of bytes needed to store packet length value
    MAX_LENGTH = 32000,  ///< Suggested maximum total size of a packet (bytes)
    HEADER_LENGTH = 6,   ///< Bytes of packet data used for header
    FOOTER_LENGTH = 2,   ///< Byset of packet data used for footer 

    /** Suggested maximum size for data payload (this is the total suggested
        packet size minus headers and footers)
    */
    MAX_DATA_LENGTH = MAX_LENGTH - HEADER_LENGTH - FOOTER_LENGTH - SIZE_OF_LENGTH
  };
  
  /// Puts a double into the packet buffer
  AREXPORT virtual void doubleToBuf(double val);
  /// Gets a double from the packet buffer
  AREXPORT virtual double bufToDouble(void);
  AREXPORT virtual void empty(void);
  AREXPORT virtual void finalizePacket(void);
  AREXPORT virtual void resetRead(void);
  AREXPORT virtual void duplicatePacket(ArNetPacket *packet);
  
  /// returns true if the checksum matches what it should be
  AREXPORT bool verifyCheckSum(void);
  /// returns the checksum, probably used only internally
  AREXPORT ArTypes::Byte2 calcCheckSum(void);

  /// Iternal function that sets if we already added the footer(for forwarding)
  bool getAddedFooter(void) { return myAddedFooter; }
  /// Iternal function that sets if we already added the footer(for forwarding)
  void setAddedFooter(bool addedFooter) { myAddedFooter = addedFooter; }

  /// an enum for where the packet came from
  enum PacketSource
  {
    TCP, ///< Came in over tcp
    UDP ///< Came in over udp
  };
  PacketSource getPacketSource(void) { return myPacketSource; }
  void setPacketSource(PacketSource source) { myPacketSource = source; }
  void setArbitraryString(const char *string) { myArbitraryString = string; }
  const char *getArbitraryString(void) { return myArbitraryString.c_str(); }

private:

  /// Inserts the header information (command, length, etc.) into the buffer.
  void insertHeader();


protected:
  PacketSource myPacketSource;
  bool myAddedFooter;
  std::string myArbitraryString;
  ArTypes::UByte2 myCommand;
};


#endif