aboutsummaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorauth <[email protected]>2020-07-05 11:25:10 +0200
committerauth <[email protected]>2020-07-05 11:25:10 +0200
commite1ca40bf09255ea74602a6b31e0e17ae611e3e3b (patch)
treed163ae07461108a2667107153b72180f69981a9c /client
parentReplaced std::byte by uint8_t (diff)
downloadloader-e1ca40bf09255ea74602a6b31e0e17ae611e3e3b.tar.xz
loader-e1ca40bf09255ea74602a6b31e0e17ae611e3e3b.zip
Added support for packet actions.
Added support for client hwid handling. Removed timeout client message.
Diffstat (limited to 'client')
-rw-r--r--client/src/assembler/opcodes.h2
-rw-r--r--client/src/client/client.cpp2
-rw-r--r--client/src/client/client.h1
-rw-r--r--client/src/client/packet.h17
-rw-r--r--client/src/main.cpp40
5 files changed, 36 insertions, 26 deletions
diff --git a/client/src/assembler/opcodes.h b/client/src/assembler/opcodes.h
index cc21ef0..5268b45 100644
--- a/client/src/assembler/opcodes.h
+++ b/client/src/assembler/opcodes.h
@@ -4,6 +4,6 @@
namespace assembler {
enum opcodes : uint8_t {
nop = 0x90,
- ret = 0xc3S
+ ret = 0xc3
};
}; \ No newline at end of file
diff --git a/client/src/client/client.cpp b/client/src/client/client.cpp
index 32a5b37..43ee6c7 100644
--- a/client/src/client/client.cpp
+++ b/client/src/client/client.cpp
@@ -45,7 +45,7 @@ void tcp::client::start(const std::string_view server_ip, const uint16_t port) {
m_active = true;
- io::logger->info("connected.");
+ connect_event.call();
}
int tcp::client::read_stream(std::vector<char>& out) {
diff --git a/client/src/client/client.h b/client/src/client/client.h
index 9bc0eb2..7618913 100644
--- a/client/src/client/client.h
+++ b/client/src/client/client.h
@@ -21,6 +21,7 @@ class client {
public:
std::string session_id;
event<packet_t&> receive_event;
+ event<> connect_event;
client() : m_socket{-1}, m_active{false} {}
diff --git a/client/src/client/packet.h b/client/src/client/packet.h
index 992bee7..f574625 100644
--- a/client/src/client/packet.h
+++ b/client/src/client/packet.h
@@ -9,14 +9,18 @@ constexpr size_t message_len = 512;
enum packet_type : int { write = 0, read };
+enum packet_action : uint8_t { message = 0, hwid = 1, session };
+
struct packet_t {
std::string message;
std::string session_id;
+ uint8_t act;
int id;
packet_t() {}
- packet_t(const std::string_view msg, const packet_type& type,
- std::string_view session = "") {
+ packet_t(const std::string_view msg, const packet_type &type,
+ std::string_view session = "",
+ const packet_action &action = packet_action::message) {
if (type == read) {
++id;
@@ -26,14 +30,16 @@ struct packet_t {
auto json = nlohmann::json::parse(message);
message = json["message"];
session_id = json["session_id"];
-
+ act = json["action"];
} else {
nlohmann::json json;
+ json["action"] = action;
json["session_id"] = session;
json["message"] = msg.data();
message = json.dump();
session_id = session;
+ act = action;
enc::encrypt_message(message);
}
@@ -42,9 +48,12 @@ struct packet_t {
~packet_t() {
message.clear();
session_id.clear();
+ act = -1;
}
- operator bool() const { return !message.empty() && !session_id.empty(); }
+ operator bool() const {
+ return !message.empty() && !session_id.empty() && act != -1;
+ }
auto &operator()() { return message; }
};
}; // namespace tcp
diff --git a/client/src/main.cpp b/client/src/main.cpp
index f0508ce..727e3a0 100644
--- a/client/src/main.cpp
+++ b/client/src/main.cpp
@@ -6,15 +6,14 @@
int main(int argc, char* argv[]) {
io::init();
- assembler::assembler a;
+ /*assembler::assembler a;
a.push({1, 2, 3, 7, 9});
a.end();
for(auto &b : a()) {
io::logger->info("{:x}", b);
}
+ std::cin.get();*/
-
- std::cin.get();
tcp::client client;
std::thread t{tcp::client::monitor, std::ref(client)};
@@ -22,35 +21,36 @@ int main(int argc, char* argv[]) {
client.start("127.0.0.1", 6666);
+ client.connect_event.add([&]() { io::logger->info("connected."); });
+
client.receive_event.add([&](tcp::packet_t& packet) {
if (!packet) return;
auto message = packet();
+ auto action = packet.act;
+
+ // move ?
+ int ret = -1;
- // first packet is the session id and current version
- if (packet.id == 1) {
+ if (action == tcp::packet_action::session) {
client.session_id = packet.session_id;
tcp::version_t v{0, 1, 0};
auto version = fmt::format("{}.{}.{}", v.major, v.minor, v.patch);
- if(version != message) {
- io::logger->error("please update your client");
+ if (version != message) {
+ io::logger->error("please update your client.");
client.shutdown();
}
- return;
- }
- if (message == "timedout") {
- io::logger->warn("connection timeout.");
- client.shutdown();
+ int ret = client.write(tcp::packet_t("hwid", tcp::packet_type::write,
+ client.session_id,
+ tcp::packet_action::hwid));
+ if (ret <= 0) {
+ io::logger->error("failed to send hwid.");
+ client.shutdown();
+ }
}
- io::logger->info("{}:{}->{}", packet.id, packet.session_id, message);
-
- std::string imports;
- client.read_stream(imports);
-
- auto json = nlohmann::json::parse(imports);
- std::ofstream o("o");
- o << std::setw(4) << json;
+ io::logger->info("{}:{}->{} {}", packet.id, packet.session_id, message,
+ packet.act);
});
while (client) {