llpluginmessagepipe.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @file llpluginmessagepipe.h
  3. * @brief Classes that implement connections from the plugin system to pipes/pumps.
  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_LLPLUGINMESSAGEPIPE_H
  33. #define LL_LLPLUGINMESSAGEPIPE_H
  34. #include "lliosocket.h"
  35. #include "llthread.h"
  36. class LLPluginMessagePipe;
  37. // Inherit from this to be able to receive messages from the LLPluginMessagePipe
  38. class LLPluginMessagePipeOwner
  39. {
  40. protected:
  41. LOG_CLASS(LLPluginMessagePipeOwner);
  42. public:
  43. LLPluginMessagePipeOwner();
  44. virtual ~LLPluginMessagePipeOwner();
  45. // Called with incoming messages
  46. virtual void receiveMessageRaw(const std::string& message) = 0;
  47. // Called when the socket has an error
  48. virtual apr_status_t socketError(apr_status_t error);
  49. // Called from LLPluginMessagePipe to manage the connection with
  50. // LLPluginMessagePipeOwner: do not use !
  51. virtual void setMessagePipe(LLPluginMessagePipe* message_pipe);
  52. protected:
  53. // Returns false if writeMessageRaw() would drop the message
  54. LL_INLINE bool canSendMessage() { return mMessagePipe != NULL; }
  55. // SendS a message over the pipe
  56. bool writeMessageRaw(const std::string& message);
  57. // Closes the pipe
  58. void killMessagePipe();
  59. protected:
  60. LLPluginMessagePipe* mMessagePipe;
  61. apr_status_t mSocketError;
  62. };
  63. class LLPluginMessagePipe
  64. {
  65. protected:
  66. LOG_CLASS(LLPluginMessagePipe);
  67. public:
  68. LLPluginMessagePipe(LLPluginMessagePipeOwner* owner,
  69. LLSocket::ptr_t socket);
  70. virtual ~LLPluginMessagePipe();
  71. // Called when the owner is done with this pipe. The next call to
  72. // process_impl should send any remaining data and exit.
  73. LL_INLINE void clearOwner() { mOwner = NULL; }
  74. bool addMessage(const std::string& message);
  75. bool pump(F64 timeout = 0.0);
  76. bool pumpOutput();
  77. bool pumpInput(F64 timeout = 0.0);
  78. protected:
  79. void processInput();
  80. // Used internally by pump()
  81. void setSocketTimeout(apr_interval_time_t timeout_usec);
  82. protected:
  83. LLPluginMessagePipeOwner* mOwner;
  84. LLSocket::ptr_t mSocket;
  85. LLMutex mInputMutex;
  86. std::string mInput;
  87. LLMutex mOutputMutex;
  88. std::string mOutput;
  89. size_t mOutputStartIndex;
  90. };
  91. #endif // LL_LLPLUGINMESSAGE_H