netStringTable.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _NETSTRINGTABLE_H_
  23. #define _NETSTRINGTABLE_H_
  24. #ifndef _DATACHUNKER_H_
  25. #include "memory/dataChunker.h"
  26. #endif
  27. #ifndef _CONSOLE_H_
  28. #include "console/console.h"
  29. #endif
  30. class NetConnection;
  31. class NetStringHandle;
  32. extern U32 GameAddTaggedString(const char *string);
  33. class NetStringTable
  34. {
  35. friend class NetStringHandle;
  36. friend U32 GameAddTaggedString(const char *string);
  37. #ifdef TORQUE_DEBUG_NET
  38. friend class RemoteCommandEvent;
  39. #endif
  40. enum Constants {
  41. InitialSize = 16,
  42. InvalidEntry = 0xFFFFFFFF,
  43. HashTableSize = 2128,
  44. DataChunkerSize = 65536
  45. };
  46. struct Entry
  47. {
  48. char *string;
  49. U32 refCount;
  50. U32 scriptRefCount;
  51. U32 next;
  52. U32 link;
  53. U32 prevLink;
  54. U32 seq;
  55. };
  56. U32 size;
  57. U32 firstFree;
  58. U32 firstValid;
  59. U32 sequenceCount;
  60. Entry *table;
  61. U32 hashTable[HashTableSize];
  62. DataChunker *allocator;
  63. NetStringTable();
  64. ~NetStringTable();
  65. U32 addString(const char *string);
  66. // XA: Moved this ones to public to avoid using the friend_ConsoleMethod hack.
  67. public:
  68. const char *lookupString(U32 id);
  69. void removeString(U32 id, bool script = false);
  70. void incStringRefScript(U32 id);
  71. private:
  72. void incStringRef(U32 id);
  73. void repack();
  74. public:
  75. static void create();
  76. static void destroy();
  77. static void expandString(NetStringHandle &string, char *buf, U32 bufSize, U32 argc, const char **argv);
  78. #if defined(TORQUE_DEBUG)
  79. void dumpToConsole();
  80. #endif // DEBUG
  81. };
  82. extern NetStringTable *gNetStringTable;
  83. class NetStringHandle
  84. {
  85. U32 index;
  86. public:
  87. NetStringHandle() { index = 0; }
  88. NetStringHandle(const NetStringHandle &string) {
  89. index = string.index;
  90. if(index)
  91. gNetStringTable->incStringRef(index);
  92. }
  93. NetStringHandle(const char *string) {
  94. index = gNetStringTable->addString(string);
  95. }
  96. NetStringHandle(U32 initIndex)
  97. {
  98. index = initIndex;
  99. if(index)
  100. gNetStringTable->incStringRef(index);
  101. }
  102. ~NetStringHandle()
  103. {
  104. if(index)
  105. gNetStringTable->removeString(index);
  106. }
  107. void setFromIndex(U32 newIndex)
  108. {
  109. if(index)
  110. gNetStringTable->removeString(index);
  111. index = newIndex;
  112. }
  113. bool operator==(const NetStringHandle &s) const { return index == s.index; }
  114. bool operator!=(const NetStringHandle &s) const { return index != s.index; }
  115. NetStringHandle &operator=(const NetStringHandle &s)
  116. {
  117. if(index)
  118. gNetStringTable->removeString(index);
  119. index = s.index;
  120. if(index)
  121. gNetStringTable->incStringRef(index);
  122. return *this;
  123. }
  124. const char *getString()
  125. {
  126. if(index)
  127. return gNetStringTable->lookupString(index);
  128. else
  129. return NULL;
  130. }
  131. bool isNull() { return index == 0; }
  132. bool isValidString() { return index != 0; }
  133. U32 getIndex() { return index; }
  134. };
  135. #endif