client, builtin/client_file: Synchronize transmission end
This commit is contained in:
parent
fdb4751d06
commit
7fb086e399
@ -46,6 +46,8 @@ struct Module: public interface::Module, public client_file::Interface
|
||||
m_server->sub_event(this, Event::t("network:new_client"));
|
||||
m_server->sub_event(this,
|
||||
Event::t("network:packet_received/core:request_file"));
|
||||
m_server->sub_event(this,
|
||||
Event::t("network:packet_received/core:all_files_transferred"));
|
||||
}
|
||||
|
||||
void event(const Event::Type &type, const Event::Private *p)
|
||||
@ -54,6 +56,8 @@ struct Module: public interface::Module, public client_file::Interface
|
||||
EVENT_TYPEN("network:new_client", on_new_client, network::NewClient)
|
||||
EVENT_TYPEN("network:packet_received/core:request_file", on_request_file,
|
||||
network::Packet)
|
||||
EVENT_TYPEN("network:packet_received/core:all_files_transferred",
|
||||
on_all_files_transferred, network::Packet)
|
||||
}
|
||||
|
||||
void on_start()
|
||||
@ -77,8 +81,10 @@ struct Module: public interface::Module, public client_file::Interface
|
||||
inetwork->send(new_client.info.id, "core:announce_file", os.str());
|
||||
});
|
||||
}
|
||||
m_server->emit_event(ss_()+"client_file:files_sent",
|
||||
new FilesSent(new_client.info.id));
|
||||
network::access(m_server, [&](network::Interface * inetwork){
|
||||
inetwork->send(new_client.info.id,
|
||||
"core:tell_after_all_files_transferred", "");
|
||||
});
|
||||
}
|
||||
|
||||
void on_request_file(const network::Packet &packet)
|
||||
@ -118,6 +124,12 @@ struct Module: public interface::Module, public client_file::Interface
|
||||
});
|
||||
}
|
||||
|
||||
void on_all_files_transferred(const network::Packet &packet)
|
||||
{
|
||||
m_server->emit_event(ss_()+"client_file:files_transmitted",
|
||||
new FilesTransmitted(packet.sender));
|
||||
}
|
||||
|
||||
// Interface
|
||||
|
||||
void add_file_content(const ss_ &name, const ss_ &content)
|
||||
|
@ -4,11 +4,12 @@
|
||||
|
||||
namespace client_file
|
||||
{
|
||||
struct FilesSent: public interface::Event::Private
|
||||
struct FilesTransmitted: public interface::Event::Private
|
||||
{
|
||||
network::PeerInfo::Id recipient;
|
||||
|
||||
FilesSent(const network::PeerInfo::Id &recipient): recipient(recipient){}
|
||||
FilesTransmitted(const network::PeerInfo::Id &recipient): recipient(
|
||||
recipient){}
|
||||
};
|
||||
|
||||
struct Interface
|
||||
|
@ -340,7 +340,7 @@ struct CApp: public Polycode::EventHandler, public App
|
||||
CApp *self = (CApp*)lua_touserdata(L, -1);
|
||||
lua_pop(L, 1);
|
||||
|
||||
try{
|
||||
try {
|
||||
self->m_state->send_packet(name, data);
|
||||
return 0;
|
||||
} catch(std::exception &e){
|
||||
@ -360,7 +360,7 @@ struct CApp: public Polycode::EventHandler, public App
|
||||
CApp *self = (CApp*)lua_touserdata(L, -1);
|
||||
lua_pop(L, 1);
|
||||
|
||||
try{
|
||||
try {
|
||||
ss_ content = self->m_state->get_file_content(name);
|
||||
lua_pushlstring(L, content.c_str(), content.size());
|
||||
return 1;
|
||||
|
@ -26,6 +26,8 @@ struct CState: public State
|
||||
sp_<app::App> m_app;
|
||||
ss_ m_cache_path;
|
||||
sm_<ss_, ss_> m_file_hashes; // name -> hash
|
||||
set_<ss_> m_waiting_files; // name
|
||||
bool m_tell_after_all_files_transferred_requested = false;
|
||||
|
||||
CState(sp_<app::App> app):
|
||||
m_socket(interface::createTCPSocket()),
|
||||
@ -152,6 +154,15 @@ struct CState: public State
|
||||
ar(file_hash);
|
||||
}
|
||||
send_packet("core:request_file", os.str());
|
||||
m_waiting_files.insert(file_name);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(packet_name == "core:tell_after_all_files_transferred"){
|
||||
if(m_waiting_files.empty()){
|
||||
send_packet("core:all_files_transferred", "");
|
||||
} else {
|
||||
m_tell_after_all_files_transferred_requested = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -166,6 +177,12 @@ struct CState: public State
|
||||
ar(file_hash);
|
||||
ar(file_content);
|
||||
}
|
||||
if(m_waiting_files.count(file_name) == 0){
|
||||
log_w(MODULE, "Received file was not requested: %s %s",
|
||||
cs(interface::sha1::hex(file_hash)), cs(file_name));
|
||||
return;
|
||||
}
|
||||
m_waiting_files.erase(file_name);
|
||||
ss_ file_hash2 = interface::sha1::calculate(file_content);
|
||||
if(file_hash != file_hash2){
|
||||
log_w(MODULE, "Requested file differs in hash: \"%s\": "
|
||||
@ -179,6 +196,12 @@ struct CState: public State
|
||||
log_i(MODULE, "Saving %s to %s", cs(file_name), cs(path));
|
||||
std::ofstream of(path, std::ios::binary);
|
||||
of<<file_content;
|
||||
if(m_tell_after_all_files_transferred_requested){
|
||||
if(m_waiting_files.empty()){
|
||||
send_packet("core:all_files_transferred", "");
|
||||
m_tell_after_all_files_transferred_requested = false;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ struct Module: public interface::Module
|
||||
m_server->sub_event(this, Event::t("core:start"));
|
||||
m_server->sub_event(this, m_EventType_test1_thing);
|
||||
m_server->sub_event(this, Event::t("network:new_client"));
|
||||
m_server->sub_event(this, Event::t("client_file:files_sent"));
|
||||
m_server->sub_event(this, Event::t("client_file:files_transmitted"));
|
||||
m_server->sub_event(this, Event::t("network:packet_received"));
|
||||
}
|
||||
|
||||
@ -45,8 +45,8 @@ struct Module: public interface::Module
|
||||
EVENT_VOIDN("core:start", on_start)
|
||||
EVENT_TYPE(m_EventType_test1_thing, on_thing, Thing)
|
||||
EVENT_TYPEN("network:new_client", on_new_client, network::NewClient)
|
||||
EVENT_TYPEN("client_file:files_sent", on_files_sent,
|
||||
client_file::FilesSent)
|
||||
EVENT_TYPEN("client_file:files_transmitted", on_files_transmitted,
|
||||
client_file::FilesTransmitted)
|
||||
EVENT_TYPEN("network:packet_received", on_packet_received, network::Packet)
|
||||
}
|
||||
|
||||
@ -65,21 +65,12 @@ struct Module: public interface::Module
|
||||
|
||||
network::access(m_server, [&](network::Interface * inetwork){
|
||||
inetwork->send(new_client.info.id, "test1:dummy", "dummy data");
|
||||
/*inetwork->send(new_client.info.id, "core:run_script",
|
||||
"print(\"Remote script is running\")\n"
|
||||
"require \"Polycode/Core\"\n"
|
||||
"scene = Scene(Scene.SCENE_2D)\n"
|
||||
"scene:getActiveCamera():setOrthoSize(640, 480)\n"
|
||||
"label = SceneLabel(\"Hello from remote module!\", 32)\n"
|
||||
"label:setPosition(0, -100, 0)\n"
|
||||
"scene:addChild(label)\n"
|
||||
);*/
|
||||
});
|
||||
}
|
||||
|
||||
void on_files_sent(const client_file::FilesSent &event)
|
||||
void on_files_transmitted(const client_file::FilesTransmitted &event)
|
||||
{
|
||||
log_v(MODULE, "on_files_sent(): recipient=%zu", event.recipient);
|
||||
log_v(MODULE, "on_files_transmitted(): recipient=%zu", event.recipient);
|
||||
network::access(m_server, [&](network::Interface * inetwork){
|
||||
inetwork->send(event.recipient, "core:run_script",
|
||||
"buildat:run_script_file(\"test1/init.lua\")");
|
||||
|
Loading…
x
Reference in New Issue
Block a user