feat: improve error handling
This commit is contained in:
parent
dbe98d0b2b
commit
af1141b85d
|
@ -4,14 +4,14 @@
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sys/msg.h>
|
#include <sys/msg.h>
|
||||||
|
|
||||||
namespace client {
|
namespace client {
|
||||||
void sendMsg(int msqid, data::RequestMapMsg *msg) {
|
void sendMsg(int msqid, data::RequestMapMsg *msg) {
|
||||||
if (msgsnd(msqid, msg, sizeof(data::RequestMapMsg) - sizeof(long), 0) < 0) {
|
if (msgsnd(msqid, msg, sizeof(data::RequestMapMsg) - sizeof(long), 0) < 0) {
|
||||||
BOOST_LOG_TRIVIAL(fatal) << "send error " << strerror(errno);
|
throw std::runtime_error(std::string("send error ") + strerror(errno));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,8 +19,7 @@ std::string recvMsg(int msqid) {
|
||||||
data::ResponseMapMsg msg;
|
data::ResponseMapMsg msg;
|
||||||
if (msgrcv(msqid, &msg, sizeof(data::ResponseMapMsg) - 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);
|
throw std::runtime_error(std::string("receive error ") + strerror(errno));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::string(&msg.data[0], msg.datalen);
|
return std::string(&msg.data[0], msg.datalen);
|
||||||
|
@ -33,8 +32,7 @@ void map(key_t key) {
|
||||||
auto msqid = msgget(key, 0664);
|
auto msqid = msgget(key, 0664);
|
||||||
|
|
||||||
if (msqid < 0) {
|
if (msqid < 0) {
|
||||||
BOOST_LOG_TRIVIAL(fatal) << "msgget fail";
|
throw std::runtime_error("msgget fail. is the server running?");
|
||||||
exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data::RequestMapMsg req;
|
data::RequestMapMsg req;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <stdexcept>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
namespace client {
|
namespace client {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "data.hpp"
|
#include "data.hpp"
|
||||||
#include <boost/log/trivial.hpp>
|
#include <boost/log/trivial.hpp>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <stdexcept>
|
||||||
#include <sys/ipc.h>
|
#include <sys/ipc.h>
|
||||||
|
|
||||||
namespace data {
|
namespace data {
|
||||||
|
@ -8,8 +9,7 @@ key_t getIpcKeyFromExeName(char *argv0) {
|
||||||
auto k = ftok(argv0, 'X');
|
auto k = ftok(argv0, 'X');
|
||||||
|
|
||||||
if (k < 0) {
|
if (k < 0) {
|
||||||
BOOST_LOG_TRIVIAL(fatal) << "failed to get IPC key";
|
throw std::runtime_error("failed to get IPC key");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return k;
|
return k;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <stdexcept>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
namespace data {
|
namespace data {
|
||||||
|
|
19
src/main.cpp
19
src/main.cpp
|
@ -3,11 +3,11 @@
|
||||||
#include "server.hpp"
|
#include "server.hpp"
|
||||||
#include <boost/log/trivial.hpp>
|
#include <boost/log/trivial.hpp>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
void main2(int argc, char *argv[]) {
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
BOOST_LOG_TRIVIAL(fatal) << "not enough arguments!";
|
throw std::runtime_error("not enough arguments!");
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(argv[1], "serve") == 0) {
|
if (strcmp(argv[1], "serve") == 0) {
|
||||||
|
@ -17,9 +17,16 @@ int main(int argc, char *argv[]) {
|
||||||
} else if (strcmp(argv[1], "resourceloc") == 0) {
|
} else if (strcmp(argv[1], "resourceloc") == 0) {
|
||||||
client::resolveResourceLoc(data::getIpcKeyFromExeName(argv[0]));
|
client::resolveResourceLoc(data::getIpcKeyFromExeName(argv[0]));
|
||||||
} else {
|
} else {
|
||||||
BOOST_LOG_TRIVIAL(fatal) << "unknown command!";
|
throw std::runtime_error("unknown command!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
try {
|
||||||
|
main2(argc, argv);
|
||||||
|
return 0;
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
BOOST_LOG_TRIVIAL(fatal) << "Exception: " << e.what();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,8 +28,7 @@ void run(key_t key) {
|
||||||
auto msqid = msgget(key, 0664 | IPC_CREAT);
|
auto msqid = msgget(key, 0664 | IPC_CREAT);
|
||||||
|
|
||||||
if (msqid < 0) {
|
if (msqid < 0) {
|
||||||
BOOST_LOG_TRIVIAL(fatal) << "msgget fail";
|
throw std::runtime_error("msgget fail");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_LOG_TRIVIAL(info) << "msqid " << msqid;
|
BOOST_LOG_TRIVIAL(info) << "msqid " << msqid;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <stdexcept>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
namespace server {
|
namespace server {
|
||||||
|
|
Loading…
Reference in a new issue