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 69 Entry *table; 70 U32 hashTable[HashTableSize]; 71 DataChunker *allocator; 72 73 NetStringTable(); 74 ~<a href="/coding/class/classnetstringtable/">NetStringTable</a>(); 75 76 U32 addString(const char *string); 77 78// XA: Moved this ones to public to avoid using the friend_ConsoleMethod hack. 79public: 80 const char *lookupString(U32 id); 81 void removeString(U32 id, bool script = false); 82 void incStringRefScript(U32 id); 83 84private: 85 void incStringRef(U32 id); 86 87 void repack(); 88public: 89 static void create(); 90 static void destroy(); 91 92 static void expandString(NetStringHandle &string, char *buf, U32 bufSize, U32 argc, const char **argv); 93 94#if defined(TORQUE_DEBUG) 95 void dumpToConsole(); 96#endif // DEBUG 97}; 98 99extern NetStringTable *gNetStringTable; 100 101// This class represents what is known as a "tagged string" in script. 102// It is essentially a networked version of the string table. The full string 103// is only transmitted once; then future references only need to transmit 104// the id. 105 106class NetStringHandle 107{ 108 U32 index; 109public: 110 NetStringHandle() { index = 0; } 111 NetStringHandle(const NetStringHandle &string) { 112 index = string.index; 113 if(index) 114 gNetStringTable->incStringRef(index); 115 } 116 NetStringHandle(const char *string) { 117 index = gNetStringTable->addString(string); 118 } 119 NetStringHandle(U32 initIndex) 120 { 121 index = initIndex; 122 if(index) 123 gNetStringTable->incStringRef(index); 124 } 125 ~NetStringHandle() 126 { 127 if(index) 128 gNetStringTable->removeString(index); 129 } 130 131 void setFromIndex(U32 newIndex) 132 { 133 if(index) 134 gNetStringTable->removeString(index); 135 index = newIndex; 136 } 137 138 bool operator==(const NetStringHandle &s) const { return index == s.index; } 139 bool operator!=(const NetStringHandle &s) const { return index != s.index; } 140 141 NetStringHandle &operator=(const NetStringHandle &s) 142 { 143 if(index) 144 gNetStringTable->removeString(index); 145 index = s.index; 146 if(index) 147 gNetStringTable->incStringRef(index); 148 return *this; 149 } 150 const char *getString() const 151 { 152 if(index) 153 return gNetStringTable->lookupString(index); 154 else 155 return NULL; 156 } 157 bool isNull() const { return index == 0; } 158 bool isValidString() const { return index != 0; } 159 U32 getIndex() const { return index; } 160}; 161 162#endif 163