client: Sending of packets from Lua
This commit is contained in:
parent
c1fe875a2c
commit
5899a081da
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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();
|
||||
|
@ -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<<file_content;
|
||||
|
@ -16,7 +16,7 @@ namespace client
|
||||
{
|
||||
virtual ~State(){}
|
||||
virtual bool connect(const ss_ &address, const ss_ &port) = 0;
|
||||
virtual bool send(const ss_ &data) = 0;
|
||||
virtual void send_packet(const ss_ &name, const ss_ &data) = 0;
|
||||
virtual void update() = 0;
|
||||
};
|
||||
|
||||
|
@ -81,6 +81,8 @@ void PacketStream::output(const ss_ &name, const ss_ &data,
|
||||
}
|
||||
}
|
||||
|
||||
log_v(MODULE, ">> %s", cs(name));
|
||||
|
||||
// Create actual packet including type and length
|
||||
std::ostringstream os(std::ios::binary);
|
||||
os<<(char)((type>>0) & 0xff);
|
||||
|
@ -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\")");
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user