aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2010-05-05 15:25:48 +0100
committerMatthew Wild <mwild1@gmail.com>2010-05-05 15:25:48 +0100
commita22e2228658189c68d1125f1fb6623088f8bc3cb (patch)
tree9e0f3456c0aacc4ea87eb5d27e29163325f0ba3c /net
parent3c7eb58b08e7f416d73f86061706674d37f83c4f (diff)
downloadprosody-a22e2228658189c68d1125f1fb6623088f8bc3cb.tar.gz
prosody-a22e2228658189c68d1125f1fb6623088f8bc3cb.zip
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Diffstat (limited to 'net')
-rw-r--r--net/server_event.lua21
-rw-r--r--net/server_select.lua23
2 files changed, 44 insertions, 0 deletions
diff --git a/net/server_event.lua b/net/server_event.lua
index 8eaa96da..43e70a0f 100644
--- a/net/server_event.lua
+++ b/net/server_event.lua
@@ -845,11 +845,32 @@ function hook_signal(signal_num, handler)
return signal_events[signal_num];
end
+local function link(sender, receiver, buffersize)
+ sender:set_mode(buffersize);
+ local sender_locked;
+
+ function receiver:ondrain()
+ if sender_locked then
+ sender:resume();
+ sender_locked = nil;
+ end
+ end
+
+ function sender:onincoming(data)
+ receiver:write(data);
+ if receiver.writebufferlen >= buffersize then
+ sender_locked = true;
+ sender:pause();
+ end
+ end
+end
+
return {
cfg = cfg,
base = base,
loop = loop,
+ link = link,
event = event,
event_base = base,
addevent = newevent,
diff --git a/net/server_select.lua b/net/server_select.lua
index 9bc112bb..68ac7091 100644
--- a/net/server_select.lua
+++ b/net/server_select.lua
@@ -675,6 +675,28 @@ closesocket = function( socket )
--mem_free( )
end
+local function link(sender, receiver, buffersize)
+ sender:set_mode(buffersize);
+ local sender_locked;
+ local _sendbuffer = receiver.sendbuffer;
+ function receiver.sendbuffer()
+ _sendbuffer();
+ if sender_locked and receiver.bufferlen() < buffersize then
+ sender:lock_read(false); -- Unlock now
+ sender_locked = nil;
+ end
+ end
+
+ local _readbuffer = sender.readbuffer;
+ function sender.readbuffer()
+ _readbuffer();
+ if not sender_locked and receiver.bufferlen() >= buffersize then
+ sender_locked = true;
+ sender:lock_read(true);
+ end
+ end
+end
+
----------------------------------// PUBLIC //--
addserver = function( addr, port, listeners, pattern, sslctx ) -- this function provides a way for other scripts to reg a server
@@ -898,6 +920,7 @@ return {
wrapclient = wrapclient,
loop = loop,
+ link = link,
stats = stats,
closeall = closeall,
addtimer = addtimer,