From 85ef003ed7836cb351ee62ed44a4837a305e9dbd Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Thu, 22 Feb 2024 20:42:08 -0500 Subject: Lua listen_events(), await_event() => get_event_{pumps,next}(). Don't set up a Lua callback to receive incoming events, a la listen_events(). Don't listen on an arbitrary event pump, a la await_event(). Instead, the new get_event_pumps() entry point simply delivers the reply pump and command pump names (as listen_events() did) without storing a Lua callback. Make LuaListener capture incoming events on the reply pump in a queue. This avoids the problem of multiple events arriving too quickly for the Lua script to retrieve. If the queue gets too big, discard the excess instead of blocking the caller of post(). Then the new get_event_next() entry point retrieves the next (pump, data) pair from the queue, blocking the Lua script until a suitable event arrives. This is closer to the use of stdin for a LEAP plugin. It also addresses the question: what should the Lua script's C++ coroutine do while waiting for an incoming reply pump event? Recast llluamanager_test.cpp for this new, more straightforward API. Move LLLeap's and LuaListener's reply LLEventPump into LLLeapListener, which they both use. This simplifies LLLeapListener's API, which was a little convoluted: the caller supplied a connect callback to allow LLLeapListener to connect some listener to the caller's reply pump. Now, instead, the caller simply passes a bool(pumpname, data) callback to receive events incoming on LLLeapListener's own reply pump. Fix a latent bug in LLLeapListener: if a plugin called listen() more than once with the same listener name, the new connection would not have been saved. While at it, replace some older Boost features in LLLeapListener and LLLeap. --- indra/llcommon/lualistener.h | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'indra/llcommon/lualistener.h') diff --git a/indra/llcommon/lualistener.h b/indra/llcommon/lualistener.h index df88d55dd5..d70d500221 100644 --- a/indra/llcommon/lualistener.h +++ b/indra/llcommon/lualistener.h @@ -14,7 +14,10 @@ #include "llevents.h" #include "llinstancetracker.h" +#include "llsd.h" +#include "llthreadsafequeue.h" #include "lluuid.h" +#include #include // std::unique_ptr #ifdef LL_TEST @@ -31,7 +34,6 @@ class LLLeapListener; * inconvenience malicious Lua scripts wanting to mess with others. The idea * is that a given lua_State stores in its Registry: * - "event.listener": the int key of the corresponding LuaListener, if any - * - "event.function": the Lua function to be called with incoming events * The original thought was that LuaListener would itself store the Lua * function -- but surprisingly, there is no C/C++ type in the API that stores * a Lua function. @@ -55,22 +57,25 @@ public: ~LuaListener(); - std::string getReplyName() const { return mReplyPump.getName(); } + std::string getReplyName() const; std::string getCommandName() const; + /** + * LuaListener enqueues reply events from its LLLeapListener on mQueue. + * Call getNext() to retrieve the next such event. Blocks the calling + * coroutine if the queue is empty. + */ + using PumpData = std::pair; + PumpData getNext(); + + friend std::ostream& operator<<(std::ostream& out, const LuaListener& self); + private: static int getUniqueKey(); + bool queueEvent(const std::string& pump, const LLSD& data); - static LLBoundListener connect(lua_State* L, LLEventPump& pump, const std::string& listener); + LLThreadSafeQueue mQueue; - static bool call_lua(lua_State* L, const std::string& pump, const LLSD& data); - -#ifndef LL_TEST - LLEventStream mReplyPump{ LLUUID::generateNewID().asString() }; -#else - LLEventLogProxyFor mReplyPump{ "luapump", false }; -#endif - LLTempBoundListener mReplyConnection; std::unique_ptr mListener; }; -- cgit v1.2.3 From 43b039dc7f83702fd2da9cb4fad64965d3f15f74 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Fri, 23 Feb 2024 09:54:48 -0500 Subject: Clean up #includes in lualistener.h --- indra/llcommon/lualistener.h | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'indra/llcommon/lualistener.h') diff --git a/indra/llcommon/lualistener.h b/indra/llcommon/lualistener.h index d70d500221..c13b7bbd5f 100644 --- a/indra/llcommon/lualistener.h +++ b/indra/llcommon/lualistener.h @@ -12,17 +12,13 @@ #if ! defined(LL_LUALISTENER_H) #define LL_LUALISTENER_H -#include "llevents.h" #include "llinstancetracker.h" #include "llsd.h" #include "llthreadsafequeue.h" -#include "lluuid.h" -#include +#include // std::ostream #include // std::unique_ptr - -#ifdef LL_TEST -#include "lleventfilter.h" -#endif +#include +#include // std::pair struct lua_State; class LLLeapListener; -- cgit v1.2.3