From 5899a081daee192f3a9e858431ef36434dfffea9 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Fri, 19 Sep 2014 01:42:52 +0300 Subject: [PATCH] client: Sending of packets from Lua --- share/client/init.lua | 13 ++++++++++++- src/client/app.cpp | 31 +++++++++++++++++++++++++++++++ src/client/main.cpp | 1 - src/client/state.cpp | 8 ++++++-- src/client/state.h | 2 +- src/impl/packet_stream.cpp | 2 ++ test/testmodules/test1/test1.cpp | 2 ++ 7 files changed, 54 insertions(+), 5 deletions(-) diff --git a/share/client/init.lua b/share/client/init.lua index 159ca98..34f0d26 100644 --- a/share/client/init.lua +++ b/share/client/init.lua @@ -12,7 +12,6 @@ local log = buildat:Logger("__client") log:info("init.lua loaded") -print("") require "Polycode/Core" scene = Scene(Scene.SCENE_2D) scene:getActiveCamera():setOrthoSize(640, 480) @@ -20,8 +19,20 @@ label = SceneLabel("Hello from Lua!", 32) label:setPosition(-50, -50, 0) scene:addChild(label) +buildat.packet_subs = {} + function buildat:sub_packet(name, cb) + buildat.packet_subs[name] = cb end function buildat:unsub_packet(cb) + for name, cb1 in pairs(buildat.packet_subs) do + if cb1 == cb then + buildat.packet_subs[cb] = nil + end + end +end + +function buildat:send_packet(name, data) + __buildat_send_packet(name, data) end diff --git a/src/client/app.cpp b/src/client/app.cpp index dbecbae..227cd32 100644 --- a/src/client/app.cpp +++ b/src/client/app.cpp @@ -264,10 +264,20 @@ struct CApp: public Polycode::EventHandler, public App lua_pushstring(L, "defaults"); lua_call(L, 1, 0); + // TODO //luaopen_Physics2D(L); //luaopen_Physics3D(L); //luaopen_UI(L); + lua_pushlightuserdata(L, (void*)this); + lua_setfield(L, LUA_REGISTRYINDEX, "__buildat_app"); + +#define DEF_BUILDAT_FUNC(name) {\ + lua_pushcfunction(L, l_##name);\ + lua_setglobal(L, "__buildat_" #name);\ +} + DEF_BUILDAT_FUNC(send_packet); + ss_ init_lua_path = g_client_config.share_path+"/client/init.lua"; int error = luaL_dofile(L, init_lua_path.c_str()); if(error){ @@ -312,6 +322,27 @@ struct CApp: public Polycode::EventHandler, public App lua_pop(L, 1); } } + + // Non-public methods + + // send_packet(name: string, data: string) + static int l_send_packet(lua_State *L) + { + size_t name_len = 0; + const char *name_c = lua_tolstring(L, 1, &name_len); + ss_ name(name_c, name_len); + size_t data_len = 0; + const char *data_c = lua_tolstring(L, 2, &data_len); + ss_ data(data_c, data_len); + + lua_getfield(L, LUA_REGISTRYINDEX, "__buildat_app"); + CApp *self = (CApp*)lua_touserdata(L, -1); + lua_pop(L, 1); + + self->m_state->send_packet(name, data); + + return 0; + } }; App* createApp(Polycode::PolycodeView *view) diff --git a/src/client/main.cpp b/src/client/main.cpp index c16ee2b..8077d3c 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -100,7 +100,6 @@ int main(int argc, char *argv[]) if(!state->connect(config.server_address, "20000")) return 1; - state->send("foo"); while(app0->update()){ state->update(); diff --git a/src/client/state.cpp b/src/client/state.cpp index 056d4c4..9295192 100644 --- a/src/client/state.cpp +++ b/src/client/state.cpp @@ -40,9 +40,11 @@ struct CState: public State return ok; } - bool send(const ss_ &data) + void send_packet(const ss_ &name, const ss_ &data) { - return m_socket->send_fd(data); + m_packet_stream.output(name, data, [&](const ss_ & packet_data){ + m_socket->send_fd(packet_data); + }); } void update() @@ -116,6 +118,8 @@ struct CState: public State ar(file_content); } // TODO: Check filename for malicious characters "/.\"\\" + // TODO: Never use a filename in the filesystem that was supplied by + // server ss_ path = g_client_config.cache_path+"/remote/"+file_name; std::ofstream of(path, std::ios::binary); of<> %s", cs(name)); + // Create actual packet including type and length std::ostringstream os(std::ios::binary); os<<(char)((type>>0) & 0xff); diff --git a/test/testmodules/test1/test1.cpp b/test/testmodules/test1/test1.cpp index 15a190d..d416e81 100644 --- a/test/testmodules/test1/test1.cpp +++ b/test/testmodules/test1/test1.cpp @@ -83,6 +83,8 @@ struct Module: public interface::Module network::access(m_server, [&](network::Interface * inetwork){ inetwork->send(event.recipient, "core:run_script", "print(\"TODO: Run init.lua\")"); + inetwork->send(event.recipient, "core:run_script", + "buildat:send_packet(\"foo\", \"bar\")"); }); }