marcoskirsch / nodemcu-httpserver

A (very) simple web server written in Lua for the ESP8266 firmware NodeMCU.
GNU General Public License v2.0
397 stars 166 forks source link

Improve usability/factorization of bufferedConnection #52

Open devyte opened 8 years ago

devyte commented 8 years ago

The bufferedConnection code has been pulled out into its own file. This improves usability, but the result needs more work. Specifically, usage depends on a socket onSent() callback, which is outside the bufferedConnection file, and on coroutine creation/handling/destruction code, which are also outside the bufferedConnection file. The ideal buffered connection would have a usage model as close as possible to the current socket usage model, have all the coroutine code hidden away, have a small mem footprint, and trivial to integrate into an application for general use. I would suggest creating something along the lines of another class which wraps the bufferedConnection, maybe called a "threaded" connection, which contains the missing pieces of dependent code. This is just an idea of course, and open for discussion.

devyte commented 8 years ago

I have a solution that encapsulates the bufferedConnection as well as the whole coroutine uglyness. Please see nodemcu-platform, file tbconnection.lua, and httpserver for usage. The encapsulation is a threaded-buffered connection. It is constructed with a normal connection, and has a :run() method that receives as arguments a function and optional arguments to pass to the function. The function passed as argument receives as first argument a buffered connection and the optional arguments passed to run(). Inside of it, you can :send() all you wish, the payloads will be buffered into chunks of threshold sice (currently set to 256 bytes), and then they get flush()ed and physically sent. Example: local function servefunc(bconn, msg) bconn:send(msg) end

local msg = "hello nodemcu" local tbconn = dofile("tbconnection.lc")(connection) tbconn:run(servefunc, msg)

It seems to work, but mem footprint is along the lines of 6K, which is large, so any ideas to reduce it are welcome. @marcoskirsch @pjsg I think it solves this issue, but you'd have to pick it up and integrate it into this repo.