net/cqueues.lua
author Kim Alvefur <zash@zash.se>
Fri, 27 May 2022 14:45:35 +0200
branch0.12
changeset 12530 252ed01896dd
parent 11003 37b884d675f7
child 12978 ba409c67353b
permissions -rw-r--r--
mod_smacks: Bounce unhandled stanzas from local origin (fix #1759) Sending stanzas with a remote session as origin when the stanzas have a local JID in the from attribute trips validation in core.stanza_router, leading to warnings: > Received a stanza claiming to be from remote.example, over a stream authed for localhost.example Using module:send() uses the local host as origin, which is fine here.

-- Prosody IM
-- Copyright (C) 2014 Daurnimator
--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
-- This module allows you to use cqueues with a net.server mainloop
--

local server = require "net.server";
local cqueues = require "cqueues";
local timer = require "util.timer";
assert(cqueues.VERSION >= 20150113, "cqueues newer than 20150113 required")

-- Create a single top level cqueue
local cq;

if server.cq then -- server provides cqueues object
	cq = server.cq;
elseif server.watchfd then
	cq = cqueues.new();
	local timeout = timer.add_task(cq:timeout() or 0, function ()
		-- FIXME It should be enough to reschedule this timeout instead of replacing it, but this does not work.  See https://issues.prosody.im/1572
		assert(cq:loop(0));
		return cq:timeout();
	end);
	server.watchfd(cq:pollfd(), function ()
		assert(cq:loop(0));
		local t = cq:timeout();
		if t then
			timer.stop(timeout);
			timeout = timer.add_task(cq:timeout(), function ()
				assert(cq:loop(0));
				return cq:timeout();
			end);
		end
	end);
else
	error "NYI"
end

return {
	cq = cq;
}