- Node-lua is a node complementation of lua which supports sync and async rpc, and task-multiplexing support in multi-threads without harmless wakeup.
- It can be used as a simple script engine or complex server engine which supports a massive of independent lua contexts (or named services) running on multi-threads which restricted to the cpu core count.
- The lua context will suspend itself when it calls a sync and async rpc using lua coroutine inside the core c codes.
- The rpc can be called within the lua coroutine where the user creates and it won't impact the normal coroutine procedure.
- A context starts with a lua file as the entry. The process will exit automaticly when all contexts ends or terminates and a context will exit automaticly when all its running and suspending sync and async remote procedure call ends or terminates.
- A optimized task scheduling is used with a thread based context queue which reduces the thread race condition and work-stealing algorithm is used in the task scheduling.
- A more friendly tcp api is embeded in this engine with sync and async implementation which is much more convenient to build a tcp server.
For windows, just open node-lua.sln and build the whole solution. For linux or other unix-like system
git clone https://github.com/socoding/node-lua.git
cd node-lua
make && make install
make release && make install
-
result, listen_socket = tcp.listen(addr, port[, backlog, [listen_callback(result, listen_socket, addr, port[, backlog])]]) --listen_callback is a once callback, blocking if callback is nil.
-
result, listen_socket = tcp.listens(sock_name[, listen_callback(result, listen_socket, sock_name)]) --listen_callback is a once callback, blocking if callback is nil.
-
result, listen_socket = tcp.listen6(addr, port[, backlog, [listen_callback(result, listen_socket, addr, port[, backlog])]]) --listen_callback is a once callback, blocking if callback is nil.
-
result, accept_socket = tcp.accept(listen_socket[, timeout]) --accept_callback is a continues callback, blocking if callback is nil.
-
result, accept_socket = tcp.accept(listen_socket[, accept_callback(result, accept_socket, listen_socket)]) --accept_callback is a continues callback, blocking if callback is nil.
-
result, connect_socket = tcp.connect(host_addr, host_port[, timeout [, connect_callback(result, connect_socket, host_addr, host_port[, timeout])]]) --connect_callback is a once callback, blocking if callback is nil.
-
result, connect_socket = tcp.connects(sock_name[, timeout [, connect_callback(result, connect_socket, host_addr, host_port[, timeout])]]) --connect_callback is a once callback, blocking if callback is nil.
-
result, connect_socket = tcp.connect6(host_addr, host_port[, timeout [, connect_callback(result, connect_socket, host_addr, host_port[, timeout])]]) --connect_callback is a once callback, blocking if callback is nil.
-
result, buffer = tcp.read(socket[, timeout]) --recv_callback is a continues callback, blocking if callback is nil.
-
result, buffer = tcp.read(socket[, recv_callback(result, buffer, socket)]) --recv_callback is a continues callback, blocking if callback is nil.
-
result, error = tcp.write(socket, buffer_or_lstring[, send_callback(result, error, socket, buffer_or_lstring)/bool safety]) --send_callback is a once callback and is safety assurance, blocking until buffer_or_lstring is sent only if safety is true.
-
tcp.set_rwopt(socket, option_table) --set tcp_socket read and write options --e.g. { "read_head_endian" = "L", "read_head_bytes" = 2, "read_head_max" = 65535, }
-
option_table = tcp.get_rwopt(socket) --get tcp_socket read and write options --e.g. { "read_head_endian" = "L", "read_head_bytes" = 2, "read_head_max" = 65535, }
-
tcp.set_nodelay(socket, enable)
-
result, error = context.send(handle, data) --QUERY
-
result, query_data = context.query(handle, data[, timeout [, query_callback(result, query_data, handle, data[, timeout])]]) --QUERY(real query) --query_callback is a once callback, blocking if callback is nil.
-
result, error = context.reply(handle, session, data) --REPLY
-
result, data, recv_handle, session = context.recv(handle[, timeout]) --recv_callback is a continues callback, blocking if callback is nil.
-
result, data, recv_handle, session = context.recv(handle[, recv_callback(result, data, recv_handle, session, handle)]) --recv_callback is a continues callback, blocking if callback is nil.
-
result, error = context.wait(handle[, timeout[, callback(result, error, handle[, timeout])]]) --connect_callback is a once callback, blocking if callback is nil.
-
timer.sleep(seconds)
-
timer.timeout(seconds, ..., callback(...))
-
timer.loop(interval, repeat_time, ..., callback(...))