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
38 uint8_t reserved[24] = {0};
39};
40
45 dcon::nation_id assigned_nation;
46 uint8_t reserved[64] = {0};
47};
48
50 dcon::nation_id playing_as{};
52 struct sockaddr_storage address;
53
56 size_t recv_count = 0;
57 std::vector<char> send_buffer;
58 std::vector<char> early_send_buffer;
59
60 // accounting for save progress
61 size_t total_sent_bytes = 0;
63 size_t save_stream_size = 0;
64 bool handshake = true;
65
67
68 bool is_banned(sys::state& state) const;
69 inline bool is_active() const {
70 return socket_fd > 0;
71 }
72};
73
79 struct sockaddr_storage address;
81 std::array<client_data, 128> clients;
82 std::vector<struct in6_addr> v6_banlist;
83 std::vector<struct in_addr> v4_banlist;
84 std::string ip_address = "127.0.0.1";
85 std::vector<char> send_buffer;
86 std::vector<char> early_send_buffer;
88 std::vector<uint8_t> save_data; //client
89
90 std::unique_ptr<uint8_t[]> current_save_buffer;
91 size_t recv_count = 0;
95 std::atomic<bool> save_slock = false;
96 bool as_v6 = false;
97 bool as_server = false;
98 bool save_stream = false; //client
99 bool is_new_game = true; // has save been loaded?
100 bool out_of_sync = false; // network -> game state signal
101 bool reported_oos = false; // has oos been reported to host yet?
102 bool handshake = true; // if in handshake mode -> expect handshake data
103 bool finished = false; //game can run after disconnection but only to show error messages
104
107};
108
109void init(sys::state& state);
111void finish(sys::state& state, bool notify_host);
112void ban_player(sys::state& state, client_data& client);
113void kick_player(sys::state& state, client_data& client);
114void switch_player(sys::state& state, dcon::nation_id new_n, dcon::nation_id old_n);
115void write_network_save(sys::state& state);
116void broadcast_save_to_clients(sys::state& state, command::payload& c, uint8_t const* buffer, uint32_t length, sys::checksum_key const& k);
118void clear_socket(sys::state& state, client_data& client);
120
121dcon::mp_player_id create_mp_player(sys::state& state, sys::player_name& name, sys::player_password_raw& password);
122dcon::mp_player_id load_mp_player(sys::state& state, sys::player_name& name, sys::player_password_hash& password_hash, sys::player_password_salt& password_salt);
123void update_mp_player_password(sys::state& state, dcon::mp_player_id player_id, sys::player_name& password);
124dcon::mp_player_id find_mp_player(sys::state& state, sys::player_name name);
125dcon::mp_player_id find_country_player(sys::state& state, dcon::nation_id nation);
126void log_player_nations(sys::state& state);
127
129bool pause_game(sys::state& state);
130bool unpause_game(sys::state& state);
132void notify_player_joins(sys::state& state, sys::player_name name, dcon::nation_id nation);
133
134void load_host_settings(sys::state& state);
135void save_host_settings(sys::state& state);
136
138private:
139 std::mutex internal_wait;
140 std::thread do_forwarding;
141 bool started = false;
142public:
144 void start_forwarding();
146};
147
148}
#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:1301
dcon::mp_player_id find_country_player(sys::state &state, dcon::nation_id nation)
Definition: network.cpp:702
bool pause_game(sys::state &state)
Definition: network.cpp:1246
void place_host_player_after_saveload(sys::state &state)
Definition: network.cpp:1667
void write_network_save(sys::state &state)
Definition: network.cpp:1268
void update_mp_player_password(sys::state &state, dcon::mp_player_id player_id, sys::player_password_raw &password)
Definition: network.cpp:683
dcon::mp_player_id find_mp_player(sys::state &state, sys::player_name name)
Definition: network.cpp:690
void load_host_settings(sys::state &state)
Definition: network.cpp:1686
void clear_socket(sys::state &state, client_data &client)
Definition: network.cpp:502
void broadcast_to_clients(sys::state &state, command::payload &c)
Definition: network.cpp:1324
bool unpause_game(sys::state &state)
Definition: network.cpp:1261
dcon::mp_player_id create_mp_player(sys::state &state, sys::player_name &name, sys::player_password_raw &password)
Definition: network.cpp:662
dcon::mp_player_id load_mp_player(sys::state &state, sys::player_name &name, sys::player_password_hash &password_hash, sys::player_password_salt &password_salt)
Definition: network.cpp:674
void notify_player_joins(sys::state &state, sys::player_name name, dcon::nation_id nation, sys::player_password_raw password)
Definition: network.cpp:895
void kick_player(sys::state &state, client_data &client)
Definition: network.cpp:1628
constexpr short default_server_port
Definition: network.hpp:26
void finish(sys::state &state, bool notify_host)
Definition: network.cpp:1568
void switch_player(sys::state &state, dcon::nation_id new_n, dcon::nation_id old_n)
Definition: network.cpp:1650
void ban_player(sys::state &state, client_data &client)
Definition: network.cpp:1634
void save_host_settings(sys::state &state)
Definition: network.cpp:1710
void send_and_receive_commands(sys::state &state)
Definition: network.cpp:1368
int socket_t
Definition: network.hpp:31
void init(sys::state &state)
Definition: network.cpp:832
void full_reset_after_oos(sys::state &state)
Definition: network.cpp:1064
void log_player_nations(sys::state &state)
Definition: network.cpp:527
Definition: constants.hpp:4
uint uint32_t
uchar uint8_t
struct sockaddr_storage address
Definition: network.hpp:52
std::vector< char > send_buffer
Definition: network.hpp:57
size_t total_sent_bytes
Definition: network.hpp:61
bool is_banned(sys::state &state) const
Definition: network.cpp:1014
sys::date last_seen
Definition: network.hpp:66
dcon::nation_id playing_as
Definition: network.hpp:50
size_t save_stream_offset
Definition: network.hpp:62
socket_t socket_fd
Definition: network.hpp:51
bool is_active() const
Definition: network.hpp:69
size_t save_stream_size
Definition: network.hpp:63
command::payload recv_buffer
Definition: network.hpp:55
client_handshake_data hshake_buffer
Definition: network.hpp:54
std::vector< char > early_send_buffer
Definition: network.hpp:58
sys::player_name nickname
Definition: network.hpp:35
sys::player_password_raw player_password
Definition: network.hpp:36
uint32_t current_save_length
Definition: network.hpp:92
std::vector< char > send_buffer
Definition: network.hpp:85
uint8_t lobby_password[16]
Definition: network.hpp:94
sys::player_password_raw player_password
Definition: network.hpp:77
std::vector< uint8_t > save_data
Definition: network.hpp:88
std::vector< struct in6_addr > v6_banlist
Definition: network.hpp:82
std::string ip_address
Definition: network.hpp:84
rigtorp::SPSCQueue< command::payload > outgoing_commands
Definition: network.hpp:80
std::vector< char > early_send_buffer
Definition: network.hpp:86
std::array< client_data, 128 > clients
Definition: network.hpp:81
struct sockaddr_storage address
Definition: network.hpp:79
sys::player_name nickname
Definition: network.hpp:76
std::unique_ptr< uint8_t[]> current_save_buffer
Definition: network.hpp:90
sys::checksum_key current_save_checksum
Definition: network.hpp:78
std::vector< struct in_addr > v4_banlist
Definition: network.hpp:83
std::atomic< bool > save_slock
Definition: network.hpp:95
command::payload recv_buffer
Definition: network.hpp:87
server_handshake_data s_hshake
Definition: network.hpp:75
dcon::nation_id assigned_nation
Definition: network.hpp:45
sys::checksum_key save_checksum
Definition: network.hpp:43
sys::checksum_key scenario_checksum
Definition: network.hpp:42
Holds important data about the game world, state, and other data regarding windowing,...