2
0

netStringTable.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. 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. // This class represents what is known as a "tagged string" in script.
  84. // It is essentially a networked version of the string table. The full string
  85. // is only transmitted once; then future references only need to transmit
  86. // the id.
  87. class NetStringHandle
  88. {
  89. U32 index;
  90. public:
  91. NetStringHandle() { index = 0; }
  92. NetStringHandle(const NetStringHandle &string) {
  93. index = string.index;
  94. if(index)
  95. gNetStringTable->incStringRef(index);
  96. }
  97. NetStringHandle(const char *string) {
  98. index = gNetStringTable->addString(string);
  99. }
  100. NetStringHandle(U32 initIndex)
  101. {
  102. index = initIndex;
  103. if(index)
  104. gNetStringTable->incStringRef(index);
  105. }
  106. ~NetStringHandle()
  107. {
  108. if(index)
  109. gNetStringTable->removeString(index);
  110. }
  111. void setFromIndex(U32 newIndex)
  112. {
  113. if(index)
  114. gNetStringTable->removeString(index);
  115. index = newIndex;
  116. }
  117. bool operator==(const NetStringHandle &s) const { return index == s.index; }
  118. bool operator!=(const NetStringHandle &s) const { return index != s.index; }
  119. NetStringHandle &operator=(const NetStringHandle &s)
  120. {
  121. if(index)
  122. gNetStringTable->removeString(index);
  123. index = s.index;
  124. if(index)
  125. gNetStringTable->incStringRef(index);
  126. return *this;
  127. }
  128. const char *getString() const
  129. {
  130. if(index)
  131. return gNetStringTable->lookupString(index);
  132. else
  133. return NULL;
  134. }
  135. bool isNull() const { return index == 0; }
  136. bool isValidString() const { return index != 0; }
  137. U32 getIndex() const { return index; }
  138. };
  139. #endif