Debian Codegraph @

common/main/net_udp.h

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 /*
8 *
9 * Prototypes for UDP-protocol network management functions.
10 *
11 */
12
13 #pragma once
14 #include "multi.h"
15 #include "newmenu.h"
16 #include "pack.h"
17 #include "ntstring.h"
18 #include "fwd-window.h"
19 #include "d_array.h"
20 #include "d_bitset.h"
21 #include <array>
22
23 // Exported functions
24 #ifdef DXX_BUILD_DESCENT
25 namespace dsx {
26 namespace multi {
27 namespace udp {
28 struct dispatch_table final : multi::dispatch_table
29 {
30 virtual void send_data(std::span<const uint8_t> data, multiplayer_data_priority priority) const override;
31 virtual void send_data_direct(std::span<const uint8_t> data, playernum_t pnum, int needack) const override;
32 virtual int objnum_is_past(objnum_t objnum) const override;
33 virtual void do_protocol_frame(int force, int listen) const override;
34 virtual window_event_result level_sync() const override;
35 virtual void send_endlevel_packet() const override;
36 virtual void kick_player(const _sockaddr &dump_addr, kick_player_reason why) const override;
37 virtual void disconnect_player(int playernum) const override;
38 virtual int end_current_level(
39 #if DXX_BUILD_DESCENT == 1
40 next_level_request_secret_flag *secret
41 #endif
42 ) const override;
43 virtual void leave_game() const override;
44 };
45
46 extern const dispatch_table dispatch;
47 int kmatrix_poll2(newmenu *menu, const d_event &event, const unused_newmenu_userdata_t *);
48 void leave_game();
49 }
50 using udp::dispatch;
51 }
52
53 window_event_result net_udp_setup_game(void);
54 }
55 #endif
56 void net_udp_manual_join_game();
57 void net_udp_list_join_game(grs_canvas &canvas);
58 window_event_result net_udp_level_sync();
59
60 // Some defines
61 // Our default port - easy to remember: D = 4, X = 24, X = 24
62 namespace dcx {
63 constexpr uint16_t UDP_PORT_DEFAULT = 42424;
64 #define UDP_MANUAL_ADDR_DEFAULT "localhost"
65 #if DXX_USE_TRACKER
66 #ifndef TRACKER_ADDR_DEFAULT
67 /* Allow an alternate default at compile time */
68 #define TRACKER_ADDR_DEFAULT "tracker.dxx-rebirth.com"
69 #endif
70 constexpr uint16_t TRACKER_PORT_DEFAULT = 9999;
71 enum class tracker_game_id : uint16_t
72 {
73 };
74 #endif
75 #define UDP_MAX_NETGAMES 900
76 constexpr std::integral_constant<unsigned, 12> UDP_NETGAMES_PPAGE{}; // Netgames on one page of Netlist
77 }
78 #define UDP_NETGAMES_PAGES 75 // Pages available on Netlist (UDP_MAX_NETGAMES/UDP_NETGAMES_PPAGE)
79 #define UDP_TIMEOUT (5*F1_0) // 5 seconds disconnect timeout
80 #define UDP_MDATA_STOR_QUEUE_SIZE 1024u // Store up to 1024 MDATA packets
81 #define UDP_MDATA_STOR_MIN_FREE_2JOIN 384u // have at least this many free packet slots before we let someone join the game
82 #define UDP_MDATA_PKT_NUM_MIN 1 // start from pkt_num 1 (0 is used to initialize the trace list)
83 #define UDP_MDATA_PKT_NUM_MAX (UDP_MDATA_STOR_QUEUE_SIZE*100) // the max value for pkt_num. roll over when we go any higher. this should be smaller than INT_MAX
84
85 // UDP-Packet identificators (ubyte) and their (max. sizes).
86 #define UPID_MAX_SIZE 1024 // Max size for a packet
87 #define UPID_MDATA_BUF_SIZE 454
88 #if DXX_USE_TRACKER
89 #define UPID_TRACKER_REGISTER 21 // Register or update a game on the tracker.
90 #define UPID_TRACKER_REMOVE 22 // Remove our game from the tracker.
91 #define UPID_TRACKER_REQGAMES 23 // Request a list of all games stored on the tracker.
92 #endif
93
94 // Structure keeping lite game infos (for netlist, etc.)
95 #ifdef DXX_BUILD_DESCENT
96 struct UDP_netgame_info_lite : public prohibit_void_ptr<UDP_netgame_info_lite>
97 {
98 struct _sockaddr game_addr;
99 std::array<short, 3> program_iver;
100 fix GameID;
101 #if DXX_USE_TRACKER
102 tracker_game_id TrackerGameID;
103 #endif
104 ntstring<NETGAME_NAME_LEN> game_name;
105 ntstring<MISSION_NAME_LEN> mission_title;
106 ntstring<8> mission_name;
107 int32_t levelnum;
108 network_game_type gamemode;
109 ubyte RefusePlayers;
110 ubyte difficulty;
111 network_state game_status;
112 ubyte numconnected;
113 ubyte max_numplayers;
114 netgame_rule_flags game_flag;
115 };
116
117 struct player_acknowledgement_mask : enumerated_bitset<MAX_PLAYERS, std::conditional<std::is_same<std::size_t, unsigned>::value, unsigned long, playernum_t>::type>
118 {
119 public:
120 constexpr player_acknowledgement_mask() :
121 enumerated_bitset{(1u << MAX_PLAYERS) - 1}
122 {
123 }
124 };
125 #endif
126
127 // packet structure for multi-buffer
128 struct UDP_mdata_info : prohibit_void_ptr<UDP_mdata_info>
129 {
130 ubyte type;
131 ubyte Player_num;
132 uint16_t mbuf_size;
133 uint32_t pkt_num;
134 std::array<uint8_t, UPID_MDATA_BUF_SIZE> mbuf;
135 };
136
137 #ifdef DXX_BUILD_DESCENT
138 // structure to store MDATA to maybe resend
139 struct UDP_mdata_store : prohibit_void_ptr<UDP_mdata_store>
140 {
141 fix64 pkt_initial_timestamp; // initial timestamp to see if packet is outdated
142 per_player_array<fix64> pkt_timestamp; // Packet timestamp
143 per_player_array<uint32_t> pkt_num; // Packet number
144 sbyte used;
145 ubyte Player_num; // sender of this packet
146 uint16_t data_size;
147 player_acknowledgement_mask player_ack; // 0 if player has not ACK'd this packet, 1 if ACK'd or not connected
148 std::array<uint8_t, UPID_MDATA_BUF_SIZE> data; // extra data of a packet - contains all multibuf data we don't want to loose
149 };
150 #endif
151
152 // structure to keep track of MDATA packets we already got, which we expect from another player and the pkt_num for the next packet we want to send to another player
153 struct UDP_mdata_check : public prohibit_void_ptr<UDP_mdata_check>
154 {
155 std::array<uint32_t, UDP_MDATA_STOR_QUEUE_SIZE> pkt_num; // all those we got just recently, so we can ignore them if we get them again
156 int cur_slot; // index we can use for a new pkt_num
157 uint32_t pkt_num_torecv; // the next pkt_num we await for this player
158 uint32_t pkt_num_tosend; // the next pkt_num we want to send to another player
159 };