Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Packet Class Reference

#include <packet.h>

Public Member Functions

 Packet ()
 
 Packet (const void *buffer, size_t sz)
 
 Packet (PacketMem &buffer)
 
 Packet (const Packet &a)
 
 ~Packet ()
 
Packetoperator= (const Packet &a)
 
void copyfrom (const Packet &a)
 
bool operator== (const Packet &p) const
 
int send (Cmd cmd, ObjSerial nserial, const char *buf, unsigned int length, int flags, const AddressIP *dst, const SOCKETALT &sock, const char *caller_file, int caller_line)
 flags is a bitwise OR of PCKTFLAGS More...
 
void bc_create (Cmd cmd, ObjSerial nserial, const char *buf, unsigned int length, int flags, const char *caller_file, int caller_line)
 flags is a bitwise OR of PCKTFLAGS More...
 
int bc_send (const AddressIP &dst, const SOCKETALT &sock)
 
void display (const char *file, int line)
 
void displayHex ()
 
unsigned int getDataLength () const
 
ObjSerial getSerial () const
 
unsigned int getTimestamp () const
 
Cmd getCommand () const
 
unsigned short getFlags () const
 
void setFlag (enum PCKTFLAGS fl)
 
void setFlags (unsigned short fl)
 
const char * getData () const
 
const char * getSendBuffer () const
 
int getSendBufferLength () const
 
void reset ()
 

Static Public Member Functions

static unsigned short getHeaderLength ()
 

Detailed Description

Definition at line 20 of file packet.h.

Constructor & Destructor Documentation

Packet::Packet ( )

Definition at line 33 of file packet.cpp.

References MAKE_VALID, and NONE.

34 {
36 
37  h.command = 0;
38  h.serial = 0;
39  h.timestamp = 0;
40  h.data_length = 0;
41  h.flags = NONE;
42 }
Packet::Packet ( const void *  buffer,
size_t  sz 
)

Definition at line 44 of file packet.cpp.

References __LINE__NOMSC, COMPRESSED, COUT, display(), int, and MAKE_VALID.

45 {
47  if (sz >= header_length) {
48  h.ntoh( buffer );
49 
50  sz -= header_length;
51  if (h.data_length > sz) {
52  COUT<<"Packet not correctly received, not enough data for buffer"<<endl
53  <<" should be still "<<h.data_length
54  <<" but buffer has only "<<sz<<endl;
55  display( __FILE__, __LINE__NOMSC );
56  h.data_length = sz; //Don't want game to crash later on!
57  } else if (h.flags&COMPRESSED) {
58  if (packet_uncompress( _packet,
59  (const unsigned char*) buffer,
60  h.data_length,
61  h ) == false)
62  display( __FILE__, __LINE__NOMSC );
63  } else {
64  PacketMem mem( buffer, sz );
65  _packet = mem;
66 
67 #ifdef VSNET_DEBUG
68  COUT<<"Parsed a packet with"
69  <<" cmd="<<Cmd( h.command )<<"("<<(int) h.command<<")"
70  <<" ser="<<h.serial
71  <<" ts="<<h.timestamp
72  <<" len="<<h.data_length
73  <<endl;
74 #endif
75  }
76  } else {
77  COUT<<"Packet not correctly received, not enough data for header"<<endl;
78  }
79 }
Packet::Packet ( PacketMem buffer)

Definition at line 81 of file packet.cpp.

References __LINE__NOMSC, buffer, COMPRESSED, COUT, display(), PacketMem::getConstBuf(), int, PacketMem::len(), and MAKE_VALID.

82 {
84  if (buffer.len() >= header_length) {
85  h.ntoh( buffer.getConstBuf() );
86  size_t sz = buffer.len();
87  sz -= header_length;
88  if (h.data_length > sz) {
89  COUT<<"Packet not correctly received, not enough data for buffer"<<endl
90  <<" should be still "<<h.data_length
91  <<" but buffer has only "<<sz<<endl;
92  display( __FILE__, __LINE__NOMSC );
93  h.data_length = sz; //Don't want game to crash later on!
94  } else if (h.flags&COMPRESSED) {
95 #ifdef HAVE_ZLIB_H
96  if (packet_uncompress( _packet,
97  (const unsigned char*) buffer.getConstBuf(),
98  h.data_length,
99  h ) == false)
100  display( __FILE__, __LINE__NOMSC );
101 #else /* HAVE_ZLIB_H */
102  COUT<<"Received compressed packet, but compiled without zlib"<<endl;
103  display( __FILE__, __LINE__NOMSC );
104 #endif /* HAVE_ZLIB_H */
105  } else {
106  _packet = buffer;
107 
108 #ifdef VSNET_DEBUG
109  COUT<<"Parsed a packet with"
110  <<" cmd="<<Cmd( h.command )<<"("<<(int) h.command<<")"
111  <<" ser="<<h.serial
112  <<" ts="<<h.timestamp
113  <<" len="<<h.data_length
114  <<endl;
115 #endif
116  }
117  } else {
118  COUT<<"Packet not correctly received, not enough data for header"<<endl;
119  }
120 }
Packet::Packet ( const Packet a)

Definition at line 122 of file packet.cpp.

References copyfrom(), and MAKE_VALID.

123 {
124  MAKE_VALID
125 
126  copyfrom( a );
127 }
Packet::~Packet ( )

Definition at line 129 of file packet.cpp.

References CHECK_VALID, and MAKE_INVALID.

130 {
133 }

Member Function Documentation

void Packet::bc_create ( Cmd  cmd,
ObjSerial  nserial,
const char *  buf,
unsigned int  length,
int  flags,
const char *  caller_file,
int  caller_line 
)
inline

flags is a bitwise OR of PCKTFLAGS

Definition at line 76 of file packet.h.

Referenced by NetServer::broadcast(), NetServer::BroadcastCargoUpgrade(), NetServer::BroadcastFire(), NetServer::BroadcastUnfire(), NetServer::sendDamages(), NetServer::sendDockAuthorize(), NetServer::sendKill(), NetServer::sendMessage(), NetServer::sendNewUnitQueue(), and NetServer::sendUnDock().

83  {
84  create( cmd, nserial, buf, length, flags, caller_file, caller_line );
85  }
int Packet::bc_send ( const AddressIP dst,
const SOCKETALT sock 
)
inline

Definition at line 87 of file packet.h.

References send().

Referenced by ZoneMgr::broadcast(), ZoneMgr::broadcastNoSelf(), ZoneMgr::broadcastSample(), and ZoneMgr::broadcastText().

88  {
89  return send( sock, &dst );
90  }
void Packet::copyfrom ( const Packet a)

Definition at line 135 of file packet.cpp.

References CHECK_VALID.

Referenced by operator=(), Packet(), and VsnetTCPSocket::recvbuf().

136 {
138  h.command = a.h.command;
139  h.serial = a.h.serial;
140  h.timestamp = a.h.timestamp;
141  h.data_length = a.h.data_length;
142  h.flags = a.h.flags;
143  _packet = a._packet;
144 }
void Packet::display ( const char *  file,
int  line 
)

Definition at line 255 of file packet.cpp.

References CHECK_VALID, PacketMem::dump(), and getDataLength().

Referenced by Packet(), and VsnetUDPSocket::sendbuf().

256 {
258  cout<<"*** "<<file<<":"<<line<<" "<<endl;
259  cout<<"*** Packet display -- Command : "<<Cmd( h.command )
260  <<" - Serial : "<<h.serial<<" - Flags : "<<h.flags<<endl;
261  cout<<"*** Size : "<<getDataLength()+header_length<<endl;
262  cout<<"*** Buffer : "<<endl;
263  _packet.dump( cout, 4 );
264 }
void Packet::displayHex ( )

Definition at line 266 of file packet.cpp.

References CHECK_VALID, PacketMem::getConstBuf(), i, and PacketMem::len().

267 {
269  cout<<"Packet : "<<h.command<<" | "<<h.serial<<" | ";
270  const char *c = _packet.getConstBuf();
271  for (size_t i = 0; i < _packet.len(); i++)
272  cout<<c[i]<<" ";
273  cout<<endl;
274 }
Cmd Packet::getCommand ( ) const
inline

Definition at line 110 of file packet.h.

Referenced by ZoneMgr::broadcast(), AccountServer::recvMsg(), VsnetUDPSocket::sendbuf(), and NetClient::synchronizeTime().

111  {
112  return (Cmd) h.command;
113  }
const char * Packet::getData ( ) const
unsigned int Packet::getDataLength ( ) const

Definition at line 337 of file packet.cpp.

References PacketMem::len().

Referenced by NetClient::authenticate(), display(), NetClient::loginAccept(), NetClient::loginAuth(), NetClient::loginChooseShip(), AccountServer::recvMsg(), AccountServer::sendAlreadyConnected(), AccountServer::sendAuthorized(), AccountServer::sendUnauthorized(), and NetClient::synchronizeTime().

338 {
339  if ( h.data_length > _packet.len() )
340  return _packet.len();
341  else
342  return h.data_length;
343 }
unsigned short Packet::getFlags ( ) const
inline

Definition at line 114 of file packet.h.

115  {
116  return h.flags;
117  }
static unsigned short Packet::getHeaderLength ( )
inlinestatic

Definition at line 97 of file packet.h.

Referenced by NetClient::authenticate().

98  {
99  return header_length;
100  }
const char * Packet::getSendBuffer ( ) const

Definition at line 344 of file packet.cpp.

References c, CHECK_VALID, and PacketMem::getConstBuf().

Referenced by VsnetUDPSocket::sendbuf().

345 {
347  const char *c = _packet.getConstBuf();
348  return c;
349 }
int Packet::getSendBufferLength ( ) const

Definition at line 351 of file packet.cpp.

Referenced by VsnetUDPSocket::sendbuf(), and VsnetTCPSocket::sendbuf().

352 {
353  return h.data_length+header_length;
354 }
ObjSerial Packet::getSerial ( ) const
inline

Definition at line 102 of file packet.h.

Referenced by NetClient::loginAccept(), AccountServer::recvMsg(), and AccountServer::writeSave().

103  {
104  return h.serial;
105  }
unsigned int Packet::getTimestamp ( ) const
inline

Definition at line 106 of file packet.h.

107  {
108  return h.timestamp;
109  }
Packet& Packet::operator= ( const Packet a)
inline

Definition at line 49 of file packet.h.

References copyfrom().

50  {
51  copyfrom( a );
52  return *this;
53  }
bool Packet::operator== ( const Packet p) const
inline

Definition at line 57 of file packet.h.

58  {
59  if (_packet == p._packet) return true;
60  cout<<"Packets are different"<<endl;
61  return false;
62  }
void Packet::reset ( )
inline

Definition at line 131 of file packet.h.

132  {
133  h.command = 0;
134  }
void Packet::setFlag ( enum PCKTFLAGS  fl)
inline

Definition at line 118 of file packet.h.

119  {
120  h.flags |= fl;
121  }
void Packet::setFlags ( unsigned short  fl)
inline

Definition at line 122 of file packet.h.

123  {
124  h.flags = fl;
125  }

The documentation for this class was generated from the following files: