ChatMessage.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "ChatMessage.h"
  2. #include <sys/time.h>
  3. ChatMessage::ChatMessage(Framework::Text message,
  4. Framework::Text channel,
  5. Framework::Text targetPlayerName)
  6. : ReferenceCounter(),
  7. message(message),
  8. channel(channel),
  9. targetPlayerName(targetPlayerName)
  10. {
  11. timeval tv;
  12. gettimeofday(&tv, 0);
  13. time = tv.tv_sec;
  14. }
  15. ChatMessage::ChatMessage(Framework::StreamReader* zReader)
  16. : ReferenceCounter()
  17. {
  18. short len;
  19. zReader->lese((char*)&len, 2);
  20. char* buffer = new char[len + 1];
  21. zReader->lese(buffer, len);
  22. buffer[len] = 0;
  23. message = buffer;
  24. delete[] buffer;
  25. char channelLen;
  26. zReader->lese(&channelLen, 1);
  27. buffer = new char[channelLen + 1];
  28. zReader->lese(buffer, channelLen);
  29. buffer[(int)channelLen] = 0;
  30. channel = buffer;
  31. delete[] buffer;
  32. zReader->lese(&channelLen, 1);
  33. buffer = new char[channelLen + 1];
  34. zReader->lese(buffer, channelLen);
  35. buffer[(int)channelLen] = 0;
  36. targetPlayerName = buffer;
  37. delete[] buffer;
  38. }
  39. Framework::Text ChatMessage::getChannel() const
  40. {
  41. return channel;
  42. }
  43. Framework::Text ChatMessage::getTargetPlayerName() const
  44. {
  45. return targetPlayerName;
  46. }
  47. void ChatMessage::writeTo(Framework::StreamWriter* zWriter) const
  48. {
  49. short mLen = (short)message.getLength();
  50. zWriter->schreibe((char*)&mLen, 2);
  51. zWriter->schreibe(message.getText(), mLen);
  52. char len = (char)channel.getLength();
  53. zWriter->schreibe(&len, 1);
  54. zWriter->schreibe(channel.getText(), len);
  55. len = (char)targetPlayerName.getLength();
  56. zWriter->schreibe(&len, 1);
  57. zWriter->schreibe(targetPlayerName.getText(), len);
  58. }