NetworkSimulator.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 NetworkSimulator.h
  13. @brief The NetworkSimulator class, which enables different network conditions testing. */
  14. #include "kNetFwd.h"
  15. #include "PolledTimer.h"
  16. #include <vector>
  17. #include <cstring>
  18. namespace kNet
  19. {
  20. /// A NetworkSimulator is attached to MessageConnections to add in an intermediate layer for
  21. /// network conditions testing.
  22. class NetworkSimulator
  23. {
  24. public:
  25. NetworkSimulator();
  26. ~NetworkSimulator();
  27. /// If false, the network simulator is not being used.
  28. /// By default, this is always false.
  29. bool enabled;
  30. /// Specifies the percentage of messages to drop. This is in the range [0.0, 1.0]. Default: 0 (disabled).
  31. float packetLossRate;
  32. /// Specifies a constant delay to add to each packet (msecs). Default: 0.
  33. float constantPacketSendDelay;
  34. /// Specifies an amount of uniformly random delay to add to each packet (msecs), [0, uniformRandomPacketSendDelay]. Default: 0.
  35. float uniformRandomPacketSendDelay;
  36. /// Specifies the percentage of messages to duplicate. This is in the range [0.0, 1.0]. Default: 0 (disabled).
  37. float packetDuplicationRate;
  38. /// Corruption options.
  39. enum
  40. {
  41. CorruptDatagram, ///< The whole datagram is subjected to data corruption. This is the default.
  42. CorruptPayload, ///< Only kNet message payload (client-side data) will be subjected to corruption.
  43. CorruptMessageType ///< Only the message payload of a single given message type will be subjected to corruption.
  44. } corruptionType;
  45. /// If corruptionType == CorruptMessageType, this field specifies a single specific message type ID which
  46. /// will be subjected to corruption. Default: 0. No message has the ID 0, i.e. this effectively means "disabled".
  47. int corruptMessageId;
  48. /// Rate in % of the datagrams to corrupt. Default: 0.
  49. float corruptToggleBitsRate;
  50. /// The minimum number of bits to corrupt when a datagram is decided to be tampered. Default: 0.
  51. int corruptMinBits;
  52. /// The maximum number of bits to corrupt when a datagram is decided to be tampered. Default: 0.
  53. int corruptMaxBits;
  54. void SubmitSendBuffer(OverlappedTransferBuffer *buffer, Socket *socket);
  55. /// Runs a polled update tick on the network simulator. Transfers all expired data.
  56. void Process();
  57. /// Discards and frees all currently queued messages.
  58. void Free();
  59. /// Performs a random roll against the corruptToggleBitsRate counter, and perhaps corrupts some bits
  60. /// of the given buffer.
  61. /// Alters the raw byte buffer contents by flipping some bits according to the currently specified
  62. /// parameters.
  63. void MaybeCorruptBufferToggleBits(void *buffer, size_t numBytes) const;
  64. private:
  65. struct QueuedBuffer
  66. {
  67. OverlappedTransferBuffer *buffer;
  68. /// Stores how long to delay this buffer until transfer.
  69. PolledTimer timeUntilTransfer;
  70. };
  71. std::vector<QueuedBuffer> queuedBuffers;
  72. MessageConnection *owner;
  73. friend class MessageConnection;
  74. };
  75. } // ~kNet