FragmentedTransferManager.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file FragmentedTransferManager.h
  13. @brief The classes \ref kNet::FragmentedSendManager FragmentedSendManager and
  14. \ref kNet::FragmentedSendManager FragmentedReceiveManager. For managing partial transfers. */
  15. #include <vector>
  16. #include <list>
  17. namespace kNet
  18. {
  19. class NetworkMessage;
  20. /// @internal Manages the allocation of transferIDs to fragmented message transfers and tracks which of the fragments have
  21. /// successfully been sent over to the receiver.
  22. class FragmentedSendManager
  23. {
  24. public:
  25. struct FragmentedTransfer
  26. {
  27. int id;
  28. /// The total number of fragments in this message.
  29. size_t totalNumFragments;
  30. std::list<NetworkMessage*> fragments;
  31. void AddMessage(NetworkMessage *message);
  32. /// Returns true if the given message was part of this transfer (which now got removed).
  33. bool RemoveMessage(NetworkMessage *message);
  34. };
  35. typedef std::list<FragmentedTransfer> TransferList;
  36. TransferList transfers;
  37. /// Returns a new FragmentedTransfer. A transferID for this transfer will not have been allocated here.
  38. /// When sending the message is finished, call FreeFragmentedTransfer.
  39. FragmentedTransfer *AllocateNewFragmentedTransfer();
  40. void RemoveMessage(FragmentedTransfer *transfer, NetworkMessage *message);
  41. /// @return True if the allocation succeeded, false otherwise.
  42. bool AllocateFragmentedTransferID(FragmentedTransfer &transfer);
  43. void FreeAllTransfers();
  44. private:
  45. void FreeFragmentedTransfer(FragmentedTransfer *transfer);
  46. };
  47. /// @internal Receives message fragments and assembles fragments to complete messages when they are finished.
  48. class FragmentedReceiveManager
  49. {
  50. public:
  51. struct ReceiveFragment
  52. {
  53. int fragmentIndex;
  54. std::vector<char> data;
  55. };
  56. struct ReceiveTransfer
  57. {
  58. int transferID;
  59. int numTotalFragments;
  60. std::vector<ReceiveFragment> fragments;
  61. };
  62. std::vector<ReceiveTransfer> transfers;
  63. void NewFragmentStartReceived(int transferID, int numTotalFragments, const char *data, size_t numBytes);
  64. bool NewFragmentReceived(int transferID, int fragmentNumber, const char *data, size_t numBytes);
  65. void AssembleMessage(int transferID, std::vector<char> &assembledData);
  66. void FreeMessage(int transferID);
  67. };
  68. } // ~kNet