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