llpipeutil.cpp

Go to the documentation of this file.
00001 
00034 #include "linden_common.h"
00035 #include "llpipeutil.h"
00036 
00037 #include <stdlib.h>
00038 #include <sstream>
00039 
00040 #include "llbufferstream.h"
00041 #include "lldefs.h"
00042 #include "llframetimer.h"
00043 #include "llpumpio.h"
00044 #include "llrand.h"
00045 #include "lltimer.h"
00046 
00047 F32 pump_loop(LLPumpIO* pump, F32 seconds)
00048 {
00049         LLTimer timer;
00050         timer.setTimerExpirySec(seconds);
00051         while(!timer.hasExpired())
00052         {
00053                 LLFrameTimer::updateFrameTime();                        
00054                 pump->pump();
00055                 pump->callback();
00056         }
00057         return timer.getElapsedTimeF32();
00058 }
00059 
00060 //virtual 
00061 LLIOPipe::EStatus LLPipeStringInjector::process_impl(
00062                 const LLChannelDescriptors& channels,
00063                 buffer_ptr_t& buffer,
00064                 bool& eos,
00065                 LLSD& context,
00066                 LLPumpIO* pump)
00067 {
00068         buffer->append(channels.out(), (U8*) mString.data(), mString.size());
00069         eos = true;
00070         return STATUS_DONE;
00071 }
00072 
00073 
00074 LLIOPipe::EStatus LLPipeStringExtractor::process_impl(
00075         const LLChannelDescriptors& channels,
00076     buffer_ptr_t& buffer,
00077     bool& eos,
00078     LLSD& context,
00079     LLPumpIO* pump)
00080 {
00081     if(!eos) return STATUS_BREAK;
00082     if(!pump || !buffer) return STATUS_PRECONDITION_NOT_MET;
00083 
00084         LLBufferStream istr(channels, buffer.get());
00085         std::ostringstream ostr;
00086         while (istr.good())
00087         {
00088                 char buf[1024];         /* Flawfinder: ignore */
00089                 istr.read(buf, sizeof(buf));    /* Flawfinder: ignore */
00090                 ostr.write(buf, istr.gcount());
00091         }
00092         mString = ostr.str();
00093         mDone = true;
00094         
00095         return STATUS_DONE;
00096 }
00097 
00098 
00099 // virtual
00100 LLIOPipe::EStatus LLIOFuzz::process_impl(
00101         const LLChannelDescriptors& channels,
00102         buffer_ptr_t& buffer,
00103         bool& eos,
00104         LLSD& context,
00105         LLPumpIO* pump)
00106 {
00107         while(mByteCount)
00108         {
00109                 std::vector<U8> data;
00110                 data.reserve(10000);
00111                 int size = llmin(10000, mByteCount);
00112                 std::generate_n(
00113                         std::back_insert_iterator< std::vector<U8> >(data),
00114                         size,
00115                         rand);
00116                 buffer->append(channels.out(), &data[0], size);
00117                 mByteCount -= size;
00118         }
00119         return STATUS_OK;
00120 }
00121 
00122 struct random_ascii_generator
00123 {
00124         random_ascii_generator() {}
00125         U8 operator()()
00126         {
00127                 int rv = rand();
00128                 rv %= (127 - 32);
00129                 rv += 32;
00130                 return rv;
00131         }
00132 };
00133 
00134 // virtual
00135 LLIOPipe::EStatus LLIOASCIIFuzz::process_impl(
00136         const LLChannelDescriptors& channels,
00137         buffer_ptr_t& buffer,
00138         bool& eos,
00139         LLSD& context,
00140         LLPumpIO* pump)
00141 {
00142         while(mByteCount)
00143         {
00144                 std::vector<U8> data;
00145                 data.reserve(10000);
00146                 int size = llmin(10000, mByteCount);
00147                 std::generate_n(
00148                         std::back_insert_iterator< std::vector<U8> >(data),
00149                         size,
00150                         random_ascii_generator());
00151                 buffer->append(channels.out(), &data[0], size);
00152                 mByteCount -= size;
00153         }
00154         return STATUS_OK;
00155 }
00156 
00157 // virtual
00158 LLIOPipe::EStatus LLIONull::process_impl(
00159         const LLChannelDescriptors& channels,
00160         buffer_ptr_t& buffer,
00161         bool& eos,
00162         LLSD& context,
00163         LLPumpIO* pump)
00164 {
00165         return STATUS_OK;
00166 }
00167 
00168 // virtual
00169 LLIOPipe::EStatus LLIOSleeper::process_impl(
00170         const LLChannelDescriptors& channels,
00171         buffer_ptr_t& buffer,
00172         bool& eos,
00173         LLSD& context,
00174         LLPumpIO* pump)
00175 {
00176         if(!mRespond)
00177         {
00178                 lldebugs << "LLIOSleeper::process_impl() sleeping." << llendl;
00179                 mRespond = true;
00180                 static const F64 SLEEP_TIME = 2.0;
00181                 pump->sleepChain(SLEEP_TIME);
00182                 return STATUS_BREAK;
00183         }
00184         lldebugs << "LLIOSleeper::process_impl() responding." << llendl;
00185         LLBufferStream ostr(channels, buffer.get());
00186         ostr << "huh? sorry, I was sleeping." << std::endl;
00187         return STATUS_DONE;
00188 }

Generated on Fri May 16 08:34:30 2008 for SecondLife by  doxygen 1.5.5