-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgodotsteam_connection_data.h
219 lines (184 loc) · 6.29 KB
/
godotsteam_connection_data.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef GODOTSTEAM_CONNECTION_DATA
#define GODOTSTEAM_CONNECTION_DATA
// SILENCE STEAMWORKS WARNINGS
/////////////////////////////////////////////////
//
// Turn off MSVC-only warning about strcpy
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable : 4996)
#pragma warning(disable : 4828)
#endif
// INCLUDE HEADERS
/////////////////////////////////////////////////
//
// Include Godot headers
#include "core/os/os.h"
#include "scene/main/multiplayer_peer.h"
// Include GodotSteam's Steam SDK headers
#include "../godotsteam/sdk/public/steam/steam_api_flat.h"
#include "godotsteam_constants.h"
// TIME FOR CLASS
/////////////////////////////////////////////////
//
class ConnectionData : public RefCounted {
GDCLASS(ConnectionData, RefCounted);
public:
ConnectionData(){};
~ConnectionData() {
SteamNetworkingMessages()->CloseSessionWithUser(network_identity);
while (pending_retry_packets.size()) {
delete pending_retry_packets.front()->get();
pending_retry_packets.pop_front();
}
}
// ENUMS
/////////////////////////////////////////////
//
enum ChannelManagement {
PING_CHANNEL,
SIZE
};
// STRUCTS
/////////////////////////////////////////////
//
struct Packet {
uint8_t data[k_cbMaxSteamNetworkingSocketsMessageSizeSend];
uint32_t size = 0;
CSteamID sender = CSteamID();
int channel = 0;
int transfer_mode = k_nSteamNetworkingSend_Reliable;
Packet() {}
Packet(const void *p_buffer, uint32 p_buffer_size, int transferMode, int packet_channel) {
ERR_FAIL_COND_MSG(p_buffer_size > k_cbMaxSteamNetworkingSocketsMessageSizeSend, "[STEAM] Tried sending a packet larger than MAX_STEAM_PACKET_SIZE");
memcpy(this->data, p_buffer, p_buffer_size);
this->size = p_buffer_size;
this->sender = CSteamID();
this->channel = packet_channel;
this->transfer_mode = transferMode;
}
};
struct PingPayload {
int peer_id = -1;
CSteamID steam_id = CSteamID();
};
// PROPERTIES
/////////////////////////////////////////////
uint64_t last_msg_timestamp;
SteamNetworkingIdentity network_identity;
int peer_id;
List<Packet *> pending_retry_packets;
CSteamID steam_id;
ConnectionData(CSteamID connection_steam_id) {
this->peer_id = -1;
this->steam_id = connection_steam_id;
this->last_msg_timestamp = 0;
network_identity = SteamNetworkingIdentity();
network_identity.SetSteamID(connection_steam_id);
}
// FUNCTIONS
/////////////////////////////////////////////
//
bool operator==(const ConnectionData &data) { return steam_id == data.steam_id; }
void add_packet(Packet *packet) { pending_retry_packets.push_back(packet); }
Dictionary collect_debug_data() {
Dictionary output;
output["peer_id"] = peer_id;
output["steam_id"] = steam_id.GetAccountID();
output["pending_packet_count"] = pending_retry_packets.size();
SteamNetConnectionRealTimeStatus_t info;
SteamNetworkingMessages()->GetSessionConnectionInfo(network_identity, nullptr, &info);
switch (info.m_eState) {
case k_ESteamNetworkingConnectionState_None:
output["connection_status"] = "None";
break;
case k_ESteamNetworkingConnectionState_Connecting:
output["connection_status"] = "Connecting";
break;
case k_ESteamNetworkingConnectionState_FindingRoute:
output["connection_status"] = "Finding route";
break;
case k_ESteamNetworkingConnectionState_Connected:
output["connection_status"] = "Connected";
break;
case k_ESteamNetworkingConnectionState_ClosedByPeer:
output["connection_status"] = "Closed by peer";
break;
case k_ESteamNetworkingConnectionState_ProblemDetectedLocally:
output["connection_status"] = "Problem detected locally";
break;
case k_ESteamNetworkingConnectionState_FinWait:
output["connection_status"] = "Final wait";
break;
case k_ESteamNetworkingConnectionState_Linger:
output["connection_status"] = "Linger";
break;
case k_ESteamNetworkingConnectionState_Dead:
output["connection_status"] = "Dead";
break;
default:
output["connection_status"] = "Unknown";
break;
}
output["packets_out_per_sec"] = info.m_flOutPacketsPerSec;
output["bytes_out_per_sec"] = info.m_flOutBytesPerSec;
output["packets_in_per_sec"] = info.m_flInPacketsPerSec;
output["bytes_in_per_sec"] = info.m_flInBytesPerSec;
output["connection_quality_local"] = info.m_flConnectionQualityLocal;
output["connection_quality_remote"] = info.m_flConnectionQualityRemote;
output["send_rate_bytes_per_second"] = info.m_nSendRateBytesPerSecond;
output["pending_unreliable"] = info.m_cbPendingUnreliable;
output["pending_reliable"] = info.m_cbPendingReliable;
output["sent_unacked_reliable"] = info.m_cbSentUnackedReliable;
output["queue_time"] = (int64_t)info.m_usecQueueTime;
output["ping"] = info.m_nPing;
return output;
}
Error ping(const PingPayload &p) {
last_msg_timestamp = OS::get_singleton()->get_ticks_msec(); // only ping once per maxDeltaT;
auto packet = new Packet((void *)&p, sizeof(PingPayload), MultiplayerPeer::TRANSFER_MODE_RELIABLE, PING_CHANNEL);
return send(packet);
}
Error ping() {
auto p = PingPayload();
return ping(p);
}
Error send(Packet *packet) {
add_packet(packet);
return sendPending();
}
EResult raw_send(Packet *packet) {
if (packet->channel == ChannelManagement::PING_CHANNEL) {
if (packet->size != sizeof(PingPayload)) {
print_error("Ping is the wrong size, ignoring");
return k_EResultFail;
}
}
return SteamNetworkingMessages()->SendMessageToUser(network_identity, packet->data, packet->size, packet->transfer_mode, packet->channel);
}
Error sendPending() {
while (pending_retry_packets.size() != 0) {
auto packet = pending_retry_packets.front()->get();
auto error_code = raw_send(packet);
if (error_code == k_EResultOK) {
delete packet;
pending_retry_packets.pop_front();
}
else {
if (packet->transfer_mode & k_nSteamNetworkingSend_Reliable) {
WARN_PRINT(vformat("Send error! Reliable, will retry. EResult %s, refer to Main class in docs.", error_code));
break;
//break and try resend later
}
else {
WARN_PRINT(vformat("Send error! Unreliable, won't retry. EResult %s, refer to Main class in docs.", error_code));
delete packet;
pending_retry_packets.pop_front();
//toss the unreliable packet and move on?
}
}
}
return OK;
}
};
#endif // GODOTSTEAM_CONNECTION_DATA_H