Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

child.hpp 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. // \file child.hpp
  2. //
  3. // Copyright (C) 2014 MicroNeil Research Corporation.
  4. //
  5. // This program is part of the MicroNeil Research Open Library Project. For
  6. // more information go to http://www.microneil.com/OpenLibrary/index.html
  7. //
  8. // This program is free software; you can redistribute it and/or modify it
  9. // under the terms of the GNU General Public License as published by the
  10. // Free Software Foundation; either version 2 of the License, or (at your
  11. // option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful, but WITHOUT
  14. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. // more details.
  17. //
  18. // You should have received a copy of the GNU General Public License along with
  19. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. // Place, Suite 330, Boston, MA 02111-1307 USA
  21. //==============================================================================
  22. /*
  23. \brief The child module provides classes to spawn and communicate
  24. with child processes.
  25. */
  26. #ifndef CHILD_HPP
  27. #define CHILD_HPP
  28. #ifdef _WIN32
  29. #include <windows.h>
  30. #endif
  31. #include <cstdint>
  32. #include <streambuf>
  33. #include <iostream>
  34. #include <string>
  35. #include <vector>
  36. #include <thread>
  37. #include <mutex>
  38. namespace CodeDweller {
  39. /**
  40. \namespace CodeDweller
  41. The CodeDweller namespace contains components providing high-level
  42. functionality for applications.
  43. */
  44. /** Class that abstracts the iostreams communication with a child
  45. process.
  46. This class provides functionality to create a child process,
  47. communicate with the child process via streams and signals, and
  48. obtain the exit code of the child process.
  49. */
  50. class ChildStream : public std::iostream {
  51. private:
  52. /// Streambuf class for reading from the standard output and
  53. /// writing to the standard input of the child process.
  54. class ChildStreambuf : public std::streambuf {
  55. friend class ChildStream;
  56. public:
  57. /// Constructor.
  58. //
  59. // \param[in] bufSize is the size in bytes of the input
  60. // buffer and output buffer.
  61. //
  62. explicit ChildStreambuf(std::size_t bufSize = 4096);
  63. #ifdef _WIN32
  64. /// Set the handle to read the standard output of the child
  65. /// process.
  66. //
  67. // \param[in] inHandle is the input handle for the standard
  68. // output of the child process.
  69. //
  70. void setInputHandle(HANDLE inHandle);
  71. /// Set the handle to write the standard input of the child
  72. /// process.
  73. //
  74. // \param[in] outHandle is the output handle for the standard
  75. // input of the child process.
  76. //
  77. void setOutputHandle(HANDLE outHandle);
  78. #else
  79. /// Set the file descriptor to read the standard output of the
  80. /// child process.
  81. //
  82. // \param[in] inFd is the input file descriptor for the standard
  83. // output of the child process.
  84. //
  85. void setInputFileDescriptor(int inFd);
  86. /// Set the file descriptor to write the standard input of the
  87. /// child process.
  88. //
  89. // \param[in] outFd is the output file descriptor for the
  90. // standard input of the child process.
  91. //
  92. void setOutputFileDescriptor(int outFd);
  93. #endif
  94. private:
  95. /** Return the number of bytes that can be read without
  96. blocking.
  97. This method checks if any input is available from the pipe,
  98. and returns the number of bytes in the input buffer plus 1.
  99. Reading that number of bytes will not block. Reading a
  100. larger number of bytes might block.
  101. \returns minimum number of bytes that can be read without
  102. blocking.
  103. */
  104. size_t numBytesAvailable() const;
  105. /// Override streambuf::underflow().
  106. int_type underflow();
  107. /// Flush the output buffer.
  108. void flushBuffer();
  109. /// Override streambuf::overflow().
  110. int_type overflow(int_type ch);
  111. /// Override streambuf::sync().
  112. int sync();
  113. /// Input and output handles.
  114. #ifdef _WIN32
  115. HANDLE inputHandle;
  116. HANDLE outputHandle;
  117. #else
  118. int inputFileDescriptor;
  119. int outputFileDescriptor;
  120. #endif
  121. /// Size of buffers.
  122. std::size_t bufferSize;
  123. /// Read buffer.
  124. std::vector<char> readBuffer;
  125. /// Write buffer.
  126. std::vector<char> writeBuffer;
  127. /// Copy constructor not implemented.
  128. ChildStreambuf(const ChildStreambuf &) = delete;
  129. /// Assignment operator not implemented.
  130. ChildStreambuf &operator=(const ChildStreambuf &) = delete;
  131. };
  132. /// Stream buffer for reading from the stdout and writing to the
  133. /// stdin of the child process.
  134. ChildStreambuf childStreambuf;
  135. public:
  136. /** Constructor for spawning with command-line parameters.
  137. The constructor configures the object, and spawns the child
  138. process.
  139. \param[in] args contains the child executable file name and
  140. command-line parameters. args[0] contains the full path of the
  141. executable, and args[1] thru args[n] are the command-line
  142. parameters.
  143. \param[in] bufSize is the input and output buffer size of the
  144. stream used to communicate with the child process.
  145. \throws runtime_error if an error occurs.
  146. */
  147. ChildStream(std::vector<std::string> const &args, size_t bufSize = 4096);
  148. /** Constructor for spawning without command-line parameters.
  149. The constructor configures the object, and spawns the child
  150. process.
  151. \param[in] childpath contains the child executable file name.
  152. \param[in] bufSize is the input and output buffer size of the
  153. stream used to communicate with the child process.
  154. \throws runtime_error if an error occurs.
  155. */
  156. ChildStream(std::string const &childpath, size_t bufSize = 4096);
  157. /** Constructor.
  158. The constructor configures the I/O buffers, but doesn't spawn
  159. any child process.
  160. \param[in] bufSize is the input and output buffer size of the
  161. stream used to communicate with the child process.
  162. */
  163. ChildStream(size_t bufSize = 4096);
  164. /** Destructor terminates the child process. */
  165. ~ChildStream();
  166. /** Spawn the child process.
  167. \param[in] args contains the child executable file name and
  168. command-line parameters. args[0] contains the full path of the
  169. executable, and args[1] thru args[n] are the command-line
  170. parameters.
  171. \throws runtime_error if an error occurs.
  172. \throws runtime_error if an error occurs.
  173. */
  174. void open(std::vector<std::string> const &args);
  175. /** Spawn the child process.
  176. \param[in] childpath contains the child executable file name.
  177. \throws runtime_error if an error occurs.
  178. */
  179. void open(std::string const &childpath);
  180. /** Get the number of bytes available for input.
  181. @returns number of bytes that can be read without blocking.
  182. */
  183. size_t numBytesAvailable() const;
  184. /** Check whether the child process is running.
  185. \returns True if the child process is running, false
  186. otherwise.
  187. */
  188. bool isRunning() const;
  189. /** Terminate the child process.
  190. \throws runtime_error if an error occurs.
  191. \throws logic_error if the child process is not running.
  192. */
  193. void close();
  194. /** Check whether the child process has exited.
  195. \returns True if the child process has exited, false
  196. otherwise.
  197. \throws runtime_error if an error occurs.
  198. \throws logic_error if the child process is not running.
  199. */
  200. bool isDone();
  201. /** Get the exit value of the child process.
  202. \returns The exit value of the child process if the child
  203. process has exited.
  204. \throws runtime_error if an error occurs.
  205. \throws logic_error if the child process has not exited.
  206. \throws logic_error if the child process is not running.
  207. */
  208. int32_t result();
  209. private:
  210. /** Spawn the child process.
  211. \throws runtime_error if an error occurs.
  212. */
  213. void run();
  214. /// Exit code to use when terminating the child process.
  215. static const uint32_t terminateExitCode = 0;
  216. /// True if the child process was successfully started.
  217. bool childStarted;
  218. /// True if the child process has exited.
  219. bool childExited;
  220. /// Initialize data members.
  221. void init();
  222. /// Child executable path and command-line parameters.
  223. std::vector<std::string> cmdArgs;
  224. #ifdef _WIN32
  225. /// Child's process handle.
  226. HANDLE childProcess;
  227. /// Child's thread handle.
  228. HANDLE childThread;
  229. #else
  230. /// Child process ID.
  231. pid_t childPid;
  232. #endif
  233. /// Exit value of the process.
  234. int32_t exitCode;
  235. /// True if the exit code has been obtained.
  236. bool exitCodeObtainedFlag;
  237. /// Return text for the most recent error.
  238. //
  239. // \returns Human-readable description of the most recent error.
  240. //
  241. static std::string getErrorText();
  242. };
  243. /** Class that abstracts the non-blocking communication with a child
  244. process.
  245. This class provides functionality to create a child process,
  246. communicate with the child process via non-blocking methods, and
  247. obtain the exit code of the child process.
  248. The existing process can be terminated by the close() method,
  249. and a new process spawned by the open() method.
  250. The close() method calls TerminateProcess under Windows, and
  251. sends SIGKILL under Linux. After sending SIGKILL, the close()
  252. methods calls waitpid() to wait for the child process to exit.
  253. When a child process is spawned, this class creates two threads:
  254. One for writing to the child, and one for reading from the
  255. child. These threads communicate with the user thread via:
  256. <ol>
  257. <li> A shared boolean that indicates when the threads should
  258. stop.</li>
  259. <li> A shared linear buffer containing data to write to the
  260. child process.</li>
  261. <li> A shared circular buffer containing data read from the
  262. child process.</li>
  263. </ol>
  264. */
  265. class Child {
  266. private:
  267. /** Circular buffer of characters. */
  268. class CircularBuffer {
  269. public:
  270. /** Constructor specifies the capacity.
  271. @param[in] maxSize is the capacity of the buffer.
  272. */
  273. CircularBuffer(size_t maxSize);
  274. /** Check whether the container is empty.
  275. @returns true if the container is empty, false otherwise.
  276. This method can be invoked by the user thread without
  277. serializing access to the object with a mutex. The
  278. reasoning is:
  279. 1) The buffer is empty if and only if iBegin == iEnd.
  280. 2) Only user thread modifies iBegin, by extracting data
  281. from the buffer.
  282. This means that if iBegin == iEnd, then:
  283. 1) iBegin is a valid index, since only the user thread can
  284. modify iBegin. The user thread maintains the validity
  285. of iBegin.
  286. 2) iEnd is a valid index, since it equals iBegin.
  287. 3) The result iBegin == iEnd is also valid, and indicates
  288. whether the buffer is empty.
  289. */
  290. bool empty() const;
  291. /** Get the size.
  292. @returns the number of bytes in the buffer.
  293. */
  294. size_t nUsed() const;
  295. /** Get the available space.
  296. @returns a number of bytes that can be written to the buffer
  297. without overwriting any existing data.
  298. This method can be invoked by the reader thread without
  299. serializing access to the object with a mutex. The reason
  300. is that:
  301. 1) The free space depends on capacity, iBegin, and iEnd.
  302. 2) The capacity is not changed while the threads are
  303. running, and only the reader thread modifies iEnd.
  304. Therefore, the reader thread always sees a valid and
  305. up-to-date value for capacity and iEnd.
  306. 3) Because the user thread modifies iBegin, iBegin might
  307. be invalid. The only invalid value is capacity + 1, in
  308. which case the correct value of iBegin is 0. This method
  309. checks for the invalid value, and uses the correct
  310. value as needed.
  311. 4) Because the user thread modifies iBegin, iBegin might
  312. be out-of-date. Because the user thread only
  313. increments iBegin, an out-of-date value would result in
  314. a smaller value of the available space in the buffer.
  315. */
  316. size_t nFree() const;
  317. /** Clear the buffer. */
  318. void clear();
  319. /** Put bytes to the buffer.
  320. @param[in] ptr is the address of the first byte to put.
  321. @param[in] nBytes is the number of bytes to put.
  322. @warning The capacity of the buffer must not be exceeded;
  323. exceeding the capacity corrupts the buffer.
  324. */
  325. void put(char const *ptr, size_t nBytes);
  326. /** Get bytes from the buffer.
  327. This method gets the specified number of bytes from the
  328. buffer, and erases those bytes from the buffer.
  329. The type T must have the following methods:
  330. <ol>
  331. <li>clear().</li>
  332. <li>capacity().</li>
  333. <li>reserve().</li>
  334. <li>push_back(char).</li>
  335. </ol>
  336. Both std::vector<char> and std::string can be used for T.
  337. @param[out] buf receives the data. The contents of buf are
  338. replaced with the data in the circular buffer.
  339. @param[in] nBytes is the number of bytes to get. Specifying
  340. a value of zero for nBytes gets and erases all the data.
  341. */
  342. template<typename T>
  343. void getAndErase(T &buf, size_t nBytes) {
  344. if ( (0 == nBytes) || (nBytes > nUsed()) ) {
  345. nBytes = nUsed();
  346. }
  347. buf.clear();
  348. if (buf.capacity() < nBytes) {
  349. buf.reserve(nBytes);
  350. }
  351. for (size_t i = 0; i < nBytes; i++) {
  352. buf.push_back(buffer[iBegin]);
  353. nextIndex(iBegin);
  354. }
  355. }
  356. /** Check whether specified data is present in the buffer.
  357. This method check whether the specified data is present in
  358. the buffer, and provides the number of characters before the
  359. specified data.
  360. The type U must have the following methods:
  361. <ol>
  362. <li>Methods required by CircularBuffer::match().</li>
  363. <li>size().</li>
  364. </ol>
  365. Both std::vector<char> and std::string can be used for T.
  366. @param[in] data is the specified data.
  367. @param[out] nBytesBefore is the number of bytes before the
  368. specified data.
  369. @returns true if the data is present in the buffer, false
  370. otherwise.
  371. @see CircularBuffer::match().
  372. */
  373. template<typename U>
  374. bool dataIsPresent(U const &data, size_t &nBytesBefore) const {
  375. size_t iLocalBegin = iBegin;
  376. if (nUsed() < data.size()) {
  377. return false;
  378. }
  379. size_t nToCheck = nUsed() - data.size() + 1;
  380. for (nBytesBefore = 0; nBytesBefore < nToCheck; nBytesBefore++) {
  381. if (match(data, iLocalBegin)) {
  382. return true;
  383. }
  384. nextIndex(iLocalBegin);
  385. }
  386. return false;
  387. }
  388. private:
  389. /** Increment the index.
  390. @param[in] index is the index to increment.
  391. */
  392. void nextIndex(size_t &index) const {
  393. index++;
  394. if (index >= capacity + 1)
  395. index = 0;
  396. }
  397. /** Check whether the specified data is in the buffer.
  398. This method checks whether the specified data is present in
  399. the buffer starting at the specified location.
  400. The type U must have the following methods:
  401. <ol>
  402. <li>operator[].</li>
  403. <li>size().</li>
  404. </ol>
  405. @param[in] data is the specified data.
  406. @param[in] index is the index of the buffer to start
  407. checking for the specified data.
  408. @returns true if the specified data is in the buffer, false
  409. otherwise.
  410. */
  411. template<typename U>
  412. bool match(U &data, size_t indx) const {
  413. for (size_t i = 0; i < data.size(); i++) {
  414. if (iEnd == indx) {
  415. return false;
  416. }
  417. if (data[i] != buffer[indx]) {
  418. return false;
  419. }
  420. nextIndex(indx);
  421. }
  422. return true;
  423. }
  424. /// Buffer to hold data.
  425. std::vector<char> buffer;
  426. /// Capacity of the buffer.
  427. size_t capacity;
  428. /// Index of first element.
  429. size_t iBegin;
  430. /// Index of one past the last element.
  431. size_t iEnd;
  432. };
  433. public:
  434. /** Constructor for spawning with command-line parameters.
  435. The constructor configures the object, and spawns the child
  436. process.
  437. \param[in] args contains the child executable file name and
  438. command-line parameters. args[0] contains the full path of the
  439. executable, and args[1] thru args[n] are the command-line
  440. parameters.
  441. \param[in] bufSize is the input and output buffer size of the
  442. stream used to communicate with the child process.
  443. \param[in] nominalAboveMinPollTime_ms is used to determine the
  444. minimum time in milliseconds that the writer thread sleeps
  445. when there's no data in the output buffer, and that the reader
  446. thread sleeps when there's no room in the input buffer. The
  447. minimum time is nominalAboveMinPollTime_ms +
  448. CodeDweller::MinimumSleeperTime.
  449. \param[in] deltaPollTime_ms is how much longer, in
  450. milliseconds, the maximum time to sleep is than the minimum time.
  451. \throws runtime_error if an error occurs.
  452. */
  453. Child(std::vector<std::string> const &args,
  454. size_t bufSize = 128 * 1024,
  455. std::uint16_t nominalAboveMinPollTime_ms = 0,
  456. std::uint16_t deltaPollTime_ms = 500);
  457. /** Constructor for spawning without command-line parameters.
  458. The constructor configures the object, and spawns the child
  459. process.
  460. \param[in] childpath contains the child executable file name.
  461. \param[in] bufSize is the input and output buffer size of the
  462. stream used to communicate with the child process.
  463. \param[in] nominalAboveMinPollTime_ms is used to determine the
  464. minimum time in milliseconds that the writer thread sleeps
  465. when there's no data in the output buffer, and that the reader
  466. thread sleeps when there's no room in the input buffer. The
  467. minimum time is nominalAboveMinPollTime_ms +
  468. CodeDweller::MinimumSleeperTime.
  469. \param[in] deltaPollTime_ms is how much longer, in
  470. milliseconds, the maximum time to sleep is than the minimum time.
  471. \throws runtime_error if an error occurs.
  472. */
  473. Child(std::string const &childpath,
  474. size_t bufSize = 128 * 1024,
  475. std::uint16_t nominalAboveMinPollTime_ms = 0,
  476. std::uint16_t deltaPollTime_ms = 500);
  477. /** Constructor.
  478. The constructor configures the I/O buffers, but doesn't spawn
  479. any child process.
  480. \param[in] bufSize is the input and output buffer size of the
  481. stream used to communicate with the child process.
  482. \param[in] nominalAboveMinPollTime_ms is used to determine the
  483. minimum time in milliseconds that the writer thread sleeps
  484. when there's no data in the output buffer, and that the reader
  485. thread sleeps when there's no room in the input buffer. The
  486. minimum time is nominalAboveMinPollTime_ms +
  487. CodeDweller::MinimumSleeperTime.
  488. \param[in] deltaPollTime_ms is how much longer, in
  489. milliseconds, the maximum time to sleep is than the minimum time.
  490. */
  491. Child(size_t bufSize = 4096,
  492. std::uint16_t nominalAboveMinPollTime_ms = 0,
  493. std::uint16_t deltaPollTime_ms = 500);
  494. /** Destructor terminates the child process. */
  495. ~Child();
  496. /** Spawn the child process.
  497. \param[in] args contains the child executable file name and
  498. command-line parameters. args[0] contains the full path of the
  499. executable, and args[1] thru args[n] are the command-line
  500. parameters.
  501. \throws runtime_error if an error occurs.
  502. \throws runtime_error if an error occurs.
  503. */
  504. void open(std::vector<std::string> const &args);
  505. /** Spawn the child process.
  506. \param[in] childpath contains the child executable file name.
  507. \throws runtime_error if an error occurs.
  508. */
  509. void open(std::string const &childpath);
  510. /** All-or-nothing non-blocking queue write request to the child.
  511. This methods attempts to queue a write request consisting of
  512. the entire contents of a container.
  513. The type T must have the following methods:
  514. <ol>
  515. <li>begin().</li>
  516. <li>end().</li>
  517. <li>size().</li>
  518. </ol>
  519. Both std::vector<char> and std::string can be used for T.
  520. @param[in] data is the data to queue.
  521. @returns true if the write request was queued, false
  522. otherwise.
  523. */
  524. template<typename T>
  525. bool write(T const &data) {
  526. if (!isRunning()) {
  527. throw std::logic_error("No child process is running.");
  528. }
  529. // The free space in the write buffer can be checked without
  530. // locking the mutex because:
  531. //
  532. // 1) bufferCapacity is unchanging, and
  533. //
  534. // 2) nWriteBytes is only decreased by the writer thread.
  535. //
  536. // This means that the calculated free space can only increase
  537. // by the action of the writer thread; the calculated free space
  538. // is a minimum value. In the worst case, this method would
  539. // return false unnecessarily.
  540. if (data.size() > bufferCapacity - nWriteBytes) {
  541. return false;
  542. }
  543. std::lock_guard<std::mutex> lock(writeBufferMutex);
  544. std::copy(data.begin(),
  545. data.end(),
  546. &(writeBuffer[nWriteBytes]));
  547. nWriteBytes += data.size();
  548. return true;
  549. }
  550. /** All-or-nothing non-blocking queue write request to the child.
  551. This methods attempts to queue a write request consisting of
  552. the entire contents of string literal.
  553. @param[in] data points to the string literal.
  554. @returns true if the write request was queued, false
  555. otherwise.
  556. */
  557. bool write(char const *data) {
  558. return write(std::string(data));
  559. }
  560. /** Non-blocking queue write request to the child.
  561. This methods attempts to queue a write request consisting of
  562. the the specified contents of a container. The number of
  563. bytes queued is the smaller of the size of the container and
  564. the number of free bytes in the output buffer.
  565. The type T must have the following methods:
  566. <ol>
  567. <li>begin().</li>
  568. <li>end().</li>
  569. <li>size().</li>
  570. </ol>
  571. Both std::vector<char> and std::string can be used for T.
  572. @param[in] data is the data to queue.
  573. @param[in] nBytes is the requested number of bytes to queue.
  574. @returns the number of bytes queued.
  575. */
  576. template<typename T>
  577. size_t write(T const &data, size_t nBytes) {
  578. if (!isRunning()) {
  579. throw std::logic_error("No child process is running.");
  580. }
  581. // See the comment above regarding checking the free space in the
  582. // write buffer without locking the mutex.
  583. size_t nFree = bufferCapacity - nWriteBytes;
  584. if (nBytes > nFree) {
  585. nBytes = nFree;
  586. }
  587. if (nBytes > data.size()) {
  588. nBytes = data.size();
  589. }
  590. std::lock_guard<std::mutex> lock(writeBufferMutex);
  591. std::copy(data.begin(),
  592. data.begin() + nBytes,
  593. &(writeBuffer[nWriteBytes]));
  594. nWriteBytes += nBytes;
  595. return nBytes;
  596. }
  597. /** Non-blocking queue write request to the child.
  598. This methods attempts to queue a write request consisting of
  599. as much as possible of the contents of the string.
  600. The type T must have the following methods:
  601. <ol>
  602. <li>begin().</li>
  603. <li>size().</li>
  604. </ol>
  605. Both std::vector<char> and std::string can be used for T.
  606. @param[in, out] data on input is the data to queue. On
  607. output, data contains the data that was not queued.
  608. @returns the number of bytes queued.
  609. */
  610. template<typename T>
  611. size_t writeAndShrink(T &data) {
  612. if (!isRunning()) {
  613. throw std::logic_error("No child process is running.");
  614. }
  615. // See the comment above regarding checking the free space in the
  616. // write buffer without locking the mutex.
  617. size_t nFree = bufferCapacity - nWriteBytes;
  618. if (0 == nFree) {
  619. return 0;
  620. }
  621. std::lock_guard<std::mutex> lock(writeBufferMutex);
  622. size_t nBytesToCopy = data.size();
  623. if (nBytesToCopy > nFree) {
  624. nBytesToCopy = nFree;
  625. }
  626. std::copy(data.begin(),
  627. data.begin() + nBytesToCopy,
  628. &(writeBuffer[nWriteBytes]));
  629. nWriteBytes += nBytesToCopy;
  630. data.erase(data.begin(), data.begin() + nBytesToCopy);
  631. return nBytesToCopy;
  632. }
  633. /** Check if all queued data was transmitted.
  634. @returns true if all the queued data was transmitted to the
  635. child, false otherwise.
  636. */
  637. bool isFinishedWriting() const;
  638. /** Non-blocking request to get data read from the child.
  639. This method attempts to get up to a specified number of bytes
  640. of data from the input buffer containing data received from
  641. the child. The data that is provided is erased from the input
  642. buffer.
  643. The type T must have the following methods:
  644. <ol>
  645. <li>Methods required by CircularBuffer::getAndErase().</li>
  646. <li>size().</li>
  647. </ol>
  648. Both std::vector<char> and std::string can be used for T.
  649. @param[out] data contains the copied data.
  650. @param[in] nBytes is the number of bytes to attempt to copy.
  651. If nBytes is zero, the contents of the entire input buffer is
  652. moved to data.
  653. @returns the number of bytes copied.
  654. @see CircularBuffer::getAndErase().
  655. */
  656. template<typename T>
  657. size_t read(T &data, size_t nBytes = 0) {
  658. if (!isRunning()) {
  659. throw std::logic_error("No child process is running.");
  660. }
  661. data.clear();
  662. // Can be called in the user thread without locking the mutex.
  663. if (readBuffer.empty()) {
  664. return 0;
  665. }
  666. std::lock_guard<std::mutex> lock(readBufferMutex);
  667. size_t nBytesToRead = nBytes;
  668. if (nBytesToRead > readBuffer.nUsed()) {
  669. nBytesToRead = readBuffer.nUsed();
  670. }
  671. readBuffer.getAndErase(data, nBytesToRead);
  672. return data.size();
  673. }
  674. /** Non-blocking request to get data up to a delimiter read from
  675. the child.
  676. This method check whether the specified delimiter is in the
  677. data received from the child.
  678. This method attempts to get data up to and not including a
  679. specified delimiter from the input buffer containing data
  680. received from the child. The data that is provided and the
  681. delimiter are erased from the input buffer.
  682. The type T must have the following methods:
  683. <ol>
  684. <li>Methods required by CircularBuffer::getAndErase().</li>
  685. <li>size().</li>
  686. <li>clear().</li>
  687. </ol>
  688. The type U must have the following methods:
  689. <ol>
  690. <li>Methods required by CircularBuffer::dataIsPresent().</li>
  691. <li>size().</li>
  692. <li>empty().</li>
  693. </ol>
  694. Both std::vector<char> and std::string can be used for T and U.
  695. @param[out] data contains the copied data.
  696. @param[in] delimiter contains the delimiter.
  697. @returns the number of bytes copied.
  698. @see CircularBuffer::getAndErase().
  699. @see CircularBuffer::dataIsPresent().
  700. */
  701. template<typename T, typename U>
  702. bool readDelimited(T &data, U const &delimiter) {
  703. if (!isRunning()) {
  704. throw std::logic_error("No child process is running.");
  705. }
  706. data.clear();
  707. // Can be called in the user thread without locking the mutex.
  708. if (readBuffer.empty()) {
  709. return false;
  710. }
  711. // Empty delimiter always matches.
  712. if (delimiter.empty()) {
  713. return true;
  714. }
  715. std::lock_guard<std::mutex> lock(readBufferMutex);
  716. size_t nBytesToRead;
  717. // Check for the presence of the delimiter.
  718. if (!readBuffer.dataIsPresent(delimiter, nBytesToRead)) {
  719. return false;
  720. }
  721. // Read the data.
  722. if (nBytesToRead > 0) {
  723. readBuffer.getAndErase(data, nBytesToRead);
  724. }
  725. // Discard the delimiter.
  726. T temp;
  727. readBuffer.getAndErase(temp, delimiter.size());
  728. return true;
  729. }
  730. /** Non-blocking request to get data up to a delimiter read from
  731. the child.
  732. This method check whether the specified delimiter is in the
  733. data received from the child.
  734. This method attempts to get data up to and not including a
  735. specified delimiter from the input buffer containing data
  736. received from the child. The data that is provided and the
  737. delimiter are erased from the input buffer.
  738. The type T must have the following methods:
  739. <ol>
  740. <li>Methods required by CircularBuffer::getAndErase().</li>
  741. <li>size().</li>
  742. <li>clear().</li>
  743. </ol>
  744. Both std::vector<char> and std::string can be used for T.
  745. @param[out] data contains the copied data.
  746. @param[in] delimiter contains the delimiter.
  747. @returns the number of bytes copied.
  748. @see CircularBuffer::getAndErase().
  749. */
  750. template<typename T>
  751. bool readDelimited(T &data, char const *delimiter) {
  752. return readDelimited(data, std::string(delimiter));
  753. }
  754. /** Check whether the child process is running.
  755. \returns True if the child process is running, false
  756. otherwise.
  757. */
  758. bool isRunning() const;
  759. /** Check error status.
  760. This method checks whether an error occurred when
  761. communicating with the child process.
  762. \param[out] errorDescription contains the description of any
  763. error that occurred.
  764. \returns true if an error occurred, false otherwise.
  765. */
  766. bool errorOccurred(std::string &errorDescription) const;
  767. /** Close the child process' standard input connection. */
  768. void closeStdIn();
  769. /** Close the connection.
  770. This method terminate the child process if it is running, and
  771. resets the object. After this method is invoked, open() can
  772. be invoked to spawn and communicate with another child
  773. process.
  774. \throws runtime_error if an error occurs.
  775. \throws logic_error if the child process is not running.
  776. */
  777. void close();
  778. /** Check whether the child process has exited.
  779. \returns True if the child process has exited, false
  780. otherwise.
  781. \throws runtime_error if an error occurs.
  782. \throws logic_error if the child process had not been started.
  783. */
  784. bool isDone();
  785. /** Get the exit value of the child process.
  786. \returns The exit value of the child process if the child
  787. process has exited.
  788. \throws runtime_error if an error occurs.
  789. \throws logic_error if the child process has not exited.
  790. \throws logic_error if the child process had not been started.
  791. */
  792. int32_t result();
  793. private:
  794. /// Initialize data members.
  795. void init();
  796. /** Spawn the child process.
  797. \throws runtime_error if an error occurs.
  798. */
  799. void run();
  800. /** Close the output connection to the child.
  801. \param[in, out] errMesg is updated with a description of any
  802. error that occurs.
  803. \returns true if the connection was closed successfully.
  804. */
  805. bool closeOutput(std::string &errMesg);
  806. /** Close the input connection to the child.
  807. \param[in, out] errMesg is updated with a description of any
  808. error that occurs.
  809. \returns true if the connection was closed successfully.
  810. */
  811. bool closeInput(std::string &errMesg);
  812. /// Reader thread object.
  813. std::thread readerThread;
  814. /// Thread start function to read data from the child.
  815. void readFromChild();
  816. /// Writer thread object.
  817. std::thread writerThread;
  818. /// Thread start function to send data to the child.
  819. void writeToChild();
  820. /// True if readerThread is to stop.
  821. bool stopReaderFlag;
  822. /// True if writerThread is to stop.
  823. bool stopWriterFlag;
  824. /// True if the reader thread is running, false otherwise.
  825. bool readerThreadIsRunning;
  826. /// True if the writer thread is running, false otherwise.
  827. bool writerThreadIsRunning;
  828. /// Description of any error.
  829. std::string errorText;
  830. /// Input and output handles.
  831. #ifdef _WIN32
  832. HANDLE inputHandle;
  833. HANDLE outputHandle;
  834. #else
  835. int inputFileDescriptor;
  836. int outputFileDescriptor;
  837. #endif
  838. /// Capacity of buffers.
  839. std::size_t bufferCapacity;
  840. /// Read buffer.
  841. CircularBuffer readBuffer;
  842. /// Mutex to serialize access to readBuffer.
  843. std::mutex readBufferMutex;
  844. /// Write buffer.
  845. std::vector<char> writeBuffer;
  846. /// Number of bytes in writeBuffer.
  847. size_t nWriteBytes;
  848. /// Mutex to serialize access to writeBuffer.
  849. std::mutex writeBufferMutex;
  850. /// Number of bytes in writer thread transmit buffer.
  851. size_t nTransmitBytes;
  852. /// Nominal poll time.
  853. //
  854. // If there isn't room in readBuffer, readerThread aperiodically
  855. // checks whether room is available. The check times are
  856. // determined by a CodeDweller::PollTimer object, which requires a
  857. // nominal poll time and a maximum poll time.
  858. int nominalPollTime_ms;
  859. /// Maximum poll time.
  860. int maximumPollTime_ms;
  861. /// Exit code to use when terminating the child process.
  862. static const uint32_t terminateExitCode = 0;
  863. /// True if the child process was successfully started.
  864. bool childStarted;
  865. /// True if the child process has exited.
  866. bool childExited;
  867. /// Child executable path and command-line parameters.
  868. std::vector<std::string> cmdArgs;
  869. #ifdef _WIN32
  870. /// Child's process handle.
  871. HANDLE childProcess;
  872. /// Child's thread handle.
  873. HANDLE childThread;
  874. #else
  875. /// Child process ID.
  876. pid_t childPid;
  877. #endif
  878. /// Exit value of the process.
  879. int32_t exitCode;
  880. /// True if the exit code has been obtained.
  881. bool exitCodeObtainedFlag;
  882. /// Return text for the most recent error.
  883. //
  884. // \returns Human-readable description of the most recent error.
  885. //
  886. static std::string getErrorText();
  887. };
  888. }
  889. #endif // CHILD_HPP