connectionStringTable.cpp
Engine/source/sim/connectionStringTable.cpp
Classes:
class
Public Functions
ConsoleDocClass(NetStringEvent , "@brief Internal event used <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> transmitting strings across the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">server.\n\n</a>" "For internal use only, not intended <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> TorqueScript or game <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">development\n\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">internal\n</a>" )
Detailed Description
Public Functions
ConsoleDocClass(NetStringEvent , "@brief Internal event used <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> transmitting strings across the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">server.\n\n</a>" "For internal use only, not intended <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> TorqueScript or game <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">development\n\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">internal\n</a>" )
IMPLEMENT_CO_NETEVENT_V1(NetStringEvent )
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#include "platform/platform.h" 25#include "core/dnet.h" 26#include "console/simBase.h" 27#include "sim/netConnection.h" 28#include "core/stream/bitStream.h" 29#include "console/consoleTypes.h" 30 31class NetStringEvent : public NetEvent 32{ 33 NetStringHandle mString; 34 U32 mIndex; 35public: 36 typedef NetEvent Parent; 37 NetStringEvent(U32 index = 0, NetStringHandle string = NetStringHandle()) 38 { 39 mIndex = index; 40 mString = string; 41 } 42 virtual void pack(NetConnection* /*ps*/, BitStream *bstream) 43 { 44 bstream->writeInt(mIndex, ConnectionStringTable::EntryBitSize); 45 bstream->writeString(mString.getString()); 46 } 47 virtual void write(NetConnection* /*ps*/, BitStream *bstream) 48 { 49 bstream->writeInt(mIndex, ConnectionStringTable::EntryBitSize); 50 bstream->writeString(mString.getString()); 51 } 52 virtual void unpack(NetConnection* /*con*/, BitStream *bstream) 53 { 54 char buf[256]; 55 mIndex = bstream->readInt(ConnectionStringTable::EntryBitSize); 56 bstream->readString(buf); 57 mString = NetStringHandle(buf); 58 } 59 virtual void notifyDelivered(NetConnection *ps, bool madeit) 60 { 61 if(madeit) 62 ps->confirmStringReceived(mString, mIndex); 63 } 64 virtual void process(NetConnection *connection) 65 { 66 Con::printf("Mapping string: %s to index: %d", mString.getString(), mIndex); 67 connection->mapString(mIndex, mString); 68 } 69#ifdef TORQUE_DEBUG_NET 70 const char *getDebugName() 71 { 72 static char buffer[512]; 73 dSprintf(buffer, sizeof(buffer), "%s - \"", getClassName()); 74 expandEscape(buffer + dStrlen(buffer), mString.getString()); 75 dStrcat(buffer, "\"", 512); 76 return buffer; 77 } 78#endif 79 DECLARE_CONOBJECT(NetStringEvent); 80}; 81 82IMPLEMENT_CO_NETEVENT_V1(NetStringEvent); 83 84ConsoleDocClass( NetStringEvent, 85 "@brief Internal event used for transmitting strings across the server.\n\n" 86 87 "For internal use only, not intended for TorqueScript or game development\n\n" 88 89 "@internal\n" 90); 91 92//-------------------------------------------------------------------- 93 94 95ConnectionStringTable::ConnectionStringTable(NetConnection *parent) 96{ 97 mParent = parent; 98 for(U32 i = 0; i < EntryCount; i++) 99 { 100 mEntryTable[i].nextHash = NULL; 101 mEntryTable[i].nextLink = &mEntryTable[i+1]; 102 mEntryTable[i].prevLink = &mEntryTable[i-1]; 103 mEntryTable[i].index = i; 104 105 mHashTable[i] = NULL; 106 } 107 mLRUHead.nextLink = &mEntryTable[0]; 108 mEntryTable[0].prevLink = &mLRUHead; 109 mLRUTail.prevLink = &mEntryTable[EntryCount-1]; 110 mEntryTable[EntryCount-1].nextLink = &mLRUTail; 111} 112 113U32 ConnectionStringTable::getNetSendId(NetStringHandle &string) 114{ 115 // see if the entry is in the hash table right now 116 U32 hashIndex = string.getIndex() % EntryCount; 117 for(Entry *walk = mHashTable[hashIndex]; walk; walk = walk->nextHash) 118 if(walk->string == string) 119 return walk->index; 120 AssertFatal(0, "Net send id is not in the table. Error!"); 121 return 0; 122} 123 124U32 ConnectionStringTable::checkString(NetStringHandle &string, bool *isOnOtherSide) 125{ 126 // see if the entry is in the hash table right now 127 U32 hashIndex = string.getIndex() % EntryCount; 128 for(Entry *walk = mHashTable[hashIndex]; walk; walk = walk->nextHash) 129 { 130 if(walk->string == string) 131 { 132 pushBack(walk); 133 if(isOnOtherSide) 134 *isOnOtherSide = walk->receiveConfirmed; 135 return walk->index; 136 } 137 } 138 139 // not in the hash table, means we have to add it 140 Entry *newEntry; 141 142 // pull the new entry from the LRU list. 143 newEntry = mLRUHead.nextLink; 144 pushBack(newEntry); 145 146 // remove the string from the hash table 147 Entry **hashWalk; 148 for (hashWalk = &mHashTable[newEntry->string.getIndex() % EntryCount]; *hashWalk; hashWalk = &((*hashWalk)->nextHash)) 149 { 150 if(*hashWalk == newEntry) 151 { 152 *hashWalk = newEntry->nextHash; 153 break; 154 } 155 } 156 157 newEntry->string = string; 158 newEntry->receiveConfirmed = false; 159 newEntry->nextHash = mHashTable[hashIndex]; 160 mHashTable[hashIndex] = newEntry; 161 162 mParent->postNetEvent(new NetStringEvent(newEntry->index, string)); 163 if(isOnOtherSide) 164 *isOnOtherSide = false; 165 return newEntry->index; 166} 167 168void ConnectionStringTable::mapString(U32 netId, NetStringHandle &string) 169{ 170 // the netId is sent by the other side... throw it in our mapping table: 171 mRemoteStringTable[netId] = string; 172} 173 174void ConnectionStringTable::readDemoStartBlock(BitStream *stream) 175{ 176 // ok, reading the demo start block 177 for(U32 i = 0; i < EntryCount; i++) 178 { 179 if(stream->readFlag()) 180 { 181 char buffer[256]; 182 stream->readString(buffer); 183 mRemoteStringTable[i] = NetStringHandle(buffer); 184 } 185 } 186} 187 188void ConnectionStringTable::writeDemoStartBlock(ResizeBitStream *stream) 189{ 190 // ok, writing the demo start block 191 for(U32 i = 0; i < EntryCount; i++) 192 { 193 if(stream->writeFlag(mRemoteStringTable[i].isValidString())) 194 { 195 stream->writeString(mRemoteStringTable[i].getString()); 196 stream->validate(); 197 } 198 } 199} 200