Skip to content

Commit fb08877

Browse files
committed
make steam connection work offline while (hopefully) correctly waiting for authentication when online
1 parent 42869ab commit fb08877

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

sources/include/cage-core/networkSteam.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace cage
6868
CAGE_CORE_API Holder<SteamServer> newSteamServer(const SteamServerCreateConfig &config);
6969

7070
// requires steam sdk
71-
// permanently switches implementation to the dedicated server api
71+
// permanently switches implementation to the dedicated server
7272
// must be used before any other functions in this api
7373
// the dedicated server must already be initialized
7474
// cannot mix client api with dedicated server api

sources/libcore/network/steam.cpp

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,18 @@ namespace cage
109109
case k_ESteamNetworkingAvailability_Current:
110110
return true; // all is done
111111
case k_ESteamNetworkingAvailability_CannotTry:
112+
CAGE_THROW_ERROR(Exception, "failed to initialize steam sockets network authentication (cannot try)");
112113
case k_ESteamNetworkingAvailability_Failed:
114+
CAGE_THROW_ERROR(Exception, "failed to initialize steam sockets network authentication (failed)");
113115
case k_ESteamNetworkingAvailability_Previously:
114-
CAGE_THROW_ERROR(Exception, "failed to initialize steam sockets network authentication");
115-
return false;
116+
CAGE_THROW_ERROR(Exception, "failed to initialize steam sockets network authentication (previously)");
116117
default:
117118
return false;
118119
}
119120
}
120121
#endif
121122

122-
void initialize(bool useRelay)
123+
void initialize(bool useAuth, bool useRelay)
123124
{
124125
#if defined(CAGE_USE_STEAM_SOCKETS)
125126
struct InitializerSockets
@@ -143,11 +144,12 @@ namespace cage
143144

144145
if (!sockets)
145146
{
147+
CAGE_ASSERT(!utils);
146148
sockets = SteamNetworkingSockets();
147149
utils = SteamNetworkingUtils();
148-
CAGE_ASSERT(sockets);
149-
CAGE_ASSERT(utils);
150150
}
151+
CAGE_ASSERT(sockets);
152+
CAGE_ASSERT(utils);
151153

152154
struct InitializerConfiguration
153155
{
@@ -156,28 +158,32 @@ namespace cage
156158
utils->SetDebugOutputFunction((ESteamNetworkingSocketsDebugOutputType)(sint32)confDebugLogLevel, &debugOutputHandler);
157159
utils->SetGlobalConfigValueFloat(k_ESteamNetworkingConfig_FakePacketLoss_Send, confSimulatedPacketLoss * 100);
158160
utils->SetGlobalConfigValueInt32(k_ESteamNetworkingConfig_FakePacketLag_Send, (sint32)confSimulatedPacketDelay);
159-
//utils->SetGlobalConfigValueInt32(k_ESteamNetworkingConfig_IPLocalHost_AllowWithoutAuth, 1);
161+
utils->SetGlobalConfigValueInt32(k_ESteamNetworkingConfig_IPLocalHost_AllowWithoutAuth, 1);
160162
}
161163
};
162164
static InitializerConfiguration initializerConfiguration;
163165

164-
#if defined(CAGE_USE_STEAM_SDK)
165-
struct InitializerAuthentication
166+
if (useAuth)
166167
{
167-
InitializerAuthentication()
168+
#if defined(CAGE_USE_STEAM_SDK)
169+
struct InitializerAuthentication
168170
{
169-
ESteamNetworkingAvailability a = sockets->InitAuthentication();
170-
while (!networkingAvailable(a))
171+
InitializerAuthentication()
171172
{
172-
threadSleep(5'000);
173-
SteamAPI_RunCallbacks();
174-
SteamGameServer_RunCallbacks();
175-
a = sockets->GetAuthenticationStatus(nullptr);
173+
while (true)
174+
{
175+
ESteamNetworkingAvailability a = sockets->InitAuthentication();
176+
if (networkingAvailable(a))
177+
break;
178+
threadSleep(5'000);
179+
SteamAPI_RunCallbacks();
180+
SteamGameServer_RunCallbacks();
181+
}
176182
}
177-
}
178-
};
179-
static InitializerAuthentication initializerAuthentication;
183+
};
184+
static InitializerAuthentication initializerAuthentication;
180185
#endif
186+
}
181187

182188
if (useRelay)
183189
{
@@ -261,13 +267,12 @@ namespace cage
261267
bool connected = false;
262268
bool disconnected = false;
263269

264-
SteamConnectionImpl(const String &address, uint16 port)
270+
SteamConnectionImpl(const SteamNetworkingIPAddr sa)
265271
{
266272
CAGE_ASSERT(((uint64)this % 2) == 0);
267273
SteamNetworkingConfigValue_t cfg[2] = {};
268274
cfg[0].SetInt64(k_ESteamNetworkingConfig_ConnectionUserData, (sint64)this);
269275
cfg[1].SetPtr(k_ESteamNetworkingConfig_Callback_ConnectionStatusChanged, (void *)&statusChangedCallback);
270-
SteamNetworkingIPAddr sa = parseAddress(address, port);
271276
sock = sockets->ConnectByIPAddress(sa, array_size(cfg), cfg);
272277
if (sock == 0)
273278
CAGE_THROW_ERROR(Exception, "ConnectByIPAddress returned invalid socket");
@@ -434,7 +439,7 @@ namespace cage
434439
{
435440
impl->waiting.push(systemMemory().createImpl<SteamConnection, SteamConnectionImpl>(info->m_hConn));
436441
}
437-
catch (const ConcurrentQueueTerminated &)
442+
catch (...) // either accepting the connection may fail, or the queue may have been terminated, and both is ignored
438443
{
439444
// nothing
440445
}
@@ -541,19 +546,20 @@ namespace cage
541546

542547
Holder<SteamConnection> newSteamConnection(const String &address, uint16 port)
543548
{
544-
initialize(false);
545-
return systemMemory().createImpl<SteamConnection, SteamConnectionImpl>(address, port);
549+
const SteamNetworkingIPAddr sa = parseAddress(address, port);
550+
initialize(!sa.IsLocalHost(), false);
551+
return systemMemory().createImpl<SteamConnection, SteamConnectionImpl>(sa);
546552
}
547553

548554
Holder<SteamConnection> newSteamConnection(uint64 steamId)
549555
{
550-
initialize(true);
556+
initialize(true, true);
551557
return systemMemory().createImpl<SteamConnection, SteamConnectionImpl>(steamId);
552558
}
553559

554560
Holder<SteamServer> newSteamServer(const SteamServerCreateConfig &config)
555561
{
556-
initialize(config.listenSteamRelay);
562+
initialize(false, config.listenSteamRelay);
557563
return systemMemory().createImpl<SteamServer, SteamServerImpl>(config);
558564
}
559565

@@ -566,7 +572,7 @@ namespace cage
566572
utils = SteamNetworkingUtils();
567573
CAGE_ASSERT(sockets);
568574
CAGE_ASSERT(utils);
569-
initialize(true);
575+
initialize(false, true);
570576
}
571577
#endif // CAGE_USE_STEAM_SDK
572578
}

0 commit comments

Comments
 (0)