SnapshotProcessor.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __SNAP_PROCESSOR_H__
  21. #define __SNAP_PROCESSOR_H__
  22. /*
  23. ================================================
  24. idSnapshotProcessor
  25. ================================================
  26. */
  27. class idSnapshotProcessor {
  28. public:
  29. static const int INITIAL_SNAP_SEQUENCE = 42;
  30. idSnapshotProcessor();
  31. ~idSnapshotProcessor();
  32. void Reset( bool cstor = false );
  33. // TrySetPendingSnapshot Sets the currently pending snap.
  34. // No new snaps will be sent until this snap has been fully sent.
  35. // Returns true of the newly supplied snapshot was accepted (there were no pending snaps)
  36. bool TrySetPendingSnapshot( idSnapShot & ss );
  37. // Peek into delta to get deltaSequence, and deltaBaseSequence
  38. void PeekDeltaSequence( const char * deltaMem, int deltaSize, int & deltaSequence, int & deltaBaseSequence );
  39. // Apply a delta to the supplied snapshot
  40. bool ApplyDeltaToSnapshot( idSnapShot & snap, const char * deltaMem, int deltaSize, int visIndex );
  41. // Attempts to write the currently pending snap to the supplied buffer, which can then be sent as an unreliable msg.
  42. // SubmitPendingSnap will submit the pending snap to a job, so that it can be retrieved later for sending.
  43. void SubmitPendingSnap( int visIndex, uint8 * objMemory, int objMemorySize, lzwCompressionData_t * lzwData );
  44. // GetPendingSnapDelta
  45. int GetPendingSnapDelta( byte * outBuffer, int maxLength );
  46. // If PendingSnapReadyToSend is true, then GetPendingSnapDelta will return something to send
  47. bool PendingSnapReadyToSend() const { return jobMemory->lzwInOutData.numlzwDeltas > 0; }
  48. // When you call WritePendingSnapshot, and then send the resulting buffer as a unreliable msg, you will eventually
  49. // receive this on the client. Call this function to receive and apply it to the base state, and possibly return a fully received snap
  50. // to then apply to the client game state
  51. bool ReceiveSnapshotDelta( const byte * deltaData, int deltaLength, int visIndex, int & outSeq, int & outBaseSeq, idSnapShot & outSnap, bool & fullSnap );
  52. // Function to apply a received (or ack'd) delta to the base state
  53. bool ApplySnapshotDelta( int visIndex, int snapshotNumber );
  54. // Remove deltas for basestate we no longer have.
  55. // We know we can remove them, because we will never be able to apply them, since
  56. // the basestate needed to generate a full snap from these deltas is gone.
  57. void RemoveDeltasForOldBaseSequence();
  58. // Make sure delta sequence and basesequence values are valid, and in order, etc
  59. void SanityCheckDeltas();
  60. // HasPendingSnap will return true if there is more of the last TrySetPendingSnapshot to be sent
  61. bool HasPendingSnap() const { return hasPendingSnap; }
  62. idSnapShot * GetBaseState() { return &baseState; }
  63. idSnapShot * GetPendingSnap(){ return &pendingSnap; }
  64. int GetSnapSequence() { return snapSequence; }
  65. int GetBaseSequence() { return baseSequence; }
  66. int GetFullSnapBaseSequence() { return lastFullSnapBaseSequence; }
  67. // This is used to ack the latest delta we have. If we have no deltas, we sent -1 to make sure
  68. // Server knows we don't want to ack, since we are as up to date as we can be
  69. int GetLastAppendedSequence() { return deltas.Num() == 0 ? -1 : deltas.ItemSequence( deltas.Num() - 1 ); }
  70. int GetSnapQueueSize() { return deltas.Num(); }
  71. bool IsBusyConfirmingPartialSnap();
  72. void AddSnapObjTemplate( int objID, idBitMsg & msg );
  73. static const int MAX_SNAPSHOT_QUEUE = 64;
  74. private:
  75. // Internal commands to set up, and flush the compressors
  76. static const int MAX_SNAP_SIZE = idPacketProcessor::MAX_MSG_SIZE;
  77. static const int MAX_SNAPSHOT_QUEUE_MEM = 64 * 1024; // 64k
  78. // sequence number of the last snapshot we sent/received
  79. // on the server, the sequencing is different for each network peer (net_verboseSnapshot 1)
  80. // on the jobbed snapshot compression path, the sequence is incremented in NewLZWStream and pulled into this in idSnapshotProcessor::GetPendingSnapDelta
  81. int snapSequence;
  82. int baseSequence;
  83. int lastFullSnapBaseSequence; // Latest base sequence number that is a full snap
  84. idSnapShot baseState; // known snapshot base on the client
  85. idDataQueue< MAX_SNAPSHOT_QUEUE, MAX_SNAPSHOT_QUEUE_MEM > deltas; // list of unacknowledged snapshot deltas
  86. idSnapShot pendingSnap; // Current snap waiting to be fully sent
  87. bool hasPendingSnap; // true if pendingSnap is still waiting to be sent
  88. struct jobMemory_t {
  89. static const int MAX_LZW_DELTAS = 1; // FIXME: cleanup the old multiple delta support completely
  90. // @TODO this is a hack fix to allow online to load into coop (where there are lots of entities).
  91. // The real solution should be coming soon.
  92. // Doom MP: we encountered the same problem, going from 1024 to 4096 as well until a better solution is in place
  93. // (initial, useless, exchange of func_statics is killing us)
  94. static const int MAX_OBJ_PARMS = 4096;
  95. static const int MAX_LZW_PARMS = 32;
  96. static const int MAX_OBJ_HEADERS = 256;
  97. static const int MAX_LZW_MEM = 1024 * 8; // 8k in the byte * lzwMem buffers, must be <= PS3_DMA_MAX
  98. // Parm memory to jobs
  99. idArray<objParms_t, MAX_OBJ_PARMS> objParms;
  100. idArray<objHeader_t, MAX_OBJ_HEADERS> headers;
  101. idArray<lzwParm_t, MAX_LZW_PARMS> lzwParms;
  102. // Output memory from jobs
  103. idArray<lzwDelta_t, MAX_LZW_DELTAS> lzwDeltas; // Info about each pending delta output from jobs
  104. idArray<byte, MAX_LZW_MEM> lzwMem; // Memory for output from lzw jobs
  105. lzwInOutData_t lzwInOutData; // In/Out data used so lzw data can persist across lzw jobs
  106. };
  107. jobMemory_t * jobMemory;
  108. idSnapShot submittedState;
  109. idSnapShot templateStates; // holds default snapshot states for some newly spawned object
  110. idSnapShot submittedTemplateStates;
  111. int partialBaseSequence;
  112. };
  113. #endif /* !__SNAP_PROCESSOR_H__ */