ChatMessage.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "ChatMessage.h"
  2. #ifndef _WINDOWS
  3. # include <sys/time.h>
  4. #endif
  5. ChatMessage::ChatMessage(Framework::Text message,
  6. Framework::Text channel,
  7. Framework::Text targetPlayerName)
  8. : ReferenceCounter(),
  9. message(message),
  10. channel(channel),
  11. targetPlayerName(targetPlayerName)
  12. {
  13. #ifdef _WINDOWS
  14. struct tm timeinfo;
  15. std::time(&time);
  16. gmtime_s(&timeinfo ,& time); /* Convert to UTC */
  17. time = mktime(&timeinfo); /* Store as unix timestamp */
  18. #else
  19. timeval tv;
  20. gettimeofday(&tv, 0);
  21. time = tv.tv_sec;
  22. #endif
  23. }
  24. ChatMessage::ChatMessage(Framework::StreamReader* zReader)
  25. : ReferenceCounter()
  26. {
  27. zReader->lese((char*)&time, 8);
  28. short len;
  29. zReader->lese((char*)&len, 2);
  30. char* buffer = new char[len + 1];
  31. zReader->lese(buffer, len);
  32. buffer[len] = 0;
  33. message = buffer;
  34. delete[] buffer;
  35. char channelLen;
  36. zReader->lese(&channelLen, 1);
  37. buffer = new char[channelLen + 1];
  38. zReader->lese(buffer, channelLen);
  39. buffer[(int)channelLen] = 0;
  40. channel = buffer;
  41. delete[] buffer;
  42. zReader->lese(&channelLen, 1);
  43. buffer = new char[channelLen + 1];
  44. zReader->lese(buffer, channelLen);
  45. buffer[(int)channelLen] = 0;
  46. targetPlayerName = buffer;
  47. delete[] buffer;
  48. }
  49. Framework::Text ChatMessage::getChannel() const
  50. {
  51. return channel;
  52. }
  53. Framework::Text ChatMessage::getTargetPlayerName() const
  54. {
  55. return targetPlayerName;
  56. }
  57. void ChatMessage::writeTo(Framework::StreamWriter* zWriter) const
  58. {
  59. zWriter->schreibe((char*)&time, 8);
  60. short mLen = (short)message.getLength();
  61. zWriter->schreibe((char*)&mLen, 2);
  62. zWriter->schreibe(message.getText(), mLen);
  63. char len = (char)channel.getLength();
  64. zWriter->schreibe(&len, 1);
  65. zWriter->schreibe(channel.getText(), len);
  66. len = (char)targetPlayerName.getLength();
  67. zWriter->schreibe(&len, 1);
  68. zWriter->schreibe(targetPlayerName.getText(), len);
  69. }