diff options
Diffstat (limited to 'include/net')
| -rw-r--r-- | include/net/client.hpp | 2 | ||||
| -rw-r--r-- | include/net/conn.hpp | 129 | ||||
| -rw-r--r-- | include/net/packet.hpp | 6 | ||||
| -rw-r--r-- | include/net/server.hpp | 25 | ||||
| -rw-r--r-- | include/net/types.hpp | 32 |
5 files changed, 99 insertions, 95 deletions
diff --git a/include/net/client.hpp b/include/net/client.hpp index 8f81638..234ca90 100644 --- a/include/net/client.hpp +++ b/include/net/client.hpp @@ -22,7 +22,7 @@ namespace net { class client { - struct sockaddr_in server_address {}; + struct sockaddr_in server_address { }; public: client(std::string hostname, int port) diff --git a/include/net/conn.hpp b/include/net/conn.hpp index b69dac6..6569973 100644 --- a/include/net/conn.hpp +++ b/include/net/conn.hpp @@ -16,9 +16,9 @@ class conn { sockaddr_in conn_addr; socklen_t conn_addr_size; - using handler_list = std::vector<std::function<bool(std::span<uint8_t>)>>; - std::array<handler_list, 256> handlers; - std::map<uint16_t, handler_list> netmodule_handlers; + using handler_list = std::vector<std::function<bool(std::span<uint8_t>)>>; + std::array<handler_list, 256> handlers; + std::map<uint16_t, handler_list> netmodule_handlers; private: packet::packet_header read_header() @@ -29,14 +29,14 @@ private: recv(packet_size); recv(id); - return { id, packet_size - 3}; + return { id, packet_size - 3 }; } public: conn(int sock_fd, sockaddr_in conn_addr, socklen_t conn_addr_size) - : sock_fd(sock_fd) - , conn_addr(conn_addr) - , conn_addr_size(conn_addr_size) + : sock_fd(sock_fd) + , conn_addr(conn_addr) + , conn_addr_size(conn_addr_size) { } @@ -67,9 +67,9 @@ public: ssize_t recv(std::span<uint8_t> data) { - if (data.size_bytes() == 0) { - return 0; - } + if (data.size_bytes() == 0) { + return 0; + } auto bytes = ::recv(sock_fd, data.data(), data.size_bytes(), 0); if (bytes <= 0) { @@ -98,34 +98,36 @@ public: auto [id, len] = read_header(); assert(id == T::packet_id); - if(len > 0) { + if (len > 0) { std::vector<uint8_t> payload(len); recv(payload); decode_packet(payload, t); } } - template <packet::packet P, typename H> - void reg_handler(H h) { - handlers[P::packet_id].push_back([this, h](std::span<uint8_t> payload) { - P t; - if (payload.size() > 0) { - packet::decode_packet(payload, t); + template <packet::packet P, typename H> + void reg_handler(H h) + { + handlers[P::packet_id].push_back([this, h](std::span<uint8_t> payload) { + P t; + if (payload.size() > 0) { + packet::decode_packet(payload, t); } - return h(t); - }); - } - - template <packet::netmodule P, typename H> - void reg_handler(H h) { - netmodule_handlers[P::module_id].push_back([this, h](std::span<uint8_t> payload) { - P t; - if (payload.size() > 0) { - packet::decode_packet(payload, t); + return h(t); + }); + } + + template <packet::netmodule P, typename H> + void reg_handler(H h) + { + netmodule_handlers[P::module_id].push_back([this, h](std::span<uint8_t> payload) { + P t; + if (payload.size() > 0) { + packet::decode_packet(payload, t); } - return h(t); - }); - } + return h(t); + }); + } template <packet::packet T> void send_packet(T p) @@ -137,52 +139,53 @@ public: if (payload.size() > 0) { send(payload); } - } - - bool handle_netmodule(std::span<uint8_t> payload) { + } + + bool handle_netmodule(std::span<uint8_t> payload) + { uint16_t id; std::memcpy(&id, payload.data(), sizeof(id)); id = to_little(id); - if (netmodule_handlers[id].size() <= 0) { + if (netmodule_handlers[id].size() <= 0) { // currently i won't bother implementing netmodules // FIXME: return false - return true; - } - - for (auto &h : handlers[id]) { - if (h(payload.subspan(2)) == true) { - return false; - } - } - return true; + return true; + } + + for (auto& h : handlers[id]) { + if (h(payload.subspan(2)) == true) { + return false; + } + } + return true; } - bool handle() { - auto [id, len] = read_header(); - if (id == 0) { - return false; - } + bool handle() + { + auto [id, len] = read_header(); + if (id == 0) { + return false; + } - std::vector<uint8_t> payload(len); - recv(payload); + std::vector<uint8_t> payload(len); + recv(payload); - //got netmodule - if (id == 82) { + // 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; - } + if (handlers[id].size() <= 0) { + return false; + } + for (auto& h : handlers[id]) { + if (h(payload) == true) { + break; + } + } + return true; + } }; }
\ No newline at end of file diff --git a/include/net/packet.hpp b/include/net/packet.hpp index f2094c0..117fc15 100644 --- a/include/net/packet.hpp +++ b/include/net/packet.hpp @@ -62,7 +62,7 @@ std::vector<uint8_t> encode_packet(T& t) template <compressed_packet T> std::vector<uint8_t> encode_packet(T& t) { - static constexpr auto chunk_size = 1024; + static constexpr auto chunk_size = 1024; std::array<uint8_t, chunk_size> buffer; std::vector<uint8_t> payload; @@ -82,10 +82,10 @@ std::vector<uint8_t> encode_packet(T& t) strm.next_in = raw_payload.data(); do { strm.avail_out = chunk_size; - strm.next_out = reinterpret_cast<Bytef *>(buffer.data()); + strm.next_out = reinterpret_cast<Bytef*>(buffer.data()); ret = deflate(&strm, flush); assert(ret != Z_STREAM_ERROR); - payload.insert(payload.end(), buffer.begin(), buffer.end()-strm.avail_out); + payload.insert(payload.end(), buffer.begin(), buffer.end() - strm.avail_out); } while (strm.avail_out == 0); assert(strm.avail_in == 0); /* all input will be used */ diff --git a/include/net/server.hpp b/include/net/server.hpp index 14a5d1d..9a27a25 100644 --- a/include/net/server.hpp +++ b/include/net/server.hpp @@ -22,11 +22,10 @@ namespace net { class server { - struct sockaddr_in server_address {}; + struct sockaddr_in server_address { }; int sock_fd; public: - server(std::string hostname, int port) { server_address.sin_family = AF_INET; @@ -34,30 +33,34 @@ public: server_address.sin_port = htons(port); } - ~server() { + ~server() + { close(sock_fd); } - void bind() { + void bind() + { sock_fd = socket(AF_INET, SOCK_STREAM, 0); - if (sock_fd < 0) + if (sock_fd < 0) throw std::runtime_error(strerror(errno)); - + int bindStatus = ::bind(sock_fd, (struct sockaddr*)&server_address, sizeof(server_address)); if (bindStatus < 0) throw std::runtime_error(strerror(errno)); } - void listen(int max_connections) { + void listen(int max_connections) + { ::listen(sock_fd, max_connections); } - - conn accept() { + + conn accept() + { sockaddr_in conn_addr {}; socklen_t conn_addr_size = sizeof(conn_addr); - int conn_fd = ::accept(sock_fd, (struct sockaddr *)&conn_addr, &conn_addr_size); - if (conn_fd < 0) + int conn_fd = ::accept(sock_fd, (struct sockaddr*)&conn_addr, &conn_addr_size); + if (conn_fd < 0) throw std::runtime_error(strerror(errno)); return conn { conn_fd, conn_addr, conn_addr_size }; } diff --git a/include/net/types.hpp b/include/net/types.hpp index 9ebc56c..8f0e383 100644 --- a/include/net/types.hpp +++ b/include/net/types.hpp @@ -25,7 +25,7 @@ template <class T> concept packet = requires(T x) { x.packet_id; }; template <class T> -concept compressed_packet = requires(T x) { packet<T> && x.compressed; }; +concept compressed_packet = requires(T x) { packet<T>&& x.compressed; }; template <class T> concept netmodule = requires(T x) { x.module_id; }; @@ -59,7 +59,7 @@ struct rgb { }; struct death_reason { - enum class reason : int { + enum class reason : int { Player = 0, NPC, Projectile, @@ -71,7 +71,7 @@ struct death_reason { }; bitset<8> reasons; - + int16_t player_index; int16_t npc_index; int16_t projectile_index; @@ -126,7 +126,7 @@ void encode_field(std::vector<uint8_t>& data, std::array<T, sz>& f) } template <typename T> -void encode_field(std::vector<uint8_t>& data, std::vector<T> &f) +void encode_field(std::vector<uint8_t>& data, std::vector<T>& f) { for (auto& e : f) { encode_field(data, e); @@ -134,7 +134,7 @@ void encode_field(std::vector<uint8_t>& data, std::vector<T> &f) } template <typename T> -void encode_field(std::vector<uint8_t>& data, std::span<T> &f) +void encode_field(std::vector<uint8_t>& data, std::span<T>& f) { for (auto& e : f) { encode_field(data, e); @@ -142,14 +142,14 @@ void encode_field(std::vector<uint8_t>& data, std::span<T> &f) } template <unsigned int bits> -void encode_field(std::vector<uint8_t>& data, bitset<bits> &f) +void encode_field(std::vector<uint8_t>& data, bitset<bits>& f) { for (auto& e : f) { encode_field(data, e); } } -void encode_field(std::vector<uint8_t>& data, death_reason &f) +void encode_field(std::vector<uint8_t>& data, death_reason& f) { encode_field(data, f.reasons); if (f.reasons[death_reason::reason::Player]) { @@ -199,14 +199,12 @@ ssize_t decode_field(std::span<uint8_t> data, T& f) return sizeof(T); } - ssize_t decode_field(std::span<uint8_t> data, std::vector<uint8_t>& f) -{ +{ f.insert(f.begin(), data.begin(), data.end()); return f.size(); } - template <typename T, std::size_t sz> ssize_t decode_field(std::span<uint8_t> data, std::array<T, sz>& f) { @@ -220,7 +218,7 @@ ssize_t decode_field(std::span<uint8_t> data, std::array<T, sz>& f) return offset; } -ssize_t decode_field(std::span<uint8_t> data, death_reason &f) +ssize_t decode_field(std::span<uint8_t> data, death_reason& f) { int offset = 0; offset += decode_field(data, f.reasons); @@ -372,7 +370,7 @@ struct send_tile_data { static constexpr uint8_t packet_id = 10; static constexpr packet_flag compressed {}; - //FIXME: implement + // FIXME: implement }; struct spawn_player { @@ -480,12 +478,12 @@ struct player_loadout { struct damage_player { static constexpr uint8_t packet_id = 117; - uint8_t client_id; + uint8_t client_id; death_reason reason; - int16_t damage; - uint8_t hit_direction; - uint8_t ty; - int8_t cooldown_counter; + int16_t damage; + uint8_t hit_direction; + uint8_t ty; + int8_t cooldown_counter; }; template <uint8_t id> |
