PageRenderTime 34ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llplugin/llpluginprocessparent.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 194 lines | 114 code | 41 blank | 39 comment | 2 complexity | 9f5389a760a0ca37b045b84c73f9a371 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llpluginprocessparent.h
  3. * @brief LLPluginProcessParent handles the parent side of the external-process plugin API.
  4. *
  5. * @cond
  6. * $LicenseInfo:firstyear=2008&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. * @endcond
  27. */
  28. #ifndef LL_LLPLUGINPROCESSPARENT_H
  29. #define LL_LLPLUGINPROCESSPARENT_H
  30. #include "llapr.h"
  31. #include "llprocesslauncher.h"
  32. #include "llpluginmessage.h"
  33. #include "llpluginmessagepipe.h"
  34. #include "llpluginsharedmemory.h"
  35. #include "lliosocket.h"
  36. #include "llthread.h"
  37. class LLPluginProcessParentOwner
  38. {
  39. public:
  40. virtual ~LLPluginProcessParentOwner();
  41. virtual void receivePluginMessage(const LLPluginMessage &message) = 0;
  42. virtual bool receivePluginMessageEarly(const LLPluginMessage &message) {return false;};
  43. // This will only be called when the plugin has died unexpectedly
  44. virtual void pluginLaunchFailed() {};
  45. virtual void pluginDied() {};
  46. };
  47. class LLPluginProcessParent : public LLPluginMessagePipeOwner
  48. {
  49. LOG_CLASS(LLPluginProcessParent);
  50. public:
  51. LLPluginProcessParent(LLPluginProcessParentOwner *owner);
  52. ~LLPluginProcessParent();
  53. void init(const std::string &launcher_filename,
  54. const std::string &plugin_dir,
  55. const std::string &plugin_filename,
  56. bool debug);
  57. void idle(void);
  58. // returns true if the plugin is on its way to steady state
  59. bool isLoading(void);
  60. // returns true if the plugin is in the steady state (processing messages)
  61. bool isRunning(void);
  62. // returns true if the process has exited or we've had a fatal error
  63. bool isDone(void);
  64. // returns true if the process is currently waiting on a blocking request
  65. bool isBlocked(void) { return mBlocked; };
  66. void killSockets(void);
  67. // Go to the proper error state
  68. void errorState(void);
  69. void setSleepTime(F64 sleep_time, bool force_send = false);
  70. F64 getSleepTime(void) const { return mSleepTime; };
  71. void sendMessage(const LLPluginMessage &message);
  72. void receiveMessage(const LLPluginMessage &message);
  73. // Inherited from LLPluginMessagePipeOwner
  74. /*virtual*/ void receiveMessageRaw(const std::string &message);
  75. /*virtual*/ void receiveMessageEarly(const LLPluginMessage &message);
  76. /*virtual*/ void setMessagePipe(LLPluginMessagePipe *message_pipe) ;
  77. // This adds a memory segment shared with the client, generating a name for the segment. The name generated is guaranteed to be unique on the host.
  78. // The caller must call removeSharedMemory first (and wait until getSharedMemorySize returns 0 for the indicated name) before re-adding a segment with the same name.
  79. std::string addSharedMemory(size_t size);
  80. // Negotiates for the removal of a shared memory segment. It is the caller's responsibility to ensure that nothing touches the memory
  81. // after this has been called, since the segment will be unmapped shortly thereafter.
  82. void removeSharedMemory(const std::string &name);
  83. size_t getSharedMemorySize(const std::string &name);
  84. void *getSharedMemoryAddress(const std::string &name);
  85. // Returns the version string the plugin indicated for the message class, or an empty string if that class wasn't in the list.
  86. std::string getMessageClassVersion(const std::string &message_class);
  87. std::string getPluginVersion(void);
  88. bool getDisableTimeout() { return mDisableTimeout; };
  89. void setDisableTimeout(bool disable) { mDisableTimeout = disable; };
  90. void setLaunchTimeout(F32 timeout) { mPluginLaunchTimeout = timeout; };
  91. void setLockupTimeout(F32 timeout) { mPluginLockupTimeout = timeout; };
  92. F64 getCPUUsage() { return mCPUUsage; };
  93. static void poll(F64 timeout);
  94. static bool canPollThreadRun() { return (sPollSet || sPollsetNeedsRebuild || sUseReadThread); };
  95. static void setUseReadThread(bool use_read_thread);
  96. static bool getUseReadThread() { return sUseReadThread; };
  97. private:
  98. enum EState
  99. {
  100. STATE_UNINITIALIZED,
  101. STATE_INITIALIZED, // init() has been called
  102. STATE_LISTENING, // listening for incoming connection
  103. STATE_LAUNCHED, // process has been launched
  104. STATE_CONNECTED, // process has connected
  105. STATE_HELLO, // first message from the plugin process has been received
  106. STATE_LOADING, // process has been asked to load the plugin
  107. STATE_RUNNING, //
  108. STATE_LAUNCH_FAILURE, // Failure before plugin loaded
  109. STATE_ERROR, // generic bailout state
  110. STATE_CLEANUP, // clean everything up
  111. STATE_EXITING, // Tried to kill process, waiting for it to exit
  112. STATE_DONE //
  113. };
  114. EState mState;
  115. void setState(EState state);
  116. bool pluginLockedUp();
  117. bool pluginLockedUpOrQuit();
  118. bool accept();
  119. LLSocket::ptr_t mListenSocket;
  120. LLSocket::ptr_t mSocket;
  121. U32 mBoundPort;
  122. LLProcessLauncher mProcess;
  123. std::string mPluginFile;
  124. std::string mPluginDir;
  125. LLPluginProcessParentOwner *mOwner;
  126. typedef std::map<std::string, LLPluginSharedMemory*> sharedMemoryRegionsType;
  127. sharedMemoryRegionsType mSharedMemoryRegions;
  128. LLSD mMessageClassVersions;
  129. std::string mPluginVersionString;
  130. LLTimer mHeartbeat;
  131. F64 mSleepTime;
  132. F64 mCPUUsage;
  133. bool mDisableTimeout;
  134. bool mDebug;
  135. bool mBlocked;
  136. bool mPolledInput;
  137. LLProcessLauncher mDebugger;
  138. F32 mPluginLaunchTimeout; // Somewhat longer timeout for initial launch.
  139. F32 mPluginLockupTimeout; // If we don't receive a heartbeat in this many seconds, we declare the plugin locked up.
  140. static bool sUseReadThread;
  141. apr_pollfd_t mPollFD;
  142. static apr_pollset_t *sPollSet;
  143. static bool sPollsetNeedsRebuild;
  144. static LLMutex *sInstancesMutex;
  145. static std::list<LLPluginProcessParent*> sInstances;
  146. static void dirtyPollSet();
  147. static void updatePollset();
  148. void servicePoll();
  149. static LLThread *sReadThread;
  150. LLMutex mIncomingQueueMutex;
  151. std::queue<LLPluginMessage> mIncomingQueue;
  152. };
  153. #endif // LL_LLPLUGINPROCESSPARENT_H