ManaPlus
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
Net::MessageOut Class Referenceabstract

#include <messageout.h>

Inheritance diagram for Net::MessageOut:
EAthena::MessageOut TmwAthena::MessageOut

Public Member Functions

virtual void writeInt8 (const int8_t value, const char *const str)
 
void writeInt16 (const int16_t value, const char *const str)
 
void writeInt32 (const int32_t value, const char *const str)
 
void writeItemId (const int32_t value, const char *const str)
 
void writeInt64 (const int64_t value, const char *const str)
 
void writeBeingId (const BeingId value, const char *const str)
 
void writeCoordinates (const uint16_t x, const uint16_t y, unsigned char direction, const char *const str)
 
void writeString (const std::string &string, int length, const char *const str)
 
void writeStringNoLog (const std::string &string, int length, const char *const str)
 
void writeFloat (const float value, const char *const str)
 
const char * getData () const
 
unsigned int getDataSize () const
 
virtual ~MessageOut ()
 
void resetPos ()
 

Static Public Member Functions

static unsigned char toServerDirection (unsigned char direction) A_CONST
 

Protected Member Functions

 MessageOut (const int16_t id)
 
virtual void expand (size_t size) const =0
 

Protected Attributes

char * mData
 
unsigned int mDataSize
 
unsigned int mPos
 
uint16_t mId
 
bool mIgnore
 

Detailed Description

Used for building an outgoing message.

Definition at line 48 of file messageout.h.

Constructor & Destructor Documentation

◆ ~MessageOut()

virtual Net::MessageOut::~MessageOut ( )
inlinevirtual

Reimplemented in TmwAthena::MessageOut, and EAthena::MessageOut.

Definition at line 108 of file messageout.h.

109  { }

◆ MessageOut()

Net::MessageOut::MessageOut ( const int16_t  id)
explicitprotected

Constructor.

Definition at line 49 of file messageout.cpp.

49  :
50  mData(nullptr),
51  mDataSize(0),
52  mPos(0),
53  mId(id),
54  mIgnore(false)
55 {
58  DEBUGLOG2("Send packet", 0, "MessageOut");
59 }
uint16_t mId
Definition: messageout.h:135
unsigned int mDataSize
Definition: messageout.h:133
unsigned int mPos
Definition: messageout.h:134
static void incOutPackets()
#define IGNOREDEBUGLOG
Definition: logger.h:48
#define DEBUGLOG2(str, pos, comment)
Definition: logger.h:42

References DEBUGLOG2, IGNOREDEBUGLOG, and PacketCounters::incOutPackets().

Member Function Documentation

◆ expand()

virtual void Net::MessageOut::expand ( size_t  size) const
protectedpure virtual

Expand the packet data to be able to hold more data.

NOTE: For performance enhancements this method could allocate extra memory in advance instead of expanding size every time more data is added.

Implemented in TmwAthena::MessageOut, and EAthena::MessageOut.

Referenced by writeCoordinates(), writeFloat(), writeInt16(), writeInt32(), writeInt64(), writeInt8(), writeString(), and writeStringNoLog().

◆ getData()

const char * Net::MessageOut::getData ( ) const

Returns the content of the message.

Definition at line 243 of file messageout.cpp.

244 {
245  return mData;
246 }

References mData.

◆ getDataSize()

unsigned int Net::MessageOut::getDataSize ( ) const

Returns the length of the data.

Definition at line 248 of file messageout.cpp.

249 {
250  return mDataSize;
251 }

References mDataSize.

◆ resetPos()

void Net::MessageOut::resetPos ( )
inline

Definition at line 111 of file messageout.h.

112  { mPos = 0; }

References mPos.

◆ toServerDirection()

unsigned char Net::MessageOut::toServerDirection ( unsigned char  direction)
static

Definition at line 253 of file messageout.cpp.

254 {
255  // Translate direction to eAthena format
256  switch (direction)
257  {
258  case 1: // DOWN
259  direction = 0;
260  break;
261  case 3: // DOWN | LEFT
262  direction = 1;
263  break;
264  case 2: // LEFT
265  direction = 2;
266  break;
267  case 6: // LEFT | UP
268  direction = 3;
269  break;
270  case 4: // UP
271  direction = 4;
272  break;
273  case 12: // UP | RIGHT
274  direction = 5;
275  break;
276  case 8: // RIGHT
277  direction = 6;
278  break;
279  case 9: // RIGHT + DOWN
280  direction = 7;
281  break;
282  default:
283  // OOPSIE! Impossible or unknown
284  direction = CAST_U8(-1);
285  break;
286  }
287  return direction;
288 }
#define CAST_U8
Definition: cast.h:27

References CAST_U8.

Referenced by EAthena::PlayerHandler::setDirection(), EAthena::HomunculusHandler::setDirection(), EAthena::MercenaryHandler::setDirection(), EAthena::PetHandler::setDirection(), and writeCoordinates().

◆ writeBeingId()

void Net::MessageOut::writeBeingId ( const BeingId  value,
const char *const  str 
)

Definition at line 129 of file messageout.cpp.

130 {
131  writeInt32(toInt(value, int32_t), str);
132 }
void writeInt32(const int32_t value, const char *const str)
Definition: messageout.cpp:88
#define toInt(val, name)
Definition: intdefines.h:47

References toInt, and writeInt32().

◆ writeCoordinates()

void Net::MessageOut::writeCoordinates ( const uint16_t  x,
const uint16_t  y,
unsigned char  direction,
const char *const  str 
)

Definition at line 148 of file messageout.cpp.

152 {
153  DEBUGLOG2(strprintf("writeCoordinates: %u,%u %u",
154  CAST_U32(x),
155  CAST_U32(y),
156  CAST_U32(direction)), mPos, str);
157  unsigned char *const data = reinterpret_cast<unsigned char*>(mData)
158  + CAST_SIZE(mPos);
159  expand(3);
160  mPos += 3;
161 
162  uint16_t temp = x;
163  temp <<= 6;
164  data[0] = 0;
165  data[1] = 1;
166  data[2] = 2;
167  data[0] = HIBYTE(temp);
168  data[1] = CAST_U8(temp);
169  temp = y;
170  temp <<= 4;
171  data[1] |= HIBYTE(temp);
172  data[2] = LOBYTE(temp);
173  direction = toServerDirection(direction);
174  data[2] |= direction;
175 }
#define CAST_U32
Definition: cast.h:31
#define CAST_SIZE
Definition: cast.h:34
static unsigned char toServerDirection(unsigned char direction) A_CONST
Definition: messageout.cpp:253
virtual void expand(size_t size) const =0
uint32_t data
#define LOBYTE(w)
Definition: messageout.cpp:145
#define HIBYTE(w)
Definition: messageout.cpp:146
std::string strprintf(const char *const format,...)

References CAST_SIZE, CAST_U32, CAST_U8, data, DEBUGLOG2, expand(), HIBYTE, LOBYTE, mData, mPos, strprintf(), toServerDirection(), x, and y.

◆ writeFloat()

void Net::MessageOut::writeFloat ( const float  value,
const char *const  str 
)

Definition at line 134 of file messageout.cpp.

135 {
136 #ifdef ENABLEDEBUGLOG
137  std::string text = strprintf("writeFloat: %f", value);
138  DEBUGLOG2(text, mPos, str);
139 #endif
140  expand(4);
141  memcpy(mData + CAST_SIZE(mPos), &value, sizeof(float));
142  mPos += 4;
143 }

References CAST_SIZE, DEBUGLOG2, expand(), mData, mPos, and strprintf().

◆ writeInt16()

void Net::MessageOut::writeInt16 ( const int16_t  value,
const char *const  str 
)

Writes a long.

Definition at line 71 of file messageout.cpp.

72 {
73  expand(2);
74 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
75  int16_t swap = SDL_Swap16(value);
76  memcpy(mData + CAST_SIZE(mPos), &swap, sizeof(int16_t));
77 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
78 
79  memcpy(mData + CAST_SIZE(mPos), &value, sizeof(int16_t));
80 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
81 
82  DEBUGLOG2("writeInt16: " + toStringPrint(CAST_U32(
83  CAST_U16(value))),
84  mPos, str);
85  mPos += 2;
86 }
#define CAST_U16
Definition: cast.h:29
std::string toStringPrint(const unsigned int val)

References CAST_SIZE, CAST_U16, CAST_U32, DEBUGLOG2, expand(), mData, mPos, and toStringPrint().

Referenced by EAthena::ChatHandler::sendRaw(), TmwAthena::ChatHandler::sendRaw(), writeItemId(), writeString(), and writeStringNoLog().

◆ writeInt32()

void Net::MessageOut::writeInt32 ( const int32_t  value,
const char *const  str 
)

Definition at line 88 of file messageout.cpp.

89 {
90  DEBUGLOG2("writeInt32: " + toStringPrint(CAST_U32(value)),
91  mPos, str);
92  expand(4);
93 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
94  int32_t swap = SDL_Swap32(value);
95  memcpy(mData + CAST_SIZE(mPos), &swap, sizeof(int32_t));
96 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
97 
98  memcpy(mData + CAST_SIZE(mPos), &value, sizeof(int32_t));
99 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
100 
101  mPos += 4;
102 }

References CAST_SIZE, CAST_U32, DEBUGLOG2, expand(), mData, mPos, and toStringPrint().

Referenced by writeBeingId(), and writeItemId().

◆ writeInt64()

void Net::MessageOut::writeInt64 ( const int64_t  value,
const char *const  str 
)

Definition at line 113 of file messageout.cpp.

114 {
115  DEBUGLOG2("writeInt64: " + toStringPrint(CAST_U32(value)),
116  mPos, str);
117  expand(8);
118 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
119  int32_t swap = SDL_Swap64(value);
120  memcpy(mData + CAST_SIZE(mPos), &swap, sizeof(int64_t));
121 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
122 
123  memcpy(mData + CAST_SIZE(mPos), &value, sizeof(int64_t));
124 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
125 
126  mPos += 8;
127 }

References CAST_SIZE, CAST_U32, DEBUGLOG2, expand(), mData, mPos, and toStringPrint().

◆ writeInt8()

void Net::MessageOut::writeInt8 ( const int8_t  value,
const char *const  str 
)
virtual

< Writes a byte. Writes a short.

Definition at line 61 of file messageout.cpp.

62 {
63  expand(1);
64  mData[mPos] = value;
65  DEBUGLOG2("writeInt8: " + toStringPrint(CAST_U32(
66  CAST_U8(value))),
67  mPos, str);
68  mPos += 1;
69 }

References CAST_U32, CAST_U8, DEBUGLOG2, expand(), mData, mPos, and toStringPrint().

◆ writeItemId()

void Net::MessageOut::writeItemId ( const int32_t  value,
const char *const  str 
)

Definition at line 104 of file messageout.cpp.

106 {
107  if (itemIdLen == 2)
108  writeInt16(CAST_S16(value), str);
109  else
110  writeInt32(value, str);
111 }
#define CAST_S16
Definition: cast.h:28
void writeInt16(const int16_t value, const char *const str)
Definition: messageout.cpp:71
int itemIdLen
Definition: client.cpp:130

References CAST_S16, itemIdLen, writeInt16(), and writeInt32().

◆ writeString()

void Net::MessageOut::writeString ( const std::string &  string,
int  length,
const char *const  str 
)

Writes a string. If a fixed length is not given (-1), it is stored as a short at the start of the string.

Definition at line 177 of file messageout.cpp.

180 {
181  int stringLength = CAST_S32(string.length());
182  if (length < 0)
183  {
184  // Write the length at the start if not fixed
185  writeInt16(CAST_S16(stringLength), "len");
186  length = stringLength;
187  }
188  else if (length < stringLength)
189  {
190  // Make sure the length of the string is no longer than specified
191  stringLength = length;
192  }
193  expand(length);
194 
195  // Write the actual string
196  memcpy(mData + CAST_SIZE(mPos), string.c_str(), stringLength);
197 
198  // Pad remaining space with zeros
199  if (length > stringLength)
200  {
201  memset(mData + CAST_SIZE(mPos + stringLength),
202  '\0',
203  length - stringLength);
204  }
205 
206  DEBUGLOG2("writeString: " + string, mPos, str);
207  mPos += length;
208 }
#define CAST_S32
Definition: cast.h:30

References CAST_S16, CAST_S32, CAST_SIZE, DEBUGLOG2, expand(), mData, mPos, and writeInt16().

◆ writeStringNoLog()

void Net::MessageOut::writeStringNoLog ( const std::string &  string,
int  length,
const char *const  str 
)

Writes a string. If a fixed length is not given (-1), it is stored as a short at the start of the string.

Definition at line 210 of file messageout.cpp.

213 {
214  int stringLength = CAST_S32(string.length());
215  if (length < 0)
216  {
217  // Write the length at the start if not fixed
218  writeInt16(CAST_S16(stringLength), "len");
219  length = stringLength;
220  }
221  else if (length < stringLength)
222  {
223  // Make sure the length of the string is no longer than specified
224  stringLength = length;
225  }
226  expand(length);
227 
228  // Write the actual string
229  memcpy(mData + CAST_SIZE(mPos), string.c_str(), stringLength);
230 
231  // Pad remaining space with zeros
232  if (length > stringLength)
233  {
234  memset(mData + CAST_SIZE(mPos + stringLength),
235  '\0',
236  length - stringLength);
237  }
238 
239  DEBUGLOG2("writeString: ***", mPos, str);
240  mPos += length;
241 }

References CAST_S16, CAST_S32, CAST_SIZE, DEBUGLOG2, expand(), mData, mPos, and writeInt16().

Field Documentation

◆ mData

char* Net::MessageOut::mData
protected

◆ mDataSize

unsigned int Net::MessageOut::mDataSize
protected

Size of data.

Definition at line 133 of file messageout.h.

Referenced by getDataSize().

◆ mId

uint16_t Net::MessageOut::mId
protected

Definition at line 135 of file messageout.h.

◆ mIgnore

bool Net::MessageOut::mIgnore
protected

Definition at line 136 of file messageout.h.

◆ mPos

unsigned int Net::MessageOut::mPos
protected

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