Diff

net/server_event.lua @ 3003:48871a549b4c

net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
author Matthew Wild <mwild1@gmail.com>
date Wed, 05 May 2010 15:25:48 +0100
parent 3002:d1f864ed44c0
child 3149:5aca2f01c0f9
line wrap: on
line diff
--- a/net/server_event.lua	Wed May 05 15:24:24 2010 +0100
+++ b/net/server_event.lua	Wed May 05 15:25:48 2010 +0100
@@ -845,11 +845,32 @@
 	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,