]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: hlua: add AppletTCP:try_receive()
authorAurelien DARRAGON <adarragon@haproxy.com>
Mon, 31 Mar 2025 11:55:35 +0000 (13:55 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Thu, 3 Apr 2025 15:52:39 +0000 (17:52 +0200)
This is the non-blocking variant for AppletTCP:receive(). It doesn't
take any argument, instead it tries to read as much data as available
at once. If no data is available, empty string is returned.

Lua documentation was updated.

doc/lua-api/index.rst
src/hlua.c

index 4231e32e15f79237bc49307a6629383df8715f40..a283da10a70e52223cc94734fce639e3af8c089a 100644 (file)
@@ -3912,6 +3912,15 @@ AppletTCP class
   :returns: always return a string, the string can be empty if the connection is
    closed.
 
+.. js:function:: AppletTCP.try_receive(applet)
+
+  Reads available data from the TCP stream and returns immediately. Returns a
+  string containing read bytes that may possibly be empty if no bytes are
+  available at that time.
+
+  :param class_AppletTCP applet: An :ref:`applettcp_class`
+  :returns: always return a string, the string can be empty.
+
 .. js:function:: AppletTCP.send(appletmsg)
 
   Send the message on the stream.
index d6ad441a036e24fc46a27047e8b03a464e127ade..30f23c764746d95b23c13ecf240bdc5dad639b2e 100644 (file)
@@ -5464,6 +5464,34 @@ __LJMP static int hlua_applet_tcp_recv(lua_State *L)
        return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
 }
 
+/* Check arguments for the function "hlua_channel_get_yield". */
+__LJMP static int hlua_applet_tcp_try_recv(lua_State *L)
+{
+       struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
+       int ret;
+
+       if (lua_gettop(L) > 1)
+               WILL_LJMP(luaL_error(L, "The 'try_recv' function only expects applet argument."));
+
+       /* Confirm or set the required length */
+       lua_pushinteger(L, -1);
+
+       /* set the expiration date (mandatory arg but not relevant here) */
+       lua_pushinteger(L, TICK_ETERNITY);
+
+       /* Initialise the string catenation. */
+       luaL_buffinit(L, &luactx->b);
+
+       ret = hlua_applet_tcp_recv_try(L);
+       if (!ret) {
+               /* return the result (may be empty). */
+               luaL_pushresult(&luactx->b);
+               return 1;
+       }
+
+       return ret;
+}
+
 /* Append data in the output side of the buffer. This data is immediately
  * sent. The function returns the amount of data written. If the buffer
  * cannot contain the data, the function yields. The function returns -1
@@ -14289,6 +14317,7 @@ lua_State *hlua_init_state(int thread_num)
        /* Register Lua functions. */
        hlua_class_function(L, "getline",   hlua_applet_tcp_getline);
        hlua_class_function(L, "receive",   hlua_applet_tcp_recv);
+       hlua_class_function(L, "try_receive", hlua_applet_tcp_try_recv);
        hlua_class_function(L, "send",      hlua_applet_tcp_send);
        hlua_class_function(L, "set_priv",  hlua_applet_tcp_set_priv);
        hlua_class_function(L, "get_priv",  hlua_applet_tcp_get_priv);