NetworkSimulator.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // Modified by Lasse Oorni for Urho3D
  12. // Urho3D: MessageConnection.h already includes NetworkSimulator.h, make sure WS2Include.h is included before windows.h / winsock.h
  13. #include "kNet/MessageConnection.h"
  14. namespace kNet
  15. {
  16. NetworkSimulator::NetworkSimulator()
  17. :enabled(false),
  18. packetLossRate(0),
  19. constantPacketSendDelay(0),
  20. uniformRandomPacketSendDelay(0),
  21. packetDuplicationRate(0.f),
  22. corruptionType(CorruptDatagram),
  23. corruptMessageId(0),
  24. corruptToggleBitsRate(0),
  25. corruptMinBits(0),
  26. corruptMaxBits(0),
  27. owner(0)
  28. {
  29. }
  30. NetworkSimulator::~NetworkSimulator()
  31. {
  32. if (queuedBuffers.size() > 0)
  33. KNET_LOG(LogError, "NetworkSimulator: Leaked %d buffers with improper NetworkSimulator teardown!", (int)queuedBuffers.size());
  34. }
  35. /// Generates a float in half-open interval [0, 1[.
  36. static float rand01()
  37. {
  38. assert(RAND_MAX >= 0x7FFF); ///\todo static assert.
  39. return (float)(rand()&0x7FFF) / 0x8000;
  40. }
  41. /// Generates a float in closed interval [0, 1].
  42. static float rand01incl()
  43. {
  44. assert(RAND_MAX >= 0x7FFF); ///\todo static assert.
  45. return (float)(rand()&0x7FFF) / 0x7FFF;
  46. }
  47. void NetworkSimulator::Free()
  48. {
  49. if (!owner || !owner->GetSocket())
  50. return;
  51. for(size_t i = 0; i < queuedBuffers.size(); ++i)
  52. owner->GetSocket()->AbortSend(queuedBuffers[i].buffer);
  53. queuedBuffers.clear();
  54. }
  55. void NetworkSimulator::SubmitSendBuffer(kNet::OverlappedTransferBuffer *buffer, Socket *socket)
  56. {
  57. if (rand01() < packetLossRate)
  58. {
  59. if (owner && owner->GetSocket())
  60. owner->GetSocket()->AbortSend(buffer);
  61. return; // Dropped this packet!
  62. }
  63. // Should we duplicate this packet?
  64. if (rand01() < packetDuplicationRate)
  65. {
  66. QueuedBuffer b;
  67. assert(socket);
  68. b.buffer = socket->BeginSend(buffer->bytesContains);
  69. if (b.buffer)
  70. {
  71. assert(b.buffer->buffer.len >= (u32)buffer->bytesContains);
  72. memcpy(b.buffer->buffer.buf, buffer->buffer.buf, buffer->bytesContains);
  73. b.buffer->bytesContains = buffer->bytesContains;
  74. // Should we corrupt the newly created copy?
  75. if (corruptionType == CorruptDatagram)
  76. MaybeCorruptBufferToggleBits(b.buffer->buffer.buf, b.buffer->bytesContains);
  77. b.timeUntilTransfer.StartMSecs(constantPacketSendDelay + rand01incl() * uniformRandomPacketSendDelay);
  78. queuedBuffers.push_back(b);
  79. }
  80. }
  81. // Should we corrupt this packet?
  82. if (corruptionType == CorruptDatagram)
  83. MaybeCorruptBufferToggleBits(buffer->buffer.buf, buffer->bytesContains);
  84. QueuedBuffer b;
  85. b.buffer = buffer;
  86. b.timeUntilTransfer.StartMSecs(constantPacketSendDelay + rand01incl() * uniformRandomPacketSendDelay);
  87. queuedBuffers.push_back(b);
  88. }
  89. void NetworkSimulator::Process()
  90. {
  91. for(size_t i = 0; i < queuedBuffers.size(); ++i)
  92. if (queuedBuffers[i].timeUntilTransfer.Test())
  93. {
  94. if (owner && owner->GetSocket())
  95. owner->GetSocket()->EndSend(queuedBuffers[i].buffer);
  96. queuedBuffers.erase(queuedBuffers.begin() + i);
  97. --i;
  98. }
  99. }
  100. void NetworkSimulator::MaybeCorruptBufferToggleBits(void *buffer, size_t numBytes) const
  101. {
  102. // Should corrupt this data?
  103. if (rand01() < corruptToggleBitsRate)
  104. {
  105. int numBitsToCorrupt = corruptMinBits + (int)(rand01() * (corruptMaxBits - corruptMinBits + 1));
  106. for(int i = 0; i < numBitsToCorrupt; ++i)
  107. {
  108. int byteIndex = (int)(rand01() * numBytes);
  109. int bitIndex = rand() % 8;
  110. int bitMask = (1 << bitIndex);
  111. ((char*)buffer)[byteIndex] ^= bitMask;
  112. }
  113. }
  114. }
  115. } // ~kNet