feat: do resource location stuff on client
This commit is contained in:
parent
a6c8175118
commit
dbe98d0b2b
|
@ -1,4 +1,5 @@
|
|||
#include "data.hpp"
|
||||
#include "resourceloc.hpp"
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
|
@ -7,16 +8,16 @@
|
|||
#include <sys/msg.h>
|
||||
|
||||
namespace client {
|
||||
void sendMsg(int msqid, data::RequestMsg *msg) {
|
||||
if (msgsnd(msqid, msg, sizeof(data::RequestMsg) - sizeof(long), 0) < 0) {
|
||||
void sendMsg(int msqid, data::RequestMapMsg *msg) {
|
||||
if (msgsnd(msqid, msg, sizeof(data::RequestMapMsg) - sizeof(long), 0) < 0) {
|
||||
BOOST_LOG_TRIVIAL(fatal) << "send error " << strerror(errno);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
std::string recvMsg(int msqid) {
|
||||
data::ResponseMsg msg;
|
||||
if (msgrcv(msqid, &msg, sizeof(data::ResponseMsg) - sizeof(long),
|
||||
data::ResponseMapMsg msg;
|
||||
if (msgrcv(msqid, &msg, sizeof(data::ResponseMapMsg) - sizeof(long),
|
||||
data::clientbound_msg, 0) < 0) {
|
||||
BOOST_LOG_TRIVIAL(fatal) << "receive error " << strerror(errno);
|
||||
exit(1);
|
||||
|
@ -36,9 +37,8 @@ void map(key_t key) {
|
|||
exit(0);
|
||||
}
|
||||
|
||||
data::RequestMsg req;
|
||||
data::RequestMapMsg req;
|
||||
req.msgtype = data::serverbound_msg;
|
||||
req.type = data::map;
|
||||
req.datalen = inp.size();
|
||||
std::copy(inp.begin(), inp.end(), req.data);
|
||||
|
||||
|
@ -50,20 +50,6 @@ void resolveResourceLoc(key_t key) {
|
|||
std::string inp;
|
||||
std::cin >> inp;
|
||||
|
||||
auto msqid = msgget(key, 0664);
|
||||
|
||||
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;
|
||||
std::cout << resourceloc::resolve(inp) << std::endl;
|
||||
}
|
||||
} // namespace client
|
||||
|
|
11
src/data.hpp
11
src/data.hpp
|
@ -6,21 +6,14 @@ const int clientbound_msg = 2;
|
|||
|
||||
key_t getIpcKeyFromExeName(char *argv0);
|
||||
|
||||
enum RequestType {
|
||||
map,
|
||||
resolve_resource_loc,
|
||||
};
|
||||
|
||||
struct RequestMsg {
|
||||
struct RequestMapMsg {
|
||||
long msgtype;
|
||||
|
||||
RequestType type;
|
||||
|
||||
unsigned int datalen;
|
||||
char data[128];
|
||||
};
|
||||
|
||||
struct ResponseMsg {
|
||||
struct ResponseMapMsg {
|
||||
long msgtype;
|
||||
|
||||
unsigned int datalen;
|
||||
|
|
|
@ -12,30 +12,16 @@
|
|||
|
||||
namespace server {
|
||||
void sendToClient(int msqid, std::string msg) {
|
||||
data::ResponseMsg pkt;
|
||||
data::ResponseMapMsg pkt;
|
||||
pkt.msgtype = data::clientbound_msg;
|
||||
pkt.datalen = msg.size();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
BOOST_LOG_TRIVIAL(info) << "starting server";
|
||||
|
||||
|
@ -50,22 +36,17 @@ void run(key_t key) {
|
|||
|
||||
auto mappings = mappings::Mappings::load();
|
||||
|
||||
data::RequestMsg msg;
|
||||
data::RequestMapMsg msg;
|
||||
for (;;) {
|
||||
if (msgrcv(msqid, &msg, sizeof(data::RequestMsg) - sizeof(long),
|
||||
if (msgrcv(msqid, &msg, sizeof(data::RequestMapMsg) - sizeof(long),
|
||||
data::serverbound_msg, 0) < 0) {
|
||||
BOOST_LOG_TRIVIAL(error) << "receive error " << strerror(errno);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (msg.type) {
|
||||
case data::map:
|
||||
handleMap(msqid, &mappings, &msg);
|
||||
break;
|
||||
case data::resolve_resource_loc:
|
||||
handleResolveResourceLoc(msqid, &msg);
|
||||
break;
|
||||
}
|
||||
std::string s(msg.data, msg.datalen);
|
||||
BOOST_LOG_TRIVIAL(info) << "got request to map '" << s << "'";
|
||||
sendToClient(msqid, mappings.map(s));
|
||||
}
|
||||
}
|
||||
} // namespace server
|
||||
|
|
Loading…
Reference in New Issue