connectionStringTable.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 _H_CONNECTIONSTRINGTABLE
  23. #define _H_CONNECTIONSTRINGTABLE
  24. /// Maintain a table of strings which are shared across the network.
  25. ///
  26. /// This allows us to reference strings in our network streams more efficiently.
  27. class ConnectionStringTable
  28. {
  29. public:
  30. enum Constants {
  31. EntryCount = 32,
  32. EntryBitSize = 5,
  33. InvalidEntryId = 32,
  34. };
  35. private:
  36. struct Entry {
  37. NetStringHandle string; ///< Global string table entry of this string
  38. /// will be 0 if this string is unused.
  39. U32 index; ///< index of this entry
  40. Entry *nextHash; ///< the next hash entry for this id
  41. Entry *nextLink; ///< the next in the LRU list
  42. Entry *prevLink; ///< the prev entry in the LRU list
  43. bool receiveConfirmed; ///< The other side now has this string.
  44. };
  45. Entry mEntryTable[EntryCount];
  46. Entry *mHashTable[EntryCount];
  47. NetStringHandle mRemoteStringTable[EntryCount];
  48. Entry mLRUHead, mLRUTail;
  49. /// Connection over which we are maintaining this string table.
  50. NetConnection *mParent;
  51. inline void pushBack(Entry *entry) // pushes an entry to the back of the LRU list
  52. {
  53. entry->prevLink->nextLink = entry->nextLink;
  54. entry->nextLink->prevLink = entry->prevLink;
  55. entry->nextLink = &mLRUTail;
  56. entry->prevLink = mLRUTail.prevLink;
  57. entry->nextLink->prevLink = entry;
  58. entry->prevLink->nextLink = entry;
  59. }
  60. public:
  61. /// Initialize the connection string table.
  62. ///
  63. /// @param parent Connection over which we are maintaining this string table.
  64. ConnectionStringTable(NetConnection *parent);
  65. /// Has the specified string been received on the other side?
  66. inline void confirmStringReceived(NetStringHandle &string, U32 index)
  67. {
  68. if(mEntryTable[index].string == string)
  69. mEntryTable[index].receiveConfirmed = true;
  70. }
  71. U32 checkString(NetStringHandle &stringTableId, bool *stringOnOtherSide = NULL); ///< Checks if the global string ID is
  72. /// currently valid for this connection
  73. /// and returns the table ID.
  74. /// Sends a string event to the other side
  75. /// if it is not active.
  76. /// It will fill in stringOnOtherSide.
  77. U32 getNetSendId(NetStringHandle &stringTableId); ///< Same return value as checkString
  78. /// but will assert if the string is not
  79. /// valid.
  80. void mapString(U32 netId, NetStringHandle &string); ///< Maps a string that
  81. /// was just sent over the net
  82. /// to the corresponding net ID.
  83. inline NetStringHandle lookupString(U32 netId) ///< looks up the string ID and returns
  84. { /// the global string table ID for that string.
  85. return mRemoteStringTable[netId];
  86. }
  87. /// @name Demo functionality
  88. /// @{
  89. void readDemoStartBlock(BitStream *stream);
  90. void writeDemoStartBlock(ResizeBitStream *stream);
  91. /// @}
  92. };
  93. #endif