Project Alice
Loading...
Searching...
No Matches
network.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <string>
5#ifdef _WIN64 // WINDOWS
6#define _WINSOCK_DEPRECATED_NO_WARNINGS 1
7#ifndef WINSOCK2_IMPORTED
8#define WINSOCK2_IMPORTED
9#include <winsock2.h>
10#include <ws2tcpip.h>
11#endif
12#else // NIX
13#include <netinet/in.h>
14#include <sys/socket.h>
15#endif
16#include "SPSCQueue.h"
17#include "container_types.hpp"
18#include "commands.hpp"
19
20namespace sys {
21struct state;
22};
23
24namespace network {
25
26inline constexpr short default_server_port = 1984;
27
28#ifdef _WIN64
29typedef SOCKET socket_t;
30#else
31typedef int socket_t;
32#endif
33
36 uint8_t password[16] = {0};
37 uint8_t reserved[48] = {0};
38};
39
44 dcon::nation_id assigned_nation;
45 uint8_t reserved[64] = {0};
46};
47
49 dcon::nation_id playing_as{};
51 struct sockaddr_storage address;
52
55 size_t recv_count = 0;
56 std::vector<char> send_buffer;
57 std::vector<char> early_send_buffer;
58
59 // accounting for save progress
60 size_t total_sent_bytes = 0;
62 size_t save_stream_size = 0;
63 bool handshake = true;
64
65 bool is_banned(sys::state& state) const;
66 inline bool is_active() const {
67 return socket_fd > 0;
68 }
69};
70
75 struct sockaddr_storage address;
77 std::array<client_data, 128> clients;
78 std::vector<struct in6_addr> v6_banlist;
79 std::vector<struct in_addr> v4_banlist;
80 std::string ip_address = "127.0.0.1";
81 std::vector<char> send_buffer;
82 std::vector<char> early_send_buffer;
84 std::vector<uint8_t> save_data; //client
85 ankerl::unordered_dense::map<int32_t, sys::player_name> map_of_player_names;
86 std::unique_ptr<uint8_t[]> current_save_buffer;
87 size_t recv_count = 0;
90 uint8_t password[16] = { 0 };
91 std::atomic<bool> save_slock = false;
92 bool as_v6 = false;
93 bool as_server = false;
94 bool save_stream = false; //client
95 bool is_new_game = true; // has save been loaded?
96 bool out_of_sync = false; // network -> game state signal
97 bool reported_oos = false; // has oos been reported to host yet?
98 bool handshake = true; // if in handshake mode -> send handshake data
99 bool finished = false; //game can run after disconnection but only to show error messages
100
103};
104
105void init(sys::state& state);
107void finish(sys::state& state, bool notify_host);
108void ban_player(sys::state& state, client_data& client);
109void kick_player(sys::state& state, client_data& client);
110void switch_player(sys::state& state, dcon::nation_id new_n, dcon::nation_id old_n);
111void write_network_save(sys::state& state);
112void broadcast_save_to_clients(sys::state& state, command::payload& c, uint8_t const* buffer, uint32_t length, sys::checksum_key const& k);
114
116private:
117 std::mutex internal_wait;
118 std::thread do_forwarding;
119 bool started = false;
120public:
122 void start_forwarding();
124};
125
126}
#define SOCKET
Definition: findsaddr-udp.c:64
void broadcast_save_to_clients(sys::state &state, command::payload &c, uint8_t const *buffer, uint32_t length, sys::checksum_key const &k)
Definition: network.cpp:775
void write_network_save(sys::state &state)
Definition: network.cpp:759
void broadcast_to_clients(sys::state &state, command::payload &c)
Definition: network.cpp:798
void kick_player(sys::state &state, client_data &client)
Definition: network.cpp:1145
constexpr short default_server_port
Definition: network.hpp:26
void finish(sys::state &state, bool notify_host)
Definition: network.cpp:1090
void switch_player(sys::state &state, dcon::nation_id new_n, dcon::nation_id old_n)
Definition: network.cpp:1152
void ban_player(sys::state &state, client_data &client)
Definition: network.cpp:1131
void send_and_receive_commands(sys::state &state)
Definition: network.cpp:853
int socket_t
Definition: network.hpp:31
void init(sys::state &state)
Definition: network.cpp:497
Definition: constants.hpp:4
uint uint32_t
uchar uint8_t
struct sockaddr_storage address
Definition: network.hpp:51
std::vector< char > send_buffer
Definition: network.hpp:56
size_t total_sent_bytes
Definition: network.hpp:60
bool is_banned(sys::state &state) const
Definition: network.cpp:566
dcon::nation_id playing_as
Definition: network.hpp:49
size_t save_stream_offset
Definition: network.hpp:61
socket_t socket_fd
Definition: network.hpp:50
bool is_active() const
Definition: network.hpp:66
size_t save_stream_size
Definition: network.hpp:62
command::payload recv_buffer
Definition: network.hpp:54
client_handshake_data hshake_buffer
Definition: network.hpp:53
std::vector< char > early_send_buffer
Definition: network.hpp:57
sys::player_name nickname
Definition: network.hpp:35
uint32_t current_save_length
Definition: network.hpp:88
std::vector< char > send_buffer
Definition: network.hpp:81
std::vector< uint8_t > save_data
Definition: network.hpp:84
std::vector< struct in6_addr > v6_banlist
Definition: network.hpp:78
std::string ip_address
Definition: network.hpp:80
rigtorp::SPSCQueue< command::payload > outgoing_commands
Definition: network.hpp:76
std::vector< char > early_send_buffer
Definition: network.hpp:82
std::array< client_data, 128 > clients
Definition: network.hpp:77
ankerl::unordered_dense::map< int32_t, sys::player_name > map_of_player_names
Definition: network.hpp:85
struct sockaddr_storage address
Definition: network.hpp:75
sys::player_name nickname
Definition: network.hpp:73
std::unique_ptr< uint8_t[]> current_save_buffer
Definition: network.hpp:86
uint8_t password[16]
Definition: network.hpp:90
sys::checksum_key current_save_checksum
Definition: network.hpp:74
std::vector< struct in_addr > v4_banlist
Definition: network.hpp:79
std::atomic< bool > save_slock
Definition: network.hpp:91
command::payload recv_buffer
Definition: network.hpp:83
server_handshake_data s_hshake
Definition: network.hpp:72
dcon::nation_id assigned_nation
Definition: network.hpp:44
sys::checksum_key save_checksum
Definition: network.hpp:42
sys::checksum_key scenario_checksum
Definition: network.hpp:41