llpluginprocesschild.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @file llpluginprocesschild.h
  3. * @brief LLPluginProcessChild handles the child side of the external-process plugin API.
  4. *
  5. * $LicenseInfo:firstyear=2008&license=viewergpl$
  6. *
  7. * Copyright (c) 2008-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLPLUGINPROCESSCHILD_H
  33. #define LL_LLPLUGINPROCESSCHILD_H
  34. #include <map>
  35. #include <queue>
  36. #include "llhost.h"
  37. #include "llplugininstance.h"
  38. #include "llpluginmessage.h"
  39. #include "llpluginmessagepipe.h"
  40. #include "llpluginsharedmemory.h"
  41. #include "lltimer.h"
  42. class LLPluginProcessChild : public LLPluginMessagePipeOwner,
  43. public LLPluginInstanceMessageListener
  44. {
  45. protected:
  46. LOG_CLASS(LLPluginProcessChild);
  47. public:
  48. enum EState
  49. {
  50. STATE_UNINITIALIZED,
  51. STATE_INITIALIZED, // init() has been called
  52. STATE_CONNECTED, // connected back to launcher
  53. STATE_PLUGIN_LOADING, // plugin library needs to be loaded
  54. STATE_PLUGIN_LOADED, // plugin library has been loaded
  55. STATE_PLUGIN_INITIALIZING, // plugin is processing init message
  56. STATE_RUNNING, // steady state (processing messages)
  57. STATE_SHUTDOWNREQ, // Parent has requested a shutdown.
  58. STATE_UNLOADING, // plugin has sent shutdown_response and needs to be unloaded
  59. STATE_UNLOADED, // plugin has been unloaded
  60. STATE_ERROR, // generic bailout state
  61. STATE_DONE // state machine will sit in this state after either error or normal termination.
  62. };
  63. LLPluginProcessChild();
  64. ~LLPluginProcessChild();
  65. void init(U32 launcher_port);
  66. void idle();
  67. void sleep(F64 seconds);
  68. void pump();
  69. // Returns true if the plugin is in the steady state (processing messages)
  70. LL_INLINE bool isRunning() { return mState == STATE_RUNNING; }
  71. // Returns true if the plugin is unloaded or we're in an unrecoverable
  72. // error state.
  73. LL_INLINE bool isDone() { return mState == STATE_DONE; }
  74. void killSockets();
  75. LL_INLINE F64 getSleepTime() const { return mSleepTime; }
  76. void sendMessageToPlugin(const LLPluginMessage& message);
  77. void sendMessageToParent(const LLPluginMessage& message);
  78. // Inherited from LLPluginMessagePipeOwner
  79. void receiveMessageRaw(const std::string& message) override;
  80. // Inherited from LLPluginInstanceMessageListener
  81. void receivePluginMessage(const std::string& message) override;
  82. private:
  83. void setState(EState state);
  84. void deliverQueuedMessages();
  85. private:
  86. LLPluginInstance* mInstance;
  87. EState mState;
  88. LLHost mLauncherHost;
  89. LLSocket::ptr_t mSocket;
  90. std::string mPluginFile;
  91. std::string mPluginDir;
  92. typedef std::map<std::string, LLPluginSharedMemory*> shared_mem_regions_t;
  93. shared_mem_regions_t mSharedMemoryRegions;
  94. std::queue<std::string> mMessageQueue;
  95. LLTimer mHeartbeat;
  96. LLTimer mWaitGoodbye;
  97. F64 mSleepTime;
  98. F64 mCPUElapsed;
  99. bool mBlockingRequest;
  100. bool mBlockingResponseReceived;
  101. };
  102. #endif // LL_LLPLUGINPROCESSCHILD_H