PageRenderTime 36ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/test/llpipeutil.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 183 lines | 136 code | 16 blank | 31 comment | 5 complexity | 7aaf7307f47a1a449799dee51bc11e36 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llpipeutil.cpp
  3. * @date 2006-05-18
  4. * @brief Utility pipe fittings for injecting and extracting strings
  5. *
  6. * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #include "linden_common.h"
  28. #include "llpipeutil.h"
  29. #include <stdlib.h>
  30. #include <sstream>
  31. #include "llbufferstream.h"
  32. #include "lldefs.h"
  33. #include "llframetimer.h"
  34. #include "llpumpio.h"
  35. #include "llrand.h"
  36. #include "lltimer.h"
  37. F32 pump_loop(LLPumpIO* pump, F32 seconds)
  38. {
  39. LLTimer timer;
  40. timer.setTimerExpirySec(seconds);
  41. while(!timer.hasExpired())
  42. {
  43. LLFrameTimer::updateFrameTime();
  44. pump->pump();
  45. pump->callback();
  46. }
  47. return timer.getElapsedTimeF32();
  48. }
  49. //virtual
  50. LLIOPipe::EStatus LLPipeStringInjector::process_impl(
  51. const LLChannelDescriptors& channels,
  52. buffer_ptr_t& buffer,
  53. bool& eos,
  54. LLSD& context,
  55. LLPumpIO* pump)
  56. {
  57. buffer->append(channels.out(), (U8*) mString.data(), mString.size());
  58. eos = true;
  59. return STATUS_DONE;
  60. }
  61. LLIOPipe::EStatus LLPipeStringExtractor::process_impl(
  62. const LLChannelDescriptors& channels,
  63. buffer_ptr_t& buffer,
  64. bool& eos,
  65. LLSD& context,
  66. LLPumpIO* pump)
  67. {
  68. if(!eos) return STATUS_BREAK;
  69. if(!pump || !buffer) return STATUS_PRECONDITION_NOT_MET;
  70. LLBufferStream istr(channels, buffer.get());
  71. std::ostringstream ostr;
  72. while (istr.good())
  73. {
  74. char buf[1024]; /* Flawfinder: ignore */
  75. istr.read(buf, sizeof(buf)); /* Flawfinder: ignore */
  76. ostr.write(buf, istr.gcount());
  77. }
  78. mString = ostr.str();
  79. mDone = true;
  80. return STATUS_DONE;
  81. }
  82. // virtual
  83. LLIOPipe::EStatus LLIOFuzz::process_impl(
  84. const LLChannelDescriptors& channels,
  85. buffer_ptr_t& buffer,
  86. bool& eos,
  87. LLSD& context,
  88. LLPumpIO* pump)
  89. {
  90. while(mByteCount)
  91. {
  92. std::vector<U8> data;
  93. data.reserve(10000);
  94. int size = llmin(10000, mByteCount);
  95. std::generate_n(
  96. std::back_insert_iterator< std::vector<U8> >(data),
  97. size,
  98. rand);
  99. buffer->append(channels.out(), &data[0], size);
  100. mByteCount -= size;
  101. }
  102. return STATUS_OK;
  103. }
  104. struct random_ascii_generator
  105. {
  106. random_ascii_generator() {}
  107. U8 operator()()
  108. {
  109. int rv = rand();
  110. rv %= (127 - 32);
  111. rv += 32;
  112. return rv;
  113. }
  114. };
  115. // virtual
  116. LLIOPipe::EStatus LLIOASCIIFuzz::process_impl(
  117. const LLChannelDescriptors& channels,
  118. buffer_ptr_t& buffer,
  119. bool& eos,
  120. LLSD& context,
  121. LLPumpIO* pump)
  122. {
  123. while(mByteCount)
  124. {
  125. std::vector<U8> data;
  126. data.reserve(10000);
  127. int size = llmin(10000, mByteCount);
  128. std::generate_n(
  129. std::back_insert_iterator< std::vector<U8> >(data),
  130. size,
  131. random_ascii_generator());
  132. buffer->append(channels.out(), &data[0], size);
  133. mByteCount -= size;
  134. }
  135. return STATUS_OK;
  136. }
  137. // virtual
  138. LLIOPipe::EStatus LLIONull::process_impl(
  139. const LLChannelDescriptors& channels,
  140. buffer_ptr_t& buffer,
  141. bool& eos,
  142. LLSD& context,
  143. LLPumpIO* pump)
  144. {
  145. return STATUS_OK;
  146. }
  147. // virtual
  148. LLIOPipe::EStatus LLIOSleeper::process_impl(
  149. const LLChannelDescriptors& channels,
  150. buffer_ptr_t& buffer,
  151. bool& eos,
  152. LLSD& context,
  153. LLPumpIO* pump)
  154. {
  155. if(!mRespond)
  156. {
  157. lldebugs << "LLIOSleeper::process_impl() sleeping." << llendl;
  158. mRespond = true;
  159. static const F64 SLEEP_TIME = 2.0;
  160. pump->sleepChain(SLEEP_TIME);
  161. return STATUS_BREAK;
  162. }
  163. lldebugs << "LLIOSleeper::process_impl() responding." << llendl;
  164. LLBufferStream ostr(channels, buffer.get());
  165. ostr << "huh? sorry, I was sleeping." << std::endl;
  166. return STATUS_DONE;
  167. }