Comparison

util/httpstream.lua @ 3565:e2cc09e83a3e

util.httpstream: A little refactoring of the coroutine control flow.
author Waqas Hussain <waqas20@gmail.com>
date Sat, 06 Nov 2010 01:08:30 +0500
parent 3564:90f4e6dc1c11
child 3566:75d287daad16
comparison
equal deleted inserted replaced
3564:90f4e6dc1c11 3565:e2cc09e83a3e
5 local deadroutine = coroutine.create(function() end); 5 local deadroutine = coroutine.create(function() end);
6 coroutine.resume(deadroutine); 6 coroutine.resume(deadroutine);
7 7
8 module("httpstream") 8 module("httpstream")
9 9
10 local function parser(data, success_cb, parser_type) 10 local function parser(success_cb, parser_type)
11 local data = coroutine.yield();
11 local function readline() 12 local function readline()
12 local pos = data:find("\r\n", nil, true); 13 local pos = data:find("\r\n", nil, true);
13 while not pos do 14 while not pos do
14 data = data..coroutine.yield(); 15 data = data..coroutine.yield();
15 pos = data:find("\r\n", nil, true); 16 pos = data:find("\r\n", nil, true);
92 else coroutine.yield("unknown-parser-type"); end 93 else coroutine.yield("unknown-parser-type"); end
93 end 94 end
94 95
95 function new(success_cb, error_cb, parser_type) 96 function new(success_cb, error_cb, parser_type)
96 local co = coroutine.create(parser); 97 local co = coroutine.create(parser);
98 coroutine.resume(co, success_cb, parser_type)
97 return { 99 return {
98 feed = function(self, data) 100 feed = function(self, data)
99 if not data then 101 if not data then
100 if parser_type == "client" then coroutine.resume(co, "", success_cb, parser_type); end 102 if parser_type == "client" then coroutine.resume(co, ""); end
101 co = deadroutine; 103 co = deadroutine;
102 return error_cb(); 104 return error_cb();
103 end 105 end
104 local success, result = coroutine.resume(co, data, success_cb, parser_type); 106 local success, result = coroutine.resume(co, data);
105 if result then 107 if result then
106 co = deadroutine; 108 co = deadroutine;
107 return error_cb(result); 109 return error_cb(result);
108 end 110 end
109 end; 111 end;