ChatMessage.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. zReader->lese((char*)&time, 8);
  19. short len;
  20. zReader->lese((char*)&len, 2);
  21. char* buffer = new char[len + 1];
  22. zReader->lese(buffer, len);
  23. buffer[len] = 0;
  24. message = buffer;
  25. delete[] buffer;
  26. char channelLen;
  27. zReader->lese(&channelLen, 1);
  28. buffer = new char[channelLen + 1];
  29. zReader->lese(buffer, channelLen);
  30. buffer[(int)channelLen] = 0;
  31. channel = buffer;
  32. delete[] buffer;
  33. zReader->lese(&channelLen, 1);
  34. buffer = new char[channelLen + 1];
  35. zReader->lese(buffer, channelLen);
  36. buffer[(int)channelLen] = 0;
  37. targetPlayerName = buffer;
  38. delete[] buffer;
  39. }
  40. Framework::Text ChatMessage::getChannel() const
  41. {
  42. return channel;
  43. }
  44. Framework::Text ChatMessage::getTargetPlayerName() const
  45. {
  46. return targetPlayerName;
  47. }
  48. void ChatMessage::writeTo(Framework::StreamWriter* zWriter) const
  49. {
  50. zWriter->schreibe((char*)&time, 8);
  51. short mLen = (short)message.getLength();
  52. zWriter->schreibe((char*)&mLen, 2);
  53. zWriter->schreibe(message.getText(), mLen);
  54. char len = (char)channel.getLength();
  55. zWriter->schreibe(&len, 1);
  56. zWriter->schreibe(channel.getText(), len);
  57. len = (char)targetPlayerName.getLength();
  58. zWriter->schreibe(&len, 1);
  59. zWriter->schreibe(targetPlayerName.getText(), len);
  60. }