feat: do resource location stuff on client

This commit is contained in:
LordMZTE 2022-11-06 23:36:19 +01:00
parent a6c8175118
commit dbe98d0b2b
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
3 changed files with 16 additions and 56 deletions

View file

@ -1,4 +1,5 @@
#include "data.hpp" #include "data.hpp"
#include "resourceloc.hpp"
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
@ -7,16 +8,16 @@
#include <sys/msg.h> #include <sys/msg.h>
namespace client { namespace client {
void sendMsg(int msqid, data::RequestMsg *msg) { void sendMsg(int msqid, data::RequestMapMsg *msg) {
if (msgsnd(msqid, msg, sizeof(data::RequestMsg) - sizeof(long), 0) < 0) { if (msgsnd(msqid, msg, sizeof(data::RequestMapMsg) - sizeof(long), 0) < 0) {
BOOST_LOG_TRIVIAL(fatal) << "send error " << strerror(errno); BOOST_LOG_TRIVIAL(fatal) << "send error " << strerror(errno);
exit(1); exit(1);
} }
} }
std::string recvMsg(int msqid) { std::string recvMsg(int msqid) {
data::ResponseMsg msg; data::ResponseMapMsg msg;
if (msgrcv(msqid, &msg, sizeof(data::ResponseMsg) - sizeof(long), if (msgrcv(msqid, &msg, sizeof(data::ResponseMapMsg) - sizeof(long),
data::clientbound_msg, 0) < 0) { data::clientbound_msg, 0) < 0) {
BOOST_LOG_TRIVIAL(fatal) << "receive error " << strerror(errno); BOOST_LOG_TRIVIAL(fatal) << "receive error " << strerror(errno);
exit(1); exit(1);
@ -36,9 +37,8 @@ void map(key_t key) {
exit(0); exit(0);
} }
data::RequestMsg req; data::RequestMapMsg req;
req.msgtype = data::serverbound_msg; req.msgtype = data::serverbound_msg;
req.type = data::map;
req.datalen = inp.size(); req.datalen = inp.size();
std::copy(inp.begin(), inp.end(), req.data); std::copy(inp.begin(), inp.end(), req.data);
@ -50,20 +50,6 @@ void resolveResourceLoc(key_t key) {
std::string inp; std::string inp;
std::cin >> inp; std::cin >> inp;
auto msqid = msgget(key, 0664); std::cout << resourceloc::resolve(inp) << std::endl;
if (msqid < 0) {
BOOST_LOG_TRIVIAL(fatal) << "msgget fail";
exit(0);
}
data::RequestMsg req;
req.msgtype = data::serverbound_msg;
req.type = data::resolve_resource_loc;
req.datalen = inp.size();
std::copy(inp.begin(), inp.end(), req.data);
sendMsg(msqid, &req);
std::cout << recvMsg(msqid) << std::endl;
} }
} // namespace client } // namespace client

View file

@ -6,21 +6,14 @@ const int clientbound_msg = 2;
key_t getIpcKeyFromExeName(char *argv0); key_t getIpcKeyFromExeName(char *argv0);
enum RequestType { struct RequestMapMsg {
map,
resolve_resource_loc,
};
struct RequestMsg {
long msgtype; long msgtype;
RequestType type;
unsigned int datalen; unsigned int datalen;
char data[128]; char data[128];
}; };
struct ResponseMsg { struct ResponseMapMsg {
long msgtype; long msgtype;
unsigned int datalen; unsigned int datalen;

View file

@ -12,30 +12,16 @@
namespace server { namespace server {
void sendToClient(int msqid, std::string msg) { void sendToClient(int msqid, std::string msg) {
data::ResponseMsg pkt; data::ResponseMapMsg pkt;
pkt.msgtype = data::clientbound_msg; pkt.msgtype = data::clientbound_msg;
pkt.datalen = msg.size(); pkt.datalen = msg.size();
std::copy(msg.begin(), msg.end(), pkt.data); std::copy(msg.begin(), msg.end(), pkt.data);
if (msgsnd(msqid, &pkt, sizeof(data::ResponseMsg) - sizeof(long), 0) < 0) { if (msgsnd(msqid, &pkt, sizeof(data::ResponseMapMsg) - sizeof(long), 0) < 0) {
BOOST_LOG_TRIVIAL(error) << "failed to send response " << strerror(errno); BOOST_LOG_TRIVIAL(error) << "failed to send response " << strerror(errno);
} }
} }
void handleResolveResourceLoc(int msqid, data::RequestMsg *msg) {
std::string s(&msg->data[0], msg->datalen);
BOOST_LOG_TRIVIAL(info) << "got request to resolve resourceloc '" << s << "'";
auto res = resourceloc::resolve(s);
sendToClient(msqid, res);
}
void handleMap(int msqid, mappings::Mappings *mappings, data::RequestMsg *msg) {
std::string s(&msg->data[0], msg->datalen);
BOOST_LOG_TRIVIAL(info) << "got request to map '" << s << "'";
sendToClient(msqid, mappings->map(s));
}
void run(key_t key) { void run(key_t key) {
BOOST_LOG_TRIVIAL(info) << "starting server"; BOOST_LOG_TRIVIAL(info) << "starting server";
@ -50,22 +36,17 @@ void run(key_t key) {
auto mappings = mappings::Mappings::load(); auto mappings = mappings::Mappings::load();
data::RequestMsg msg; data::RequestMapMsg msg;
for (;;) { for (;;) {
if (msgrcv(msqid, &msg, sizeof(data::RequestMsg) - sizeof(long), if (msgrcv(msqid, &msg, sizeof(data::RequestMapMsg) - sizeof(long),
data::serverbound_msg, 0) < 0) { data::serverbound_msg, 0) < 0) {
BOOST_LOG_TRIVIAL(error) << "receive error " << strerror(errno); BOOST_LOG_TRIVIAL(error) << "receive error " << strerror(errno);
continue; continue;
} }
switch (msg.type) { std::string s(msg.data, msg.datalen);
case data::map: BOOST_LOG_TRIVIAL(info) << "got request to map '" << s << "'";
handleMap(msqid, &mappings, &msg); sendToClient(msqid, mappings.map(s));
break;
case data::resolve_resource_loc:
handleResolveResourceLoc(msqid, &msg);
break;
}
} }
} }
} // namespace server } // namespace server