summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/net/client.hpp2
-rw-r--r--include/net/conn.hpp63
-rw-r--r--include/net/server.hpp5
-rw-r--r--include/net/types.hpp277
-rw-r--r--include/version.hpp2
5 files changed, 21 insertions, 328 deletions
diff --git a/include/net/client.hpp b/include/net/client.hpp
index fdcb27d..bcf47ec 100644
--- a/include/net/client.hpp
+++ b/include/net/client.hpp
@@ -1,8 +1,6 @@
#pragma once
#include <cerrno>
-#include <cstdio>
-#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <string>
diff --git a/include/net/conn.hpp b/include/net/conn.hpp
index 9755f88..6cd6024 100644
--- a/include/net/conn.hpp
+++ b/include/net/conn.hpp
@@ -4,6 +4,7 @@
#include <functional>
#include <map>
#include <span>
+#include <stdexcept>
#include <vector>
#include <netinet/in.h>
@@ -20,6 +21,7 @@ class conn {
using handler = std::function<bool(std::span<uint8_t>)>;
using handler_list = std::vector<handler>;
+
std::array<handler_list, 256> handlers;
std::map<uint16_t, handler_list> netmodule_handlers;
@@ -56,12 +58,6 @@ public:
}
}
- template <class H>
- void reg_handler(int id, H h)
- {
- handlers[id].push_back(h);
- }
-
template <packet::packet P, typename H>
void reg_handler(H h)
{
@@ -107,55 +103,30 @@ public:
send_packet(T::packet_id, payload);
}
- bool handle_netmodule(std::span<uint8_t> payload)
+ template <class T>
+ void handle(T unk_handler)
{
- uint16_t id;
- std::memcpy(&id, payload.data(), sizeof(id));
- id = to_little(id);
-
- if (netmodule_handlers[id].size() <= 0) {
- // currently i won't bother implementing netmodules
- // FIXME: return false
- return true;
+ auto [id, len] = read_header();
+ if (id == 0) {
+ throw std::runtime_error("invalid message id");
}
+ std::vector<uint8_t> payload(len);
+ rw.read(payload);
+ if (handlers[id].size() <= 0) {
+ unk_handler(id, payload);
+ }
for (auto& h : handlers[id]) {
- if (h(payload.subspan(2)) == true) {
- return false;
+ if (h(payload) == true) {
+ return;
}
}
- return true;
}
- bool handle()
- {
- try {
- auto [id, len] = read_header();
- if (id == 0) {
- return false;
- }
-
- std::vector<uint8_t> payload(len);
- rw.read(payload);
- // got netmodule
- if (id == 82) {
- return handle_netmodule(payload);
- }
-
- if (handlers[id].size() <= 0) {
- return false;
- }
-
- for (auto& h : handlers[id]) {
- if (h(payload) == true) {
- break;
- }
- }
- return true;
- } catch (io::file_io::eof e) {
- return false;
- }
+ void handle()
+ {
+ handle([](int, std::vector<uint8_t>) {});
}
};
} \ No newline at end of file
diff --git a/include/net/server.hpp b/include/net/server.hpp
index 9b5e7c5..f2b5240 100644
--- a/include/net/server.hpp
+++ b/include/net/server.hpp
@@ -1,8 +1,6 @@
#pragma once
#include <cerrno>
-#include <cstdio>
-#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <string>
@@ -45,6 +43,9 @@ public:
if (sock_fd < 0)
throw std::runtime_error(strerror(errno));
+ int yes=1;
+ setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
+
int bindStatus = ::bind(sock_fd, (struct sockaddr*)&server_address, sizeof(server_address));
if (bindStatus < 0)
throw std::runtime_error(strerror(errno));
diff --git a/include/net/types.hpp b/include/net/types.hpp
index 5fe4bac..b9453d6 100644
--- a/include/net/types.hpp
+++ b/include/net/types.hpp
@@ -1,15 +1,12 @@
#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 {
namespace packet {
@@ -75,280 +72,6 @@ struct vec2 {
T y;
};
-struct death_reason {
- enum class reason : int {
- Player = 0,
- NPC,
- Projectile,
- Other,
- ProjectileType,
- Item,
- ItemPrefix,
- Custom,
- };
-
- bitset<8> reasons;
-
- int16_t player_index;
- int16_t npc_index;
- int16_t projectile_index;
- uint8_t other_index;
- int16_t projectile_type;
- int16_t item_type;
- uint8_t item_prefix;
- std::string custom;
-};
-
-struct conn_request {
- static constexpr uint8_t packet_id = 1;
-
- std::string ver;
-};
-
-struct disconnect {
- static constexpr uint8_t packet_id = 2;
-
- nstring reason;
-};
-
-struct accept {
- static constexpr uint8_t packet_id = 3;
-
- std::uint8_t client_id;
- uint8_t _ = 0;
-};
-
-struct player_info {
- static constexpr uint8_t packet_id = 4;
-
- uint8_t client_id;
- uint8_t skin_variant;
- uint8_t hair_variant;
- std::string name;
- uint8_t hair_dye;
- uint16_t hide_visuals;
- uint8_t hide_misc;
- rgb hair_color;
- rgb skin_color;
- rgb eye_color;
- rgb shirt_color;
- rgb undershirt_color;
- rgb pants_color;
- rgb shoes_color;
- uint8_t difficulty;
- uint8_t torches;
- uint8_t permanent_buffs;
-};
-
-struct player_inventory_slot {
- static constexpr uint8_t packet_id = 5;
-
- uint8_t client_id;
- int16_t slot_id;
- int16_t amount;
- uint8_t prefix;
- int16_t item_id;
-};
-
-struct request_world_data {
- static constexpr uint8_t packet_id = 6;
-};
-
-struct world_info {
- static constexpr uint8_t packet_id = 7;
-
- // FIXME: i'm not sure this struct is correct
- int32_t time;
- uint8_t day_info;
- uint8_t moon_phase;
- int16_t max_tiles_x;
- int16_t max_tiles_y;
- int16_t spawn_x;
- int16_t spawn_y;
- int16_t world_surface;
- int16_t rock_layer;
- int32_t world_id;
- std::string world_name;
- uint8_t game_mode;
- std::array<uint8_t, 16> world_unique_id;
- std::array<int32_t, 2> world_generator_version;
- uint8_t moon_type;
- std::array<uint8_t, 13> backgrounds;
- uint8_t ice_back_style;
- uint8_t jungle_back_style;
- uint8_t hell_back_style;
- float wind_speed_set;
- uint8_t cloud_number;
- std::array<int32_t, 3> trees;
- std::array<uint8_t, 4> tree_styles;
- std::array<int32_t, 3> cave_backs;
- std::array<uint8_t, 4> cave_back_styles;
- std::array<uint8_t, 13> tree_top_styles;
- float rain;
- bitset<104> flags1;
- std::array<int16_t, 7> ore_tiers_tiles;
- int8_t invasion_type;
- uint64_t lobby_id;
- float sandstorm_severity;
-};
-
-struct request_tiles_at {
- static constexpr uint8_t packet_id = 8;
- int32_t spawn_x;
- int32_t spawn_y;
-};
-
-struct statusbar_text {
- static constexpr uint8_t packet_id = 9;
-
- int32_t status_max;
- nstring status_text;
- uint8_t server_flags;
-};
-
-struct send_tile_data {
- static constexpr uint8_t packet_id = 10;
- static constexpr packet_flag compressed {};
-
- // FIXME: implement
-};
-
-struct spawn_player {
- static constexpr uint8_t packet_id = 12;
-
- uint8_t client_id;
-
- int16_t spawn_x;
- int16_t spawn_y;
- int32_t respawn_timer;
- uint16_t pve_death_count;
- uint16_t pvp_death_count;
- uint8_t spwan_context;
-};
-
-struct player_health {
- static constexpr uint8_t packet_id = 16;
-
- uint8_t client_id;
- uint16_t current;
- 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;
-};
-
-struct send_password {
- static constexpr uint8_t packet_id = 38;
-
- std::string password;
-};
-
-struct player_mana {
- static constexpr uint8_t packet_id = 42;
-
- uint8_t client_id;
- uint16_t current;
- uint16_t max;
-};
-
-struct initial_spawn_player {
- static constexpr uint8_t packet_id = 49;
-};
-
-struct update_player_buffs {
- static constexpr uint8_t packet_id = 50;
-
- uint8_t client_id;
- std::array<uint16_t, max_buffs> buffs;
-};
-
-struct world_evil {
- static constexpr uint8_t packet_id = 57;
-
- uint8_t good;
- uint8_t evil;
- uint8_t blood;
-};
-
-struct player_uuid {
- static constexpr uint8_t packet_id = 68;
-
- std::string uuid;
-};
-
-struct npc_kill_count {
- static constexpr uint8_t packet_id = 83;
-
- uint16_t npc_type;
- uint32_t npc_kill_count;
-};
-
-struct tower_powers {
- static constexpr uint8_t packet_id = 101;
- uint16_t solar, nebula, vertex, stardust;
-};
-
-struct connection_completed {
- static constexpr uint8_t packet_id = 129;
-};
-
-struct monster_types {
- static constexpr uint8_t packet_id = 136;
-
- std::array<std::array<uint16_t, 3>, 2> types;
-};
-
-struct player_loadout {
- static constexpr uint8_t packet_id = 147;
-
- uint8_t client_id;
- uint8_t index;
- uint16_t hide_accessory;
-};
-
-struct damage_player {
- static constexpr uint8_t packet_id = 117;
-
- uint8_t client_id;
- death_reason reason;
- int16_t damage;
- uint8_t hit_direction;
- uint8_t ty;
- 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{nstring::Literal, str};
diff --git a/include/version.hpp b/include/version.hpp
index b5ec59f..a6061fe 100644
--- a/include/version.hpp
+++ b/include/version.hpp
@@ -1,4 +1,4 @@
#pragma once
constexpr auto max_buffs = 44;
-constexpr auto terraria_version = 279; \ No newline at end of file
+constexpr auto terraria_version = 318; \ No newline at end of file