netStringTable.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "core/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 : U32
  41. {
  42. InitialSize = 16,
  43. InvalidEntry = 0xFFFFFFFF,
  44. HashTableSize = 2128,
  45. DataChunkerSize = 65536
  46. };
  47. struct Entry
  48. {
  49. char *string;
  50. U32 refCount;
  51. U32 scriptRefCount;
  52. U32 next;
  53. U32 link;
  54. U32 prevLink;
  55. U32 seq;
  56. };
  57. U32 size;
  58. U32 firstFree;
  59. U32 firstValid;
  60. U32 sequenceCount;
  61. Entry *table;
  62. U32 hashTable[HashTableSize];
  63. DataChunker *allocator;
  64. NetStringTable();
  65. ~NetStringTable();
  66. U32 addString(const char *string);
  67. // XA: Moved this ones to public to avoid using the friend_ConsoleMethod hack.
  68. public:
  69. const char *lookupString(U32 id);
  70. void removeString(U32 id, bool script = false);
  71. void incStringRefScript(U32 id);
  72. private:
  73. void incStringRef(U32 id);
  74. void repack();
  75. public:
  76. static void create();
  77. static void destroy();
  78. static void expandString(NetStringHandle &string, char *buf, U32 bufSize, U32 argc, const char **argv);
  79. #if defined(TORQUE_DEBUG)
  80. void dumpToConsole();
  81. #endif // DEBUG
  82. };
  83. extern NetStringTable *gNetStringTable;
  84. // This class represents what is known as a "tagged string" in script.
  85. // It is essentially a networked version of the string table. The full string
  86. // is only transmitted once; then future references only need to transmit
  87. // the id.
  88. class NetStringHandle
  89. {
  90. U32 index;
  91. public:
  92. NetStringHandle() { index = 0; }
  93. NetStringHandle(const NetStringHandle &string) {
  94. index = string.index;
  95. if(index)
  96. gNetStringTable->incStringRef(index);
  97. }
  98. NetStringHandle(const char *string) {
  99. index = gNetStringTable->addString(string);
  100. }
  101. NetStringHandle(U32 initIndex)
  102. {
  103. index = initIndex;
  104. if(index)
  105. gNetStringTable->incStringRef(index);
  106. }
  107. ~NetStringHandle()
  108. {
  109. if(index)
  110. gNetStringTable->removeString(index);
  111. }
  112. void setFromIndex(U32 newIndex)
  113. {
  114. if(index)
  115. gNetStringTable->removeString(index);
  116. index = newIndex;
  117. }
  118. bool operator==(const NetStringHandle &s) const { return index == s.index; }
  119. bool operator!=(const NetStringHandle &s) const { return index != s.index; }
  120. NetStringHandle &operator=(const NetStringHandle &s)
  121. {
  122. if(index)
  123. gNetStringTable->removeString(index);
  124. index = s.index;
  125. if(index)
  126. gNetStringTable->incStringRef(index);
  127. return *this;
  128. }
  129. const char *getString() const
  130. {
  131. if(index)
  132. return gNetStringTable->lookupString(index);
  133. else
  134. return NULL;
  135. }
  136. bool isNull() const { return index == 0; }
  137. bool isValidString() const { return index != 0; }
  138. U32 getIndex() const { return index; }
  139. };
  140. #endif