diff options
| author | hovertank3d <[email protected]> | 2025-11-15 18:31:32 +0100 |
|---|---|---|
| committer | hovertank3d <[email protected]> | 2025-11-15 18:31:32 +0100 |
| commit | c2b53f27bad24003867afa5f5f7aedcf6fa7b26d (patch) | |
| tree | b1cbc5ae52957df6c6aeba60c7be5ae35872e709 /include/net | |
| parent | fa318825ef0404816728f733484caa1bc23afa53 (diff) | |
| download | ogurec-c2b53f27bad24003867afa5f5f7aedcf6fa7b26d.tar.gz ogurec-c2b53f27bad24003867afa5f5f7aedcf6fa7b26d.zip | |
minor fixes
Diffstat (limited to 'include/net')
| -rw-r--r-- | include/net/client.hpp | 4 | ||||
| -rw-r--r-- | include/net/conn.hpp | 51 | ||||
| -rw-r--r-- | include/net/types.hpp | 102 |
3 files changed, 105 insertions, 52 deletions
diff --git a/include/net/client.hpp b/include/net/client.hpp index 87b0ea1..fdcb27d 100644 --- a/include/net/client.hpp +++ b/include/net/client.hpp @@ -26,10 +26,10 @@ class client { struct sockaddr_in server_address {}; public: - client(std::string hostname, int port) + client(std::string ip, int port) { server_address.sin_family = AF_INET; - inet_pton(AF_INET, hostname.c_str(), &server_address.sin_addr); + inet_pton(AF_INET, ip.c_str(), &server_address.sin_addr); server_address.sin_port = htons(port); } diff --git a/include/net/conn.hpp b/include/net/conn.hpp index 6f57293..9755f88 100644 --- a/include/net/conn.hpp +++ b/include/net/conn.hpp @@ -86,12 +86,6 @@ public: }); } - template <packet::packet T> - void send_packet(T p) - { - send_packet(T::packet_id, encode_packet(p)); - } - void send_packet(uint8_t id, std::span<uint8_t> payload) { std::vector<uint8_t> packet_data; @@ -106,6 +100,13 @@ public: rw.write(packet_data); } + template <packet::packet T> + void send_packet(T p) + { + auto payload = encode_packet(p); + send_packet(T::packet_id, payload); + } + bool handle_netmodule(std::span<uint8_t> payload) { uint16_t id; @@ -128,29 +129,33 @@ public: bool handle() { - auto [id, len] = read_header(); - if (id == 0) { - return false; - } + try { + auto [id, len] = read_header(); + if (id == 0) { + return false; + } - std::vector<uint8_t> payload(len); - rw.read(payload); + std::vector<uint8_t> payload(len); + rw.read(payload); - // got netmodule - if (id == 82) { - return handle_netmodule(payload); - } + // got netmodule + if (id == 82) { + return handle_netmodule(payload); + } - if (handlers[id].size() <= 0) { - return false; - } + if (handlers[id].size() <= 0) { + return false; + } - for (auto& h : handlers[id]) { - if (h(payload) == true) { - break; + for (auto& h : handlers[id]) { + if (h(payload) == true) { + break; + } } + return true; + } catch (io::file_io::eof e) { + return false; } - return true; } }; }
\ No newline at end of file diff --git a/include/net/types.hpp b/include/net/types.hpp index 13c96e0..5fe4bac 100644 --- a/include/net/types.hpp +++ b/include/net/types.hpp @@ -1,11 +1,14 @@ #pragma once #include <array> +#include <cstdint> #include <cstring> #include <string> #include <unistd.h> +#include <vector> #include "util/bitset.hpp" +#include "util/io.hpp" #include "version.hpp" namespace net { @@ -22,25 +25,41 @@ concept compressed_packet = packet<T> && requires(T x) { x.compressed; }; template <class T> concept netmodule = requires(T x) { x.module_id; }; -struct nstring : std::string { - enum : uint8_t { +struct nstring { + enum ns_type : uint8_t { Literal, Formattable, LocalizationKey, - } mode; - - std::string s; - - nstring(std::string&& s) - : mode(Literal) - , s(s) - { + } type; + + std::string text; + std::vector<std::string> substitutions; + + template <class T> + ssize_t read(io::serialized_io<T>& io) { + uint8_t t; + auto offset = io.read(t); + type = (ns_type(t)); + + offset += io.read(text); + + uint8_t subs_len; + offset += io.read(subs_len); + if (subs_len > 0) { + substitutions.resize(subs_len); + io.read(substitutions); + } + + return offset; } - - nstring(std::string& s) - : mode(Literal) - , s(s) - { + + template <class T> + ssize_t write(io::serialized_io<T>& io) { + auto offset = io.write(uint8_t(type)); + offset += io.write(text); + offset += io.write(uint8_t(substitutions.size())); + offset += io.write(substitutions); + return offset; } }; @@ -50,6 +69,12 @@ struct rgb { uint8_t b; }; +template<class T> +struct vec2 { + T x; + T y; +}; + struct death_reason { enum class reason : int { Player = 0, @@ -209,6 +234,28 @@ struct player_health { uint16_t max; }; +struct drop_item { + static constexpr uint8_t packet_id = 21; + + int16_t old_id; + + vec2<float> position; + vec2<float> velocity; + int16_t stack; + uint8_t prefix; + bool own_ignore; + + int16_t new_id; +}; + +struct player_zones { + static constexpr uint8_t packet_id = 36; + + uint8_t client_id; + uint32_t zone_flags; + uint8_t zone_flags2; +}; + struct request_password { static constexpr uint8_t packet_id = 37; }; @@ -264,22 +311,14 @@ struct tower_powers { uint16_t solar, nebula, vertex, stardust; }; -struct monster_types { - static constexpr uint8_t packet_id = 136; - - std::array<std::array<uint16_t, 3>, 2> types; -}; - struct connection_completed { static constexpr uint8_t packet_id = 129; }; -struct player_zones { - static constexpr uint8_t packet_id = 36; +struct monster_types { + static constexpr uint8_t packet_id = 136; - uint8_t client_id; - uint32_t zone_flags; - uint8_t zone_flags2; + std::array<std::array<uint16_t, 3>, 2> types; }; struct player_loadout { @@ -301,9 +340,18 @@ struct damage_player { int8_t cooldown_counter; }; +// NetModules + +struct client_text { + static constexpr uint8_t module_id = 1; + + nstring command; + nstring text; +}; + inline nstring operator""_ns(const char* str, std::size_t) { - return nstring(str); + return nstring{nstring::Literal, str}; } } |
